Clover coverage report - Java Language Levels Test Coverage (javalanglevels-20120305-r5436)
Coverage timestamp: Sun Mar 4 2012 22:02:46 CST
file stats: LOC: 286   Methods: 26
NCLOC: 135   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
MethodData.java 75% 82.9% 69.2% 79.1%
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.util.Log;
 41    import java.util.*;
 42    import junit.framework.TestCase;
 43    import edu.rice.cs.javalanglevels.parser.JExprParser;
 44   
 45    /** Represents the data for a given method. */
 46    public class MethodData extends BodyData {
 47   
 48    // protected static final Log _log = new Log("MethodData.txt", false);
 49   
 50    /**Generic Type Parameters. Not used at any language level.*/
 51    private TypeParameter[] _typeParameters;
 52   
 53    /**The return type of the method*/
 54    private SymbolData _returnType;
 55   
 56    /**Array of VariableDatas, corresponding to the parameters to the method.*/
 57    private VariableData[] _params;
 58   
 59    /**Array of Strings corresponding to exceptions this method is declared to throw*/
 60    private String[] _thrown;
 61   
 62    /**JExpression corresponding to this method */
 63    private JExpression _jexpr;
 64   
 65    /**True if this method was auto-generated during our language level conversion. False otherwise*/
 66    private boolean _generated;
 67   
 68    /** Constructor for MethodData.
 69    * @param name The String name of the method
 70    * @param modifiersAndVisibility The modifiers of the method
 71    * @param typeParameters The generic type parameters of the method. Not used.
 72    * @param returnType The SymbolData corresponding to the return type of the method
 73    * @param params The VariableData[] corresponding to the method parameters
 74    * @param thrown The Strings corresponding to the exceptions the method is declared to throw
 75    * @param enclosingClass The SymbolData that contains this method
 76    * @param jexpr The JExpression corresponding to this method.
 77    */
 78  803848 public MethodData(String name, ModifiersAndVisibility modifiersAndVisibility, TypeParameter[] typeParameters,
 79    SymbolData returnType, VariableData[] params, String[] thrown, SymbolData enclosingClass,
 80    JExpression jexpr) {
 81  803848 super(enclosingClass);
 82  803848 _name = name;
 83  803848 _modifiersAndVisibility = modifiersAndVisibility;
 84  803848 _typeParameters = typeParameters;
 85  803848 _returnType = returnType;
 86  803848 _params = params;
 87  803848 _thrown = thrown;
 88  803848 _jexpr = jexpr;
 89  803848 _generated = false;
 90    }
 91   
 92    /** Constructor used by the LanguageLevelConverter, where only the name and params matter*/
 93  14 public MethodData(String name, VariableData[] params) {
 94  14 this(name, new ModifiersAndVisibility(SourceInfo.NONE, new String[0]), new TypeParameter[0], null,
 95    params, new String[0], null, new NullLiteral(SourceInfo.NONE));
 96    }
 97   
 98    /** Factory methods used for debugging purposes among other things. */
 99  802958 public static MethodData make(String name, ModifiersAndVisibility modifiersAndVisibility, TypeParameter[] typeParameters,
 100    SymbolData returnType, VariableData[] params, String[] thrown, SymbolData enclosingClass,
 101    JExpression jexpr) {
 102   
 103  802958 MethodData md =
 104    new MethodData(name, modifiersAndVisibility, typeParameters, returnType, params, thrown, enclosingClass, jexpr);
 105    // _log.log("Allocated: " + md + '\n' + "With modifiers: " + md.getMav());
 106  802958 return md;
 107    }
 108   
 109  0 public static MethodData make(String name, VariableData[] params) { return new MethodData(name, params); }
 110   
 111    /**@return true if this method was generated during the LanguageLevel conversion process*/
 112  398 public boolean isGenerated() { return _generated; }
 113   
 114    /** Returns true if this MethodData is static. */
 115  0 public boolean isStatic() { return hasModifier("static"); }
 116   
 117    /**@param generated true or false--whether to set this method to generated or not*/
 118  591 public void setGenerated(boolean generated) { _generated = generated; }
 119   
 120    /** Two MethodDatas are equal if ... */
 121  5359 public boolean equals(Object obj) {
 122  8 if (obj == this) return true;
 123  1 if (obj == null) return false;
 124  10 if ((obj.getClass() != this.getClass())) return false;
 125  5340 MethodData md = (MethodData) obj;
 126   
 127  5340 return _name.equals(md.getName()) &&
 128    _modifiersAndVisibility.equals(md.getMav()) &&
 129    // Type parameters are not used in functional code, but names are irrelevant anyway
 130    // LanguageLevelVisitor.arrayEquals(_typeParameters, md.getTypeParameters()) &&
 131    LanguageLevelVisitor.arrayEquals(_params, md.getParams()) && // TODO: this comparison should be less stringnet; names don't matter
 132    LanguageLevelVisitor.arrayEquals(_thrown, md.getThrown()) &&
 133    _enclosingData.get(0) == md.getEnclosingData().get(0) &&
 134    _vars.equals(md.getVars());
 135    }
 136   
 137    /** Define a hashCode() method that hashes on the method name and its enclosing data. */
 138  0 public int hashCode() { return getName().hashCode() ^ getEnclosingData().hashCode(); }
 139   
 140  0 public boolean isMethodData() { return true; }
 141   
 142  201 public MethodData getMethodData() { return this; }
 143   
 144    /**@return the type parameters*/
 145  2 public TypeParameter[] getTypeParameters() { return _typeParameters; }
 146   
 147    /**@return the return type*/
 148  918 public SymbolData getReturnType() { return _returnType; }
 149   
 150    /**set the return type to be rt*/
 151  14 public void setReturnType(SymbolData rt) { _returnType = rt; }
 152   
 153    /**@return the method params*/
 154  3823 public VariableData[] getParams() { return _params; }
 155   
 156    /**Set the method params to be p*/
 157  324 public void setParams(VariableData[] p) { _params = p; }
 158   
 159    /**@return the thrown array for this method*/
 160  494 public String[] getThrown() { return _thrown; }
 161   
 162    /** Sets thrown to be thrown*/
 163  22 public void setThrown(String[] thrown) { _thrown = thrown; }
 164   
 165    /** @return the modifiers and visibility*/
 166  2107 public ModifiersAndVisibility getMav() { return _modifiersAndVisibility; }
 167   
 168    /** Makes this method public. Only used in ClassBodyElementaryVisitor. */
 169  0 public void addPublicMav() {
 170  0 String[] oldMav = _modifiersAndVisibility.getModifiers();
 171  0 String[] modifiers = new String[oldMav.length + 1];
 172  0 modifiers[0] = "public";
 173  0 for (int i = 0; i < oldMav.length; i++) { modifiers[i+1] = oldMav[i];
 174    }
 175  0 _modifiersAndVisibility = new ModifiersAndVisibility(_modifiersAndVisibility.getSourceInfo(), modifiers);
 176    }
 177   
 178   
 179    /** @return the JExpression corresponding to this method*/
 180  9 public JExpression getJExpression() { return _jexpr; }
 181   
 182  0 public String toString() { return "method: " + _name; }
 183   
 184  0 public String toBigString() {
 185  0 return "method " + _name + "<" + _modifiersAndVisibility + ", " +
 186    Arrays.toString(_typeParameters) + ", " + _returnType + ", " + Arrays.toString(_params) + ", " +
 187    Arrays.toString(_thrown) + ", " + _jexpr + ", " + _generated + ">" ; }
 188   
 189    /** Tests the methods declared above*/
 190    public static class MethodDataTest extends TestCase {
 191   
 192    private MethodData _md;
 193    private MethodData _md2;
 194   
 195    private ModifiersAndVisibility _publicMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public"});
 196    private ModifiersAndVisibility _publicMav2 = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public"});
 197    private ModifiersAndVisibility _protectedMav =
 198    new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"protected"});
 199    private ModifiersAndVisibility _finalMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"final"});
 200   
 201  0 public MethodDataTest() { this(""); }
 202   
 203  1 public MethodDataTest(String name) { super(name); }
 204   
 205  1 public void testEquals() {
 206  1 VariableData vd = new VariableData("v", _publicMav, SymbolData.INT_TYPE, true, _md);
 207  1 VariableData vd2 = new VariableData("v2", _protectedMav, SymbolData.BOOLEAN_TYPE, true, _md);
 208  1 TypeParameter[] tp = new TypeParameter[0];
 209   
 210  1 Type t = new PrimitiveType(SourceInfo.NONE, "int");
 211  1 Word name = new Word(SourceInfo.NONE, "m");
 212  1 Word paramName = new Word(SourceInfo.NONE, "i");
 213  1 FormalParameter fp =
 214    new FormalParameter(SourceInfo.NONE, new UninitializedVariableDeclarator(SourceInfo.NONE, t, paramName),
 215    false);
 216  1 MethodDef mdef =
 217    new AbstractMethodDef(SourceInfo.NONE, _publicMav, tp, t, name, new FormalParameter[] {fp},
 218    new ReferenceType[0]);
 219  1 _md = new MethodData("m", _publicMav, tp, SymbolData.INT_TYPE, new VariableData[] {vd},
 220    new String[] {"I throw this"}, SymbolData.BOOLEAN_TYPE, mdef);
 221   
 222    //Two method datas that should be equal
 223  1 _md2 = new MethodData("m", _publicMav2, tp, SymbolData.INT_TYPE, new VariableData[]{vd},
 224    new String[] {"I throw this"}, SymbolData.BOOLEAN_TYPE, mdef);
 225  1 assertTrue("Two MethodDatas with same fields should be equal", _md.equals(_md2));
 226   
 227    //different return types okay
 228  1 _md2 = new MethodData("m", _publicMav2, tp, SymbolData.DOUBLE_TYPE, new VariableData[]{vd},
 229    new String[] {"I throw this"}, SymbolData.BOOLEAN_TYPE, mdef);
 230  1 assertTrue("Two MethodDatas with same fields but different return types should be equal", _md.equals(_md2));
 231   
 232    //different method defs okay
 233  1 _md2 = new MethodData("m", _publicMav2, tp, SymbolData.INT_TYPE, new VariableData[]{vd},
 234    new String[] {"I throw this"}, SymbolData.BOOLEAN_TYPE, null);
 235  1 assertTrue("Two MethodDatas with same fields but different method defs should be equal", _md.equals(_md2));
 236   
 237    //compared to null
 238  1 _md2 = null;
 239  1 assertFalse("A MethodData is never equal to null", _md.equals(_md2));
 240   
 241    //compared to different class
 242  1 assertFalse("A MethodData is never equal to another class", _md.equals(new Integer(5)));
 243   
 244    //different names
 245  1 _md2 = new MethodData("q", _publicMav2, tp, SymbolData.INT_TYPE, new VariableData[]{vd},
 246    new String[] {"I throw this"}, SymbolData.BOOLEAN_TYPE, mdef);
 247  1 assertFalse("Two MethodDatas with different names are not equal", _md.equals(_md2));
 248   
 249    //different modifiers and visibility
 250  1 _md2 = new MethodData("m", _finalMav, tp, SymbolData.INT_TYPE, new VariableData[]{vd},
 251    new String[] {"I throw this"}, SymbolData.BOOLEAN_TYPE, mdef);
 252  1 assertFalse("Two MethodDatas with different MAVs are not equal", _md.equals(_md2));
 253   
 254    /* The different type parameters test is quoted out because the particular names do not matter. */
 255    // Different type parameters
 256    // TypeParameter[] tp2 =
 257    // new TypeParameter[] { new TypeParameter(SourceInfo.NONE, new TypeVariable(SourceInfo.NONE,"tv"),
 258    // new TypeVariable(SourceInfo.NONE,"i"))};
 259    //
 260    // _md2 = new MethodData("m", _publicMav2, tp2, SymbolData.INT_TYPE, new VariableData[]{vd},
 261    // new String[] {"I throw this"}, SymbolData.BOOLEAN_TYPE, mdef);
 262    // assertFalse("Two MethodDatas with different type parameters are not equal", _md.equals(_md2));
 263   
 264    //different thrown
 265  1 _md2 = new MethodData("m", _publicMav2, tp, SymbolData.INT_TYPE, new VariableData[]{vd},
 266    new String[] {"I throw this", "maybe this too"}, SymbolData.BOOLEAN_TYPE, mdef);
 267  1 assertFalse("Two MethodDatas with different thrown arrays are not equal", _md.equals(_md2));
 268   
 269    //different enclosing datas
 270  1 _md2 = new MethodData("m", _publicMav2, tp, SymbolData.INT_TYPE, new VariableData[]{vd},
 271    new String[] {"I throw this"}, SymbolData.NULL_TYPE, mdef);
 272  1 assertFalse("Two MethodDatas with different enclosing datas are not equal", _md.equals(_md2));
 273   
 274    //different parameters
 275  1 _md2 = new MethodData("m", _publicMav2, tp, SymbolData.INT_TYPE, new VariableData[]{vd, vd2},
 276    new String[] {"I throw this"}, SymbolData.NULL_TYPE, mdef);
 277  1 assertFalse("Two MethodDatas with different variables are not equal", _md.equals(_md2));
 278   
 279    //same parameters, but in different order
 280  1 _md = new MethodData("m", _publicMav2, tp, SymbolData.INT_TYPE, new VariableData[]{vd2, vd},
 281    new String[] {"I throw this"}, SymbolData.NULL_TYPE, mdef);
 282  1 assertFalse("Two MethodDatas with same parameters in different order are not equal", _md.equals(_md2));
 283   
 284    }
 285    }
 286    }