Clover coverage report - Java Language Levels Test Coverage (javalanglevels-20120305-r5436)
Coverage timestamp: Sun Mar 4 2012 22:02:46 CST
file stats: LOC: 562   Methods: 23
NCLOC: 354   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
BodyBodyIntermediateVisitor.java 53.1% 90.6% 87% 84.7%
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.*;
 41    import edu.rice.cs.javalanglevels.util.*;
 42    import java.util.*;
 43    import java.io.*;
 44   
 45    import junit.framework.TestCase;
 46   
 47    /** Language Level Visitor for the Intermediate Language Level. Enforces constraints during the
 48    * first walk of the AST (checking for language specific errors and building the symbol table).
 49    * This class enforces things that are common to all contexts reachable within a method body or other body
 50    * (not class or interface body) at the Intermediate Language Level).
 51    */
 52    public class BodyBodyIntermediateVisitor extends IntermediateVisitor {
 53   
 54    /**The MethodData of this method.*/
 55    private BodyData _bodyData;
 56   
 57    /** Constructor for BodyBodyElementaryVisitor.
 58    * @param bodyData The BodyData that encloses the context we are visiting.
 59    * @param file The source file this came from.
 60    * @param packageName The package the source file is in
 61    * @importedFiles A list of classes that were specifically imported
 62    * @param importedPackages A list of package names that were specifically imported
 63    * @param classesInThisFile A list of the classes that are yet to be defined in this source file
 64    * @param continuations A hashtable corresponding to the continuations (unresolved Symbol Datas) that will need
 65    * to be resolved
 66    * @param fixUps A list of commands to be performed after this pass to fixup the symbolTable
 67    */
 68  178 public BodyBodyIntermediateVisitor(BodyData bodyData,
 69    File file,
 70    String packageName,
 71    String enclosingClassName,
 72    LinkedList<String> importedFiles,
 73    LinkedList<String> importedPackages,
 74    HashSet<String> classesInThisFile,
 75    Hashtable<String, Triple<SourceInfo, LanguageLevelVisitor, SymbolData>> continuations,
 76    LinkedList<Command> fixUps,
 77    HashSet<String> innerClassesInThisBody) {
 78  178 super(file,
 79    packageName,
 80    enclosingClassName,
 81    importedFiles,
 82    importedPackages,
 83    classesInThisFile,
 84    continuations,
 85    fixUps);
 86  178 _bodyData = bodyData;
 87  178 assert _enclosingClassName != null;
 88    // _innerClassesInThisBody = innerClassesInThisBody;
 89    }
 90   
 91    /** Give an appropriate error */
 92  1 public Void forMethodDefDoFirst(MethodDef that) {
 93  1 _addError("Methods definitions cannot appear within the body of another method or block.", that);
 94  1 return null;
 95    }
 96   
 97    /* There is currently no way to differentiate between a block statement and
 98    * an instance initializer in a braced body given the general nature of a
 99    * braced body. Whenever an instance initialization is visited in a method
 100    * body, we must assume that it is a block statement.
 101    */
 102  0 public Void forInstanceInitializer(InstanceInitializer that) {
 103  0 return forBlock(that.getCode());
 104    }
 105   
 106    /* Visit this Block within the same BlockData with a new BodyBodyIntermediate visitor (to deal with new local scope?)
 107    * after making sure no errors need to be thrown.*/
 108  37 public Void forBlock(Block that) {
 109  37 forBlockDoFirst(that);
 110  0 if (prune(that)) return null;
 111    // The following code is incomprehensible. Why mutate _bodyData? Why create bd?
 112  37 BlockData bd = new BlockData(_bodyData);
 113  37 _bodyData.addBlock(bd);
 114  37 that.getStatements().visit(new BodyBodyIntermediateVisitor(bd, _file, _package, _enclosingClassName, _importedFiles,
 115    _importedPackages, _classesInThisFile, continuations,
 116    fixUps, new HashSet<String>()));
 117  37 return forBlockOnly(that);
 118    }
 119   
 120    /** Visit the block as in forBlock(), but first add the exception parameter as a variable in that
 121    * lock.
 122    * TODO: move this method into LanguageLevelVisitor. */
 123  12 public Void forCatchBlock(CatchBlock that) {
 124    // System.err.println("***ALARM*** BodyBodyIntermediateVisitor is visiting catch block");
 125  12 forCatchBlockDoFirst(that);
 126  0 if (prune(that)) return null;
 127   
 128  12 Block b = that.getBlock();
 129  12 forBlockDoFirst(b);
 130  0 if (prune(b)) return null;
 131  12 BlockData bd = new BlockData(_bodyData);
 132  12 _bodyData.addBlock(bd);
 133   
 134  12 SymbolData enclosing = getQualifiedSymbolData(_enclosingClassName);
 135   
 136  12 VariableData exceptionVar =
 137    formalParameters2VariableData(new FormalParameter[]{ that.getException() }, enclosing)[0]; // !!!!!! Wny not bd?
 138  0 if (prune(that.getException())) return null;
 139  12 bd.addVar(exceptionVar);
 140   
 141    // System.err.println("Visiting augmented catch block with new visitor!");
 142  12 BodyBodyIntermediateVisitor bbijv =
 143    new BodyBodyIntermediateVisitor(bd, _file, _package, _enclosingClassName, _importedFiles,
 144    _importedPackages, _classesInThisFile, continuations, fixUps,
 145    new HashSet<String>());
 146  12 b.getStatements().visit(bbijv);
 147  12 forBlockOnly(b);
 148  12 return forCatchBlockOnly(that);
 149    }
 150   
 151    /** Adds the variables that were declared to the body data and make sure that no two variables have the same name.*/
 152  19 public Void forVariableDeclarationOnly(VariableDeclaration that) {
 153  19 if (! _bodyData.addFinalVars(_variableDeclaration2VariableData(that, _bodyData))) {
 154    /* Does not allow repeated use of a for loop index variable. TODO: fix this. */
 155    // System.err.println("Generating duplicate variable error");
 156  1 _addAndIgnoreError("You cannot have two variables with the same name.", that);
 157    }
 158  19 return null;
 159    }
 160   
 161    // /** Override method in IntermediateVisitor that throws an error here.*/
 162    // public Void forTryCatchStatementDoFirst(TryCatchStatement that) { return null; /* No errors to throw here. */ }
 163   
 164    /** Process a local inner class definition */
 165  2 public Void forInnerClassDef(InnerClassDef that) {
 166    // TODO: is this necessarily local?
 167  2 SymbolData enclosingClass = _bodyData.getSymbolData();
 168  2 assert _enclosingClassName != null;
 169  2 assert enclosingClass != null;
 170  2 assert _enclosingClassName.equals(getQualifiedClassName(enclosingClass.getName()));
 171   
 172  2 String relName = that.getName().getText();
 173  2 String fullName = _enclosingClassName + '$' + enclosingClass.preincrementLocalClassNum() + relName;
 174    // System.err.println("***ALARM*** Processing local class '" + relName + "' in class " + _enclosingClassName
 175    // + " with flattened class name " + fullName);
 176  2 handleInnerClassDef(that, _bodyData, relName, fullName);
 177    // How do we know that generated number is correct?
 178  2 return null;
 179    }
 180   
 181    /** Process a local inner interface definition */
 182  3 public Void forInnerInterfaceDef(InnerInterfaceDef that) {
 183    // System.err.println("***Signalling local interface error");
 184  3 _addAndIgnoreError("Local interfaces are illegal in Java.", that);
 185    // handleInnerInterfaceDef(that, _bodyData, getQualifiedClassName(_bodyData.getSymbolData().getName()) + '.' +
 186    // _bodyData.getSymbolData().preincrementLocalClassNum() + that.getName().getText());
 187    // // How do we know that generated number is correct?
 188  3 return null;
 189    }
 190   
 191    /** Delegate to helper method. */
 192  1 public Void forSimpleAnonymousClassInstantiation(SimpleAnonymousClassInstantiation that) {
 193  1 SymbolData enclosing = getQualifiedSymbolData(_enclosingClassName);
 194  1 assert enclosing != null;
 195  1 simpleAnonymousClassInstantiationHelper(that, enclosing);
 196  1 return null;
 197    }
 198   
 199    /** Delegate to helper method. */
 200  0 public Void forComplexAnonymousClassInstantiation(ComplexAnonymousClassInstantiation that) {
 201  0 SymbolData enclosing = getQualifiedSymbolData(_enclosingClassName);
 202  0 assert enclosing != null;
 203  0 complexAnonymousClassInstantiationHelper(that, enclosing); // TODO: the wrong enclosing context?
 204  0 return null;
 205    }
 206   
 207    /** If this is the body of a constructor, referencing 'this' is illegal. So, check to see if this is a constructor,
 208    * and if so, throw an error. This should catch both the ComplexThisReference and the SimpleThisReference case.
 209    */
 210    //TODO: it might be nice to create a ConstructorBodyIntermediateVisitor, so this check is not necessary here.
 211  9 public Void forThisReferenceDoFirst(ThisReference that) {
 212  9 if (isConstructor(_bodyData)) {
 213  2 _addAndIgnoreError("You cannot reference the field 'this' inside a constructor at the Intermediate Level", that);
 214    }
 215  9 return null;
 216    }
 217   
 218    /** Process a local variable declaration within a method. Calls the super method to convert these to a VariableData
 219    * array, then makes sure that each VariableData is set to be final, as required at the Intermediate level.
 220    * @param enclosingData The Data which contains the variables
 221    */
 222  19 protected VariableData[] _variableDeclaration2VariableData(VariableDeclaration vd, Data enclosingData) {
 223  19 VariableData[] vds = super._variableDeclaration2VariableData(vd, enclosingData);
 224  19 for (int i = 0; i < vds.length; i++) {
 225  23 if (vds[i].getMav().getModifiers().length > 0) {
 226  0 StringBuilder s = new StringBuilder("the keyword(s) ");
 227  0 String[] modifiers = vds[i].getMav().getModifiers();
 228  0 for (String m: modifiers) { if (! m.equals("final")) s.append("\"" + m + "\" "); }
 229  0 _addAndIgnoreError("You cannot use " + s + "to declare a local variable", vd);
 230    }
 231  23 if (! vds[i].isFinal()) vds[i].setFinal();
 232  23 vds[i].setIsLocalVariable(true); // Was commented out. Why ????
 233    }
 234    // System.err.println("Return VariableDatas " + vds);
 235  19 return vds;
 236    }
 237   
 238    // /** Check for problems with modifiers that are specific to method definitions. */
 239    // public Void forModifiersAndVisibilityDoFirst(ModifiersAndVisibility that) {
 240    // String[] modifiers = that.getModifiers();
 241    // if (Utilities.isAbstract(modifiers) && Utilities.isStatic(modifiers)) _badModifiers("static", "abstract", that);
 242    // return super.forModifiersAndVisibilityDoFirst(that);
 243    // }
 244   
 245    /** Test most of the methods declared above right here: */
 246    public static class BodyBodyIntermediateVisitorTest extends TestCase {
 247   
 248    private BodyBodyIntermediateVisitor _bbv;
 249   
 250    private SymbolData _sd1;
 251    private MethodData _md1;
 252    private ModifiersAndVisibility _publicMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public"});
 253    private ModifiersAndVisibility _protectedMav =
 254    new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"protected"});
 255    private ModifiersAndVisibility _privateMav =
 256    new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"private"});
 257    private ModifiersAndVisibility _packageMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[0]);
 258    private ModifiersAndVisibility _abstractMav =
 259    new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"abstract"});
 260    private ModifiersAndVisibility _finalMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"final"});
 261   
 262   
 263  0 public BodyBodyIntermediateVisitorTest() { this(""); }
 264   
 265  7 public BodyBodyIntermediateVisitorTest(String name) { super(name); }
 266   
 267  7 public void setUp() {
 268  7 _sd1 = new SymbolData("ILikeMonkey");
 269  7 _sd1.setIsContinuation(false);
 270   
 271  7 _md1 = new MethodData("methodName",
 272    _publicMav,
 273    new TypeParameter[0],
 274    SymbolData.INT_TYPE,
 275    new VariableData[0],
 276    new String[0],
 277    _sd1,
 278    null);
 279   
 280  7 errors = new LinkedList<Pair<String, JExpressionIF>>();
 281  7 LanguageLevelConverter.symbolTable.clear();
 282  7 LanguageLevelConverter._newSDs.clear();
 283  7 LanguageLevelConverter.symbolTable.put("ILikeMonkey", _sd1);
 284  7 visitedFiles = new LinkedList<Pair<LanguageLevelVisitor, edu.rice.cs.javalanglevels.tree.SourceFile>>();
 285    // _hierarchy = new Hashtable<String, TypeDefBase>();
 286   
 287  7 _bbv =
 288    new BodyBodyIntermediateVisitor(_md1,
 289    new File(""),
 290    "",
 291    "ILikeMonkey",
 292    new LinkedList<String>(),
 293    new LinkedList<String>(),
 294    new HashSet<String>(),
 295    new Hashtable<String, Triple<SourceInfo, LanguageLevelVisitor, SymbolData>>(),
 296    new LinkedList<Command>(),
 297    new HashSet<String>());
 298   
 299  7 _bbv._classesInThisFile = new HashSet<String>();
 300  7 _bbv.continuations = new Hashtable<String, Triple<SourceInfo, LanguageLevelVisitor, SymbolData>>();
 301    // _bbv._resetNonStaticFields();
 302  7 _bbv._importedPackages.addFirst("java.lang");
 303  7 _sd1.setSuperClass(_bbv.getQualifiedSymbolData("java.lang.Object"));
 304   
 305  7 _errorAdded = false;
 306    }
 307   
 308  1 public void testForMethodDefDoFirst() {
 309  1 ConcreteMethodDef cmd = new ConcreteMethodDef(SourceInfo.NONE,
 310    _packageMav,
 311    new TypeParameter[0],
 312    new PrimitiveType(SourceInfo.NONE, "int"),
 313    new Word(SourceInfo.NONE, "methodName"),
 314    new FormalParameter[0],
 315    new ReferenceType[0],
 316    new BracedBody(SourceInfo.NONE, new BodyItemI[0]));
 317  1 cmd.visit(_bbv);
 318  1 assertEquals("There should be one error.", 1, errors.size());
 319  1 assertEquals("The error message should be correct.",
 320    "Methods definitions cannot appear within the body of another method or block.",
 321    errors.get(0).getFirst());
 322    }
 323   
 324    /* These last two tests are shared with ClassBodyIntermediateVisitor, perhaps we could factor them out. */
 325   
 326  1 public void testForVariableDeclarationOnly() {
 327    // Check one that works
 328  1 VariableDeclaration vdecl = new VariableDeclaration(SourceInfo.NONE,
 329    _packageMav,
 330    new VariableDeclarator[] {
 331    new UninitializedVariableDeclarator(SourceInfo.NONE,
 332    new PrimitiveType(SourceInfo.NONE, "double"),
 333    new Word (SourceInfo.NONE, "field1")),
 334    new UninitializedVariableDeclarator(SourceInfo.NONE,
 335    new PrimitiveType(SourceInfo.NONE, "boolean"),
 336    new Word (SourceInfo.NONE, "field2"))});
 337  1 VariableData vd1 = new VariableData("field1", _finalMav, SymbolData.DOUBLE_TYPE, false, _bbv._bodyData);
 338  1 VariableData vd2 = new VariableData("field2", _finalMav, SymbolData.BOOLEAN_TYPE, false, _bbv._bodyData);
 339  1 vdecl.visit(_bbv);
 340    // if (errors.size() > 0) System.err.println("Error was:" + errors.get(0).getFirst());
 341  1 assertEquals("There should not be any errors.", 0, errors.size());
 342  1 assertTrue("field1 was added.", _md1.getVars().contains(vd1));
 343  1 assertTrue("field2 was added.", _md1.getVars().contains(vd2));
 344   
 345    // Check one that doesn't work
 346  1 VariableDeclaration vdecl2 = new VariableDeclaration(SourceInfo.NONE,
 347    _packageMav,
 348    new VariableDeclarator[] {
 349    new UninitializedVariableDeclarator(SourceInfo.NONE,
 350    new PrimitiveType(SourceInfo.NONE, "double"),
 351    new Word (SourceInfo.NONE, "field3")),
 352    new UninitializedVariableDeclarator(SourceInfo.NONE,
 353    new PrimitiveType(SourceInfo.NONE, "int"),
 354    new Word (SourceInfo.NONE, "field3"))});
 355  1 VariableData vd3 = new VariableData("field3", _finalMav, SymbolData.DOUBLE_TYPE, false, _bbv._bodyData);
 356  1 vdecl2.visit(_bbv);
 357  1 assertEquals("There should be one error.", 1, errors.size());
 358  1 assertEquals("The error message should be correct", "You cannot have two variables with the same name.",
 359    errors.get(0).getFirst());
 360  1 assertTrue("field3 was added.", _md1.getVars().contains(vd3));
 361    }
 362   
 363    // public void testForOtherExpressionOnly() {
 364    // // Test that if the OtherExpressino contains a Word, that the Word is resolved.
 365    // assertFalse("java.lang.System should not be in the symbolTable.",
 366    // LanguageLevelConverter.symbolTable.containsKey("java.lang.System"));
 367    // Expression ex =
 368    // new Expression( SourceInfo.NONE,
 369    // new ExpressionPiece[] { new OtherExpression(SourceInfo.NONE,
 370    // new Word(SourceInfo.NONE, "System"))});
 371    // ex.visit(_bbv);
 372    ////// System.out.println(errors.get(0).getFirst());
 373    //// for (int i = 0; i < errors.size(); i++)
 374    //// System.out.println(errors.get(i).getFirst());
 375    // assertEquals("There should not be any errors.", 0, errors.size());
 376    // assertTrue("java.lang.System should be in the symbolTable.",
 377    // LanguageLevelConverter.symbolTable.containsKey("java.lang.System"));
 378    // }
 379   
 380    /** Generate a block containing a bitwise or operation and the specified SourceInfo si. Note: we need
 381    * to generate error blocks with distinct literal data to avoid generating duplicate error messages
 382    * which are suppressed in the cumulative errors table. */
 383  2 private Block _generateErrorBlock(int litValue) {
 384  2 final SourceInfo si = SourceInfo.NONE;
 385  2 BracedBody errorBody = new BracedBody(si, new BodyItemI[] {
 386    new ExpressionStatement(si,
 387    new BitwiseOrExpression(si,
 388    new SimpleNameReference(si,
 389    new Word(si, "i")),
 390    new IntegerLiteral(si, litValue)))});
 391  2 return new Block(si, errorBody);
 392    }
 393   
 394   
 395  1 public void testForTryCatchStatement() {
 396    //Make sure that no error is thrown
 397  1 BracedBody emptyBody = new BracedBody(SourceInfo.NONE, new BodyItemI[0]);
 398  1 Block b = new Block(SourceInfo.NONE, emptyBody);
 399   
 400  1 NormalTryCatchStatement ntcs = new NormalTryCatchStatement(SourceInfo.NONE, b, new CatchBlock[0]);
 401  1 TryCatchFinallyStatement tcfs = new TryCatchFinallyStatement(SourceInfo.NONE, b, new CatchBlock[0], b);
 402  1 ntcs.visit(_bbv);
 403  1 tcfs.visit(_bbv);
 404  1 assertEquals("After visiting both NormalTryCatchStatement and TryCatchFinallyStatement, there should be no "
 405    + "errors", 0, errors.size());
 406   
 407    // //make sure that if there is an error in one of the bodies, it is caught:
 408    // BracedBody errorBody = new BracedBody(SourceInfo.NONE, new BodyItemI[] {
 409    // new ExpressionStatement(SourceInfo.NONE,
 410    // new BitwiseOrExpression(SourceInfo.NONE,
 411    // new SimpleNameReference(SourceInfo.NONE,
 412    // new Word(SourceInfo.NONE, "i")),
 413    // new IntegerLiteral(SourceInfo.NONE, 10)))});
 414    // Block errorBlock = new Block(SourceInfo.NONE, errorBody);
 415   
 416    // assert ! SourceInfo.TEST_0.equals(SourceInfo.TEST_1);
 417  1 ntcs = new NormalTryCatchStatement(SourceInfo.TEST_0, _generateErrorBlock(0), new CatchBlock[0]);
 418  1 ntcs.visit(_bbv);
 419  1 assertEquals("Should be one error", 1, errors.size());
 420  1 assertEquals("Error message should be correct",
 421    "Bitwise or expressions cannot be used in the functional language level. "
 422    + "Perhaps you meant to compare two values using regular or (||)",
 423    errors.getLast().getFirst());
 424   
 425    //make sure that if there is an error in one of the catch statements, it is caught:
 426  1 UninitializedVariableDeclarator uvd =
 427    new UninitializedVariableDeclarator(SourceInfo.TEST_1,
 428    new PrimitiveType(SourceInfo.TEST_1, "int"),
 429    new Word(SourceInfo.TEST_1, "i"));
 430  1 FormalParameter fp = new FormalParameter(SourceInfo.TEST_1, uvd, false);
 431   
 432  1 tcfs = new TryCatchFinallyStatement(SourceInfo.TEST_1, b, new CatchBlock[] {
 433    new CatchBlock(SourceInfo.TEST_1, fp, _generateErrorBlock(1))
 434    }, b);
 435   
 436  1 assertEquals("Should be one error", 1, errors.size());
 437  1 tcfs.visit(_bbv);
 438  1 assertEquals("Should be two errors", 2, errors.size());
 439  1 assertEquals("Error message should be correct",
 440    "Bitwise or expressions cannot be used in the functional language level."
 441    + " Perhaps you meant to compare two values using regular or (||)",
 442    errors.getLast().getFirst());
 443    }
 444   
 445  1 public void testForThisReferenceDoFirst() {
 446  1 SimpleThisReference str = new SimpleThisReference(SourceInfo.NONE);
 447  1 ComplexThisReference ctr =
 448    new ComplexThisReference(SourceInfo.NONE,
 449    new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "field")));
 450   
 451    //if a this reference occurs outside of a constructor, no error
 452  1 _bbv._bodyData = _md1;
 453  1 str.visit(_bbv);
 454  1 ctr.visit(_bbv);
 455  1 assertEquals("Should be no errors", 0, errors.size());
 456   
 457   
 458    //if a this reference occurs in a constructor, give an error
 459  1 MethodData constr = new MethodData("ILikeMonkey", _publicMav, new TypeParameter[0], _sd1,
 460    new VariableData[0],
 461    new String[0],
 462    _sd1,
 463    null);
 464  1 _bbv._bodyData = constr;
 465  1 str.visit(_bbv);
 466  1 assertEquals("Should be 1 error", 1, errors.size());
 467  1 assertEquals("Error message should be correct",
 468    "You cannot reference the field 'this' inside a constructor at the Intermediate Level",
 469    errors.getLast().getFirst());
 470   
 471  1 ctr.visit(_bbv);
 472  1 assertEquals("Should be 2 errors", 2, errors.size());
 473  1 assertEquals("Error message should be correct",
 474    "You cannot reference the field 'this' inside a constructor at the Intermediate Level",
 475    errors.getLast().getFirst());
 476   
 477   
 478    }
 479   
 480  1 public void testForInnerClassDef() {
 481   
 482    // Test a local inner class definition and reference
 483  1 SymbolData obj = new SymbolData("ILikeMonkey");
 484  1 LanguageLevelConverter.symbolTable.put("ILikeMonkey", obj);
 485  1 InnerClassDef cd0 =
 486    new InnerClassDef(SourceInfo.NONE,
 487    _packageMav,
 488    new Word(SourceInfo.NONE, "Rod"),
 489    new TypeParameter[0],
 490    new ClassOrInterfaceType(SourceInfo.NONE, "ILikeMonkey", new Type[0]),
 491    new ReferenceType[0],
 492    new BracedBody(SourceInfo.NONE, new BodyItemI[0]));
 493  1 cd0.visit(_bbv);
 494  1 assertEquals("There should be no errors", 0, errors.size());
 495  1 SymbolData innerClass1 = _bbv._bodyData.getInnerClassOrInterface("Rod");
 496  1 assertNotNull("Should have a inner class named Rod", innerClass1);
 497   
 498    // Test one with explicit modifiers
 499  1 InnerClassDef cd1 =
 500    new InnerClassDef(SourceInfo.NONE,
 501    _publicMav,
 502    new Word(SourceInfo.NONE, "Todd"),
 503    new TypeParameter[0],
 504    new ClassOrInterfaceType(SourceInfo.NONE, "ILikeMonkey", new Type[0]),
 505    new ReferenceType[0],
 506    new BracedBody(SourceInfo.NONE, new BodyItemI[0]));
 507  1 cd1.visit(_bbv);
 508  1 assertEquals("There should be no errors", 0, errors.size()); // modifiers are allowed
 509  1 SymbolData innerClass2 = _bbv._bodyData.getInnerClassOrInterface("Todd");
 510  1 assertNotNull("Should have a inner class named Todd", innerClass2);
 511    }
 512   
 513  1 public void testForInnerInterfaceDef() {
 514    //Test a trivial inner interface definition
 515  1 InnerInterfaceDef iid =
 516    new InnerInterfaceDef(SourceInfo.NONE,
 517    _packageMav,
 518    new Word(SourceInfo.NONE, "Broken"),
 519    new TypeParameter[0],
 520    new ReferenceType[0],
 521    new BracedBody(SourceInfo.NONE, new BodyItemI[0]));
 522  1 iid.visit(_bbv);
 523  1 assertEquals("There should be one error", 1, errors.size());
 524  1 assertEquals("The error message should be correct",
 525    "Local interfaces are illegal in Java.", errors.get(0).getFirst());
 526  1 SymbolData innerInterface = _bbv._bodyData.getInnerClassOrInterface("Broken");
 527  1 assertNull("Should NOT have a inner interface named Broken", innerInterface);
 528   
 529    // Test a inner interface definition and reference
 530  1 InnerInterfaceDef id0 =
 531    new InnerInterfaceDef(SourceInfo.NONE,
 532    _packageMav,
 533    new Word(SourceInfo.NONE, "RodInterface"),
 534    new TypeParameter[0],
 535    new ReferenceType[0],
 536    new BracedBody(SourceInfo.NONE, new BodyItemI[0]));
 537  1 id0.visit(_bbv);
 538  1 assertEquals("There should be 2 errors", 2, errors.size());
 539  1 assertEquals("The error message should be correct",
 540    "Local interfaces are illegal in Java.", errors.get(1).getFirst());
 541  1 innerInterface = _bbv._bodyData.getInnerClassOrInterface("RodInterface");
 542  1 assertNull("Should NOT have a inner interface named RodInterface", innerInterface);
 543   
 544    // Test one with explicit modifiers
 545  1 InnerInterfaceDef id1 =
 546    new InnerInterfaceDef(SourceInfo.NONE,
 547    _publicMav,
 548    new Word(SourceInfo.NONE, "Todd"),
 549    new TypeParameter[0],
 550    new ReferenceType[0],
 551    new BracedBody(SourceInfo.NONE, new BodyItemI[0]));
 552  1 id1.visit(_bbv);
 553  1 assertEquals("There should be three errors", 3, errors.size()); // class modifiers are allowed
 554  1 assertEquals("The error message should be correct",
 555    "Local interfaces are illegal in Java.", errors.get(2).getFirst());
 556  1 innerInterface = _bbv._bodyData.getInnerClassOrInterface("Todd");
 557  1 assertNull("Should NOT have a inner interface named Todd", innerInterface);
 558    }
 559   
 560  1 public void testDummy() { }
 561    }
 562    }