Clover coverage report - Java Language Levels Test Coverage (javalanglevels-20120305-r5436)
Coverage timestamp: Sun Mar 4 2012 22:02:46 CST
file stats: LOC: 370   Methods: 38
NCLOC: 197   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
VariableData.java 75% 81.8% 76.3% 79.3%
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 junit.framework.TestCase;
 44   
 45    /** Represents the data for a given variable (including fields). */
 46    public class VariableData {
 47   
 48    /**The name of this variable*/
 49    private String _name;
 50   
 51    /** The modifiers and visibility. */
 52    private ModifiersAndVisibility _modifiersAndVisibility;
 53   
 54    /** The type of this variable represented by a SymbolData. */
 55    private SymbolData _type;
 56   
 57    /** True if this variable has been given a value*/
 58    private boolean _hasBeenAssigned;
 59   
 60    /** True if this variable has an initializer*/
 61    private boolean _hasInitializer;
 62   
 63    /** The data that this variable belongs to. For a formal parameter, it is the enclosing class rather than the
 64    * enclosing method. */
 65    private Data _enclosingData;
 66   
 67    /** True iff this is a field we had generated. */
 68    private boolean _generated;
 69   
 70    /** True iff this is a method parameter. */
 71    private boolean _isLocalVariable;
 72   
 73    /** An instance corresponding to this; generated on demand. */
 74    private InstanceData _instance;
 75   
 76    /** Constructor for VariableData. hasInitializer and generated are set to {@code false}.
 77    * @param name The name of the variable
 78    * @param modifiersAndVisibility The modifiersAndVisibility
 79    * @param type The SymbolData type of the variable.
 80    * @param hasBeenAssigned true if this variable has a value
 81    * @param enclosingData the enclosing data
 82    */
 83  293865 public VariableData(String name, ModifiersAndVisibility modifiersAndVisibility, SymbolData type,
 84    boolean hasBeenAssigned, Data enclosingData) {
 85  293865 _name = name;
 86  293865 _modifiersAndVisibility = modifiersAndVisibility;
 87  293865 _type = type;
 88  293865 _hasBeenAssigned = hasBeenAssigned;
 89  293865 _enclosingData = enclosingData;
 90  293865 _hasInitializer = false;
 91  293865 _generated = false;
 92  293865 _isLocalVariable = false;
 93  293865 _instance = null;
 94    }
 95   
 96    /** This constructor is only used when reading method parameters in a class file because class files only store the
 97    * types of method parameters.
 98    */
 99  791928 public VariableData(SymbolData type) {
 100  791928 _name = "";
 101  791928 _modifiersAndVisibility = new ModifiersAndVisibility(SourceInfo.NONE, new String[0]);
 102  791928 _type = type;
 103  791928 _hasBeenAssigned = false;
 104  791928 _isLocalVariable = true;
 105  791928 _instance = null;
 106    }
 107   
 108    /** Make a copy of this VariableData erasing all visibility modifiers and setting hasBeenAssigned to true. Used in
 109    * LanguageLevelVisitor.createConstructor to generate the parameter list of the created constructor.
 110    * @return a new Variable Data identical to this except for erasing visibility modifiers. */
 111  74 public VariableData copyWithoutVisibility() {
 112  74 String[] mavStrings = _modifiersAndVisibility.getModifiers();
 113  74 LinkedList<String> newMavList = new LinkedList<String>();
 114  74 int size = 0;
 115  74 for (int i = 0; i < mavStrings.length; i++) {
 116  145 String mod = mavStrings[i];
 117  145 if (! isVisibility(mod)) {
 118  69 newMavList.add(mod);
 119  69 size++;
 120    }
 121    }
 122  74 String[] newMavStrings = newMavList.toArray(new String[size]);
 123  74 ModifiersAndVisibility newMav = new ModifiersAndVisibility(SourceInfo.NONE, newMavStrings);
 124  74 return new VariableData(_name, newMav, _type, true, _enclosingData);
 125    }
 126   
 127    /** Checks the values of the fields*/
 128  146 public boolean equals(Object obj) {
 129  1 if (obj == null) return false;
 130  145 else if (obj.getClass() != this.getClass()) {
 131    // System.err.println("VariableData.equals: Class equality failure");
 132  0 return false;
 133    }
 134   
 135  145 VariableData vd = (VariableData) obj;
 136   
 137  145 if (! _name.equals(vd.getName())) {
 138    // System.err.println("VariableData.equals: name equality failure");
 139  32 return false;
 140    }
 141  113 if (! _modifiersAndVisibility.equals(vd.getMav())) {
 142    // System.err.println("VariableData.equals: modifiersAndVisibility equality failure");
 143  1 return false;
 144    }
 145   
 146  112 if (! _type.equals(vd.getType())) {
 147    // System.err.println("VariableData.equals: type equality failure");
 148  2 return false;
 149    }
 150   
 151  110 if (_hasBeenAssigned != vd.hasValue()) {
 152    // System.err.println("VariableData.equals: hasBeenAssigned equality failure");
 153  0 return false;
 154    }
 155  110 if (_hasInitializer != vd._hasInitializer) {
 156    // System.err.println("VariableData.equals: hasInitializer equality failure");
 157  0 return false;
 158    }
 159  110 Data otherEnclosingData = vd.getEnclosingData();
 160   
 161  110 if (_enclosingData == null) {
 162  7 if (otherEnclosingData == null) return true;
 163    else {
 164    // System.err.println("VariableData.equals: enclosingData failure");
 165  0 return false;
 166    }
 167    } // formerly .equals but led to infinite loop when _enclosingData is a VariableData
 168  103 else if (_enclosingData != otherEnclosingData) {
 169    // System.err.println("VariableData.equals: enclosingData failure");
 170  0 return false;
 171    }
 172   
 173  103 return true;
 174    }
 175   
 176    /**Hash on the name and enclosing data, since within each enclosing data, the variable name should be unique*/
 177  0 public int hashCode() { return getEnclosingData().hashCode() ^ getName().hashCode(); }
 178   
 179    /**@return a readable representation of the Variable data*/
 180  0 public String toString() {
 181  0 return "VariableData(" + _name + ", " + Arrays.toString(_modifiersAndVisibility.getModifiers()) + ", " + _type +
 182    ", " + _hasBeenAssigned + ")";
 183    }
 184   
 185    /**@return the name of this field or variable*/
 186  8752727 public String getName() { return _name; }
 187   
 188    /**Set the name of this variable to the specified string. */
 189  0 public void setName(String s) { _name = s; }
 190   
 191    /**@return the modifiers and visibility. */
 192  597 public ModifiersAndVisibility getMav() { return _modifiersAndVisibility; }
 193   
 194    /**Set the modifiers and visibility to the specified value. */
 195  23 public void setMav(ModifiersAndVisibility mav) { _modifiersAndVisibility = mav; }
 196   
 197    /** @return the SymbolData representing the type of this variable. */
 198  1589 public SymbolData getType() { return _type;
 199    /* The following appears to break the Augmentor visitor. Why? */
 200    // if (_type != null) return _type;
 201    // return SymbolData.NOT_FOUND;
 202    }
 203   
 204    /** Sets the SymbolData representing the type of this variable. */
 205  6 public void setType(SymbolData type) { _type = type; }
 206   
 207    /** Assumes _type != null
 208    * @return the InstanceData corresponding to the type of this variable. */
 209  1 public InstanceData getInstanceData() {
 210  1 assert _type != null;
 211  1 if (_instance == null) _instance = _type.getInstanceData();
 212  1 return _instance;
 213    }
 214   
 215    /** @return the enclosing data. */
 216  1134 public Data getEnclosingData() { return _enclosingData; }
 217   
 218    /** Sets the enclosing data to the specified value. */
 219  698035 public void setEnclosingData(Data d) { _enclosingData = d; }
 220   
 221    /** @return the _isLocalVariable flag.*/
 222  2 public boolean isLocalVariable() { return _isLocalVariable; }
 223   
 224    /** Sets the isLocalVariable flag to the specified value. */
 225  118 public void setIsLocalVariable(boolean b) { _isLocalVariable = b; }
 226   
 227    /** Adds "final" to the modifiers and visibility for this class, if it is not already there. */
 228  121 public void setFinal() {
 229  121 String[] newModifiers = Utilities.catenate(_modifiersAndVisibility.getModifiers(), new String[]{"final"});
 230    // System.err.println("modifiers with 'final' = " + Arrays.toString(newModifiers));
 231  121 _modifiersAndVisibility = new ModifiersAndVisibility(SourceInfo.NONE, newModifiers);
 232    }
 233   
 234    /** Adds "private" to the modifiers and visibility for this class, if it is not already there. */
 235  81 public void setPrivate() {
 236  81 if (! hasModifier("private")) {
 237  80 String[] modifiers = _modifiersAndVisibility.getModifiers();
 238  80 String[] newModifiers = new String[modifiers.length + 1];
 239  80 newModifiers[0] = "private";
 240  80 for (int i = 1; i <= modifiers.length; i++) {
 241  4 newModifiers[i] = modifiers[i-1];
 242    }
 243  80 _modifiersAndVisibility = new ModifiersAndVisibility(SourceInfo.NONE, newModifiers);
 244    }
 245    }
 246   
 247    /** Add the specified modifier to the modifiers and visibility for this class, if it is not already there. */
 248  0 public void addModifier(String s) {
 249  0 if (! hasModifier(s)) {
 250  0 String[] modifiers = _modifiersAndVisibility.getModifiers();
 251  0 String[] newModifiers = new String[modifiers.length + 1];
 252  0 newModifiers[0] = s;
 253  0 for (int i = 1; i <= modifiers.length; i++) {
 254  0 newModifiers[i] = modifiers[i-1];
 255    }
 256  0 _modifiersAndVisibility = new ModifiersAndVisibility(SourceInfo.NONE, newModifiers);
 257    }
 258    }
 259   
 260    /** Adds "private" and "final" to the modifiers and visibility for this class, if it is not already there. */
 261  0 public void setPrivateAndFinal() {
 262  0 setPrivate();
 263  0 setFinal();
 264    }
 265   
 266    /** Adds "final" and "static" to the modifiers and visibility for this class, if it is not already there. */
 267  0 public void setFinalAndStatic() {
 268  0 setFinal();
 269  0 addModifier("static");
 270    }
 271   
 272    /** Returns true if this VariableData is final. */
 273  413 public boolean isFinal() { return hasModifier("final"); }
 274   
 275    /** Returns true if this VariableData is private. */
 276  95 public boolean isPrivate() { return hasModifier("private"); }
 277   
 278    /** Returns true if this VariableData is static. */
 279  93 public boolean isStatic() { return hasModifier("static"); }
 280   
 281  145 public static boolean isVisibility(String s) {
 282  145 return s.equals("private") || s.equals("protected") || s.equals("public");
 283    }
 284   
 285    /** Returns true if this variable has the modifier specified. */
 286  941 public boolean hasModifier(String modifier) {
 287  941 String[] mavStrings = _modifiersAndVisibility.getModifiers();
 288  941 for (int i = 0; i < mavStrings.length; i++) {
 289  901 if (mavStrings[i].equals(modifier)) {
 290  172 return true;
 291    }
 292    }
 293  769 return false;
 294    }
 295   
 296    /**Set the generated flag to the specified value*/
 297  15 public void setGenerated(boolean value) { _generated = value; }
 298   
 299    /**@return the generated flag*/
 300  0 public boolean isGenerated() { return _generated; }
 301   
 302    /** Returns true iff this variable was declared with an initial value */
 303  87 public boolean hasInitializer() { return _hasInitializer; }
 304   
 305    /** Set the "hasInitializer" flag */
 306  195 public void setHasInitializer(boolean value) { _hasInitializer = value; }
 307   
 308    /** Returns true if this VariableData has been given a value. */
 309  977 public boolean hasValue() { return _hasBeenAssigned; }
 310   
 311    /** Set _hasBeenAssigned flag. */
 312  0 public void setHasValue() { _hasBeenAssigned = true; }
 313   
 314    /** If this VariableData has not been given a value, set _hasBeenAssigned to true, and return true. Otherwise,
 315    * return false to indicate it already has a value and should not be reassigned. */
 316  371 public boolean gotValue() {
 317  245 if (hasValue()) { return false; }
 318  126 _hasBeenAssigned = true;
 319  126 return true;
 320    }
 321   
 322    /** If this VariableData has a value, set _hasBeenAssigned to false, and return true. Otherwise, return false. */
 323  45 public boolean lostValue() {
 324  45 if (hasValue()) {
 325  43 _hasBeenAssigned = false;
 326  43 return true;
 327    }
 328  2 return false;
 329    }
 330   
 331    /** Test the methods defined in the above class. */
 332    public static class VariableDataTest extends TestCase {
 333   
 334    private VariableData _vd;
 335    private VariableData _vd2;
 336   
 337    private ModifiersAndVisibility _publicMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public"});
 338    private ModifiersAndVisibility
 339    _publicMav2 = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public"});
 340    private ModifiersAndVisibility
 341    _protectedMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"protected"});
 342  0 public VariableDataTest() { this(""); }
 343  1 public VariableDataTest(String name) { super(name); }
 344   
 345  1 public void testEquals() {
 346  1 _vd = new VariableData("v", _publicMav, SymbolData.INT_TYPE, true, null);
 347   
 348    //check variables that are equal;
 349  1 _vd2 = new VariableData("v", _publicMav2, SymbolData.INT_TYPE, true, null);
 350  1 assertTrue("Equals should return true if two VariableDatas are equal", _vd.equals(_vd2));
 351  1 assertTrue("Equals should return true in opposite direction as well", _vd2.equals(_vd));
 352   
 353    //comparison to null
 354  1 _vd2 = null;
 355  1 assertFalse("Equals should return false if VariableData is compared to null",_vd.equals(null));
 356   
 357    //different names
 358  1 _vd2 = new VariableData("q", _publicMav, SymbolData.INT_TYPE, true, null);
 359  1 assertFalse("Equals should return false if variable names are different", _vd.equals(_vd2));
 360   
 361    //different MAV
 362  1 _vd2 = new VariableData("v", _protectedMav, SymbolData.INT_TYPE, true, null);
 363  1 assertFalse("Equals should return false if variable modifiers are different", _vd.equals(_vd2));
 364   
 365    //different types
 366  1 _vd2 = new VariableData("v", _publicMav, SymbolData.BOOLEAN_TYPE, true, null);
 367  1 assertFalse("Equals should return false if variable types are different", _vd.equals(_vd2));
 368    }
 369    }
 370    }