Clover coverage report - Java Language Levels Test Coverage (javalanglevels-20120305-r5436)
Coverage timestamp: Sun Mar 4 2012 22:02:46 CST
file stats: LOC: 308   Methods: 13
NCLOC: 209   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
InterfaceBodyTypeChecker.java 71.4% 94.3% 84.6% 90.9%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * names of its contributors may be used to endorse or promote products
 15    * derived from this software without specific prior written permission.
 16    *
 17    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.javalanglevels;
 38   
 39    import edu.rice.cs.javalanglevels.tree.*;
 40    import edu.rice.cs.javalanglevels.parser.JExprParser;
 41    import java.util.*;
 42    import java.io.*;
 43    import edu.rice.cs.plt.reflect.JavaVersion;
 44    import edu.rice.cs.plt.iter.*;
 45   
 46    import junit.framework.TestCase;
 47   
 48    /**Do the TypeChecking appropriate to the context of a class body. Common to all Language Levels.*/
 49    public class InterfaceBodyTypeChecker extends SpecialTypeChecker {
 50   
 51    /**The SymbolData corresponding to this interface.*/
 52    private SymbolData _symbolData;
 53   
 54    /* Constructor for InterfaceBodyTypeChecker. Adds all the variables in the symbol data to the list of what can be
 55    * seen from this context, since interface fields can always be seen.
 56    * @param sd The SymbolData of the interface we are type checking.
 57    * @param file The File corresponding to the source file we are checking.
 58    * @param packageName The package of the source file.
 59    * @param importedFiles A list of the names of the classes that are specifically imported in the source file
 60    * @param importedPackages A list of the names of the packages that are imported in the source file.
 61    * @param vars A list of the variable datas that can be seen and have been given a value before this context
 62    * @param thrown The exceptions that are thrown
 63    */
 64  19 public InterfaceBodyTypeChecker(SymbolData sd, File file, String packageName, LinkedList<String> importedFiles, LinkedList<String> importedPackages, LinkedList<VariableData> vars, LinkedList<Pair<SymbolData, JExpression>> thrown) {
 65  19 super(sd, file, packageName, importedFiles, importedPackages, vars, thrown);
 66  19 _vars.addAll(sd.getVars());
 67  19 _symbolData = sd;
 68    }
 69   
 70    /**@return the SymbolData corresponding to the enclosing interface*/
 71  0 protected Data _getData() {
 72  0 return _symbolData;
 73    }
 74   
 75   
 76    /** All fields in interfaces must be initialized where they are declared, so throw an error.*/
 77  1 public TypeData forUninitializedVariableDeclarator(UninitializedVariableDeclarator that) {
 78  1 _addError("All fields in interfaces must be initialized", that);
 79  1 return null;
 80    }
 81   
 82    /**
 83    * Resolve all the stuff that is stored in the AbstractMethodDef. Find the method data
 84    * that was created on the first pass to correspond to this method, and make sure that this
 85    * method doesn't resolve another method with a different return type.
 86    */
 87  13 public TypeData forAbstractMethodDef(AbstractMethodDef that) {
 88  13 final TypeData mavRes = that.getMav().visit(this);
 89  13 final TypeData[] typeParamsRes = makeArrayOfRetType(that.getTypeParams().length);
 90  13 for (int i = 0; i < that.getTypeParams().length; i++) {
 91  0 typeParamsRes[i] = that.getTypeParams()[i].visit(this);
 92    }
 93  13 final TypeData resRes = getSymbolData(that.getResult().getName(), _symbolData, that);//that.getResult().visit(this);
 94  13 final TypeData nameRes = that.getName().visit(this);
 95  13 final TypeData[] paramsRes = makeArrayOfRetType(that.getParams().length);
 96  13 for (int i = 0; i<paramsRes.length; i++) {
 97  6 paramsRes[i] = getSymbolData(that.getParams()[i].getDeclarator().getType().getName(), _symbolData, that.getParams()[i]);
 98    }
 99  13 final TypeData[] throwsRes = makeArrayOfRetType(that.getThrows().length);
 100  13 for (int i = 0; i < that.getThrows().length; i++) {
 101  0 throwsRes[i] = getSymbolData(that.getThrows()[i].getName(), _symbolData, that.getThrows()[i]);//that.getThrows()[i].visit(this);
 102    }
 103    // Ensure that this method doesn't override another method with a different return type.
 104  13 MethodData md = _symbolData.getMethod(that.getName().getText(), paramsRes);
 105  13 if (md == null) {
 106  0 throw new RuntimeException("Internal Program Error: Could not find the method " + that.getName().getText() + " in interface " + _symbolData.getName() + ". Please report this bug.");
 107    }
 108  13 SymbolData.checkDifferentReturnTypes(md, _symbolData, LanguageLevelConverter.OPT.javaVersion());
 109  13 return resRes;
 110    }
 111   
 112   
 113    /**This error should be thrown in the first pass, but throw it again here, just in case*/
 114  1 public TypeData forConcreteMethodDef(ConcreteMethodDef that) {
 115  1 _addError("Concrete method definitions cannot appear in interfaces", that);
 116  1 return null;
 117    }
 118   
 119    /**
 120    * Try to resolve the type that is referenced. Check to see if it is accessible from this context.
 121    */
 122  4 public TypeData forTypeOnly(Type that) {
 123  4 Data sd = getSymbolData(that.getName(), _symbolData, that);
 124  4 if (sd != null) {sd = sd.getOuterData();}
 125  4 while (sd != null && !LanguageLevelVisitor.isJavaLibraryClass(sd.getSymbolData().getName())) {
 126  3 if (!checkAccess(that, sd.getMav(), sd.getName(), sd.getSymbolData(), _symbolData, "class or interface")) {
 127  1 return null;
 128    }
 129  2 sd = sd.getOuterData();
 130    }
 131  3 return null;
 132    }
 133   
 134   
 135    /**
 136    * Test the methods declared in the above class.
 137    */
 138    public static class InterfaceBodyTypeCheckerTest extends TestCase {
 139   
 140    private InterfaceBodyTypeChecker _ibbtc;
 141   
 142    private SymbolData _sd1;
 143    private SymbolData _sd2;
 144    private SymbolData _sd3;
 145    private SymbolData _sd4;
 146    private SymbolData _sd5;
 147    private SymbolData _sd6;
 148    private ModifiersAndVisibility _publicMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public"});
 149    private ModifiersAndVisibility _protectedMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"protected"});
 150    private ModifiersAndVisibility _privateMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"private"});
 151    private ModifiersAndVisibility _packageMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[0]);
 152    private ModifiersAndVisibility _abstractMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"abstract"});
 153    private ModifiersAndVisibility _finalMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"final"});
 154   
 155   
 156  0 public InterfaceBodyTypeCheckerTest() {
 157  0 this("");
 158    }
 159  4 public InterfaceBodyTypeCheckerTest(String name) {
 160  4 super(name);
 161    // _file = File.createTempFile("DrJava-test", ".java");
 162    // _bv = new BeginnerVisitor(new File(""), "", new LinkedList<String>(), new LinkedList<String>(),
 163    // new LinkedList<String>());
 164    }
 165   
 166  4 public void setUp() {
 167  4 _sd1 = new SymbolData("i.like.monkey");
 168  4 _sd2 = new SymbolData("i.like.giraffe");
 169  4 _sd3 = new SymbolData("zebra");
 170  4 _sd4 = new SymbolData("u.like.emu");
 171  4 _sd5 = new SymbolData("");
 172  4 _sd6 = new SymbolData("cebu");
 173  4 errors = new LinkedList<Pair<String, JExpressionIF>>();
 174  4 LanguageLevelConverter.symbolTable.clear();
 175  4 LanguageLevelConverter._newSDs.clear();
 176  4 _ibbtc =
 177    new InterfaceBodyTypeChecker(_sd1, new File(""), "", new LinkedList<String>(), new LinkedList<String>(),
 178    new LinkedList<VariableData>(), new LinkedList<Pair<SymbolData, JExpression>>());
 179  4 LanguageLevelConverter.OPT = new Options(JavaVersion.JAVA_5, EmptyIterable.<File>make());
 180  4 _ibbtc._importedPackages.addFirst("java.lang");
 181    }
 182   
 183  1 public void testForUninitializedVariableDeclaratorOnly() {
 184  1 VariableData vd1 = new VariableData("Mojo", _publicMav, SymbolData.INT_TYPE, false, _sd1);
 185  1 _sd1.addVar(vd1);
 186  1 UninitializedVariableDeclarator uvd =
 187    new UninitializedVariableDeclarator(SourceInfo.NONE,
 188    new PrimitiveType(SourceInfo.NONE, "int"),
 189    new Word(SourceInfo.NONE, "Mojo"));
 190  1 uvd.visit(_ibbtc);
 191  1 _ibbtc.forUninitializedVariableDeclaratorOnly(uvd, SymbolData.INT_TYPE, null);
 192  1 assertEquals("There should be one error", 1, errors.size());
 193  1 assertEquals("The error message should be correct", "All fields in interfaces must be initialized",
 194    errors.get(0).getFirst());
 195    }
 196   
 197  1 public void testForInitializedVariableDeclaratorOnly() {
 198  1 VariableData vd1 = new VariableData("Mojo", _publicMav, SymbolData.INT_TYPE, false, _sd1);
 199  1 _sd1.addVar(vd1);
 200  1 InitializedVariableDeclarator ivd =
 201    new InitializedVariableDeclarator(SourceInfo.NONE,
 202    new PrimitiveType(SourceInfo.NONE, "int"),
 203    new Word(SourceInfo.NONE, "Mojo"),
 204    new IntegerLiteral(SourceInfo.NONE, 1));
 205  1 ivd.visit(_ibbtc);
 206  1 assertEquals("There should be no errors.", 0, errors.size());
 207  1 assertTrue("_vars should contain Mojo.", _ibbtc._vars.contains(vd1));
 208  1 ivd = new InitializedVariableDeclarator(SourceInfo.NONE,
 209    new PrimitiveType(SourceInfo.NONE, "int"),
 210    new Word(SourceInfo.NONE, "Santa's Little Helper"),
 211    new IntegerLiteral(SourceInfo.NONE, 1));
 212  1 try {
 213  1 ivd.visit(_ibbtc);
 214  0 fail("Should have thrown a RuntimeException because there's no field named Santa's Little Helper.");
 215    }
 216    catch (RuntimeException re) {
 217  1 assertEquals("The error message should be correct.",
 218    "Internal Program Error: The field or variable Santa's Little Helper was not found in this block. Please report this bug.", re.getMessage());
 219    }
 220    }
 221   
 222  1 public void testForConcreteMethodDef() {
 223  1 FormalParameter[] fps = new FormalParameter[] {
 224    new FormalParameter(SourceInfo.NONE,
 225    new UninitializedVariableDeclarator(SourceInfo.NONE,
 226    new PrimitiveType(SourceInfo.NONE, "double"),
 227    new Word (SourceInfo.NONE, "field1")),
 228    false),
 229    new FormalParameter(SourceInfo.NONE,
 230    new UninitializedVariableDeclarator(SourceInfo.NONE,
 231    new PrimitiveType(SourceInfo.NONE, "boolean"),
 232    new Word (SourceInfo.NONE, "field2")),
 233    false)};
 234   
 235  1 ConcreteMethodDef cmd =
 236    new ConcreteMethodDef(SourceInfo.NONE,
 237    _packageMav,
 238    new TypeParameter[0],
 239    new PrimitiveType(SourceInfo.NONE, "int"),
 240    new Word(SourceInfo.NONE, "methodName"),
 241    fps,
 242    new ReferenceType[0],
 243    new BracedBody(SourceInfo.NONE, new BodyItemI[] {
 244    new ValueReturnStatement(SourceInfo.NONE,
 245    new IntegerLiteral(SourceInfo.NONE, 5))}));
 246   
 247  1 MethodData md = new MethodData("methodName",
 248    _packageMav,
 249    new TypeParameter[0],
 250    SymbolData.INT_TYPE,
 251    new VariableData[] { new VariableData(SymbolData.DOUBLE_TYPE), new VariableData(SymbolData.BOOLEAN_TYPE) },
 252    new String[0],
 253    _sd1,
 254    null); // no SourceInfo
 255   
 256  1 md.getParams()[0].setEnclosingData(md);
 257  1 md.getParams()[1].setEnclosingData(md);
 258   
 259  1 _sd1.addMethod(md);
 260    // _ibbtc._vars.addLast(new VariableData("field1", _packageMav, SymbolData.DOUBLE_TYPE));
 261  1 cmd.visit(_ibbtc);
 262  1 assertEquals("There should be one error.", 1, errors.size());
 263  1 assertEquals("The error message should be correct", "Concrete method definitions cannot appear in interfaces", errors.get(0).getFirst());
 264    }
 265   
 266  1 public void testForTypeOnly() {
 267  1 Type t = new PrimitiveType(SourceInfo.NONE, "double");
 268  1 t.visit(_ibbtc);
 269  1 assertEquals("There should be no errors", 0, errors.size());
 270   
 271  1 SymbolData sd = new SymbolData("Adam");
 272  1 sd.setIsContinuation(false);
 273  1 symbolTable.put("Adam", sd);
 274  1 sd.setMav(_publicMav);
 275  1 t = new ClassOrInterfaceType(SourceInfo.NONE, "Adam", new Type[0]);
 276  1 t.visit(_ibbtc);
 277  1 assertEquals("There should still be no errors", 0, errors.size());
 278   
 279  1 SymbolData innerSd = new SymbolData("Adam$Wulf");
 280  1 innerSd.setIsContinuation(false);
 281  1 sd.addInnerClass(innerSd);
 282  1 innerSd.setOuterData(sd);
 283  1 innerSd.setMav(_publicMav);
 284  1 _ibbtc.symbolTable.put("USaigehgihdsgslghdlighs", innerSd);
 285  1 t = new ClassOrInterfaceType(SourceInfo.NONE, "Adam.Wulf", new Type[0]);
 286  1 t.visit(_ibbtc);
 287  1 assertEquals("There should still be no errors", 0, errors.size());
 288   
 289  1 innerSd.setMav(_privateMav);
 290  1 t = new ClassOrInterfaceType(SourceInfo.NONE, "Adam.Wulf", new Type[0]);
 291  1 t.visit(_ibbtc);
 292  1 String tcSDName = _ibbtc._symbolData.getName();
 293  1 assertEquals("There should be one error", 1, errors.size());
 294  1 assertEquals("The error message should be correct",
 295    "The class or interface Adam.Wulf in Adam.Wulf is private and cannot be accessed from " + tcSDName,
 296    errors.get(0).getFirst());
 297   
 298  1 sd.setMav(_privateMav);
 299  1 innerSd.setMav(_publicMav);
 300  1 t = new ClassOrInterfaceType(SourceInfo.NONE, "Adam.Wulf", new Type[0]);
 301  1 t.visit(_ibbtc);
 302  1 assertEquals("There should be two errors", 2, errors.size());
 303  1 assertEquals("The error message should be correct",
 304    "The class or interface Adam in Adam is private and cannot be accessed from " + tcSDName,
 305    errors.get(1).getFirst());
 306    }
 307    }
 308    }