1 /* 2 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 4916263 27 * @summary Test 28 * 29 * @author Serguei Spitsyn 30 * 31 * @run build TestScaffold VMConnection TargetListener TargetAdapter 32 * @run compile -g LocalVariableEqual.java 33 * @run driver LocalVariableEqual 34 */ 35 import com.sun.jdi.*; 36 import com.sun.jdi.event.*; 37 import com.sun.jdi.request.*; 38 import java.util.*; 39 40 /********** target program **********/ 41 42 class LocalVariableEqualTarg { 43 public static void main(String[] args){ 44 int intVar = 10; 45 System.out.println("LocalVariableEqualTarg: Started"); 46 intVar = staticMeth(intVar); 47 System.out.println("LocalVariableEqualTarg: Finished"); 48 } 49 50 public static int staticMeth(int intArg) { 51 System.out.println("staticMeth: Started"); 52 int result; 53 { 54 { boolean bool_1 = false; 55 intArg++; 56 { 57 { byte byte_2 = 2; 58 intArg++; 59 } 60 byte byte_1 = 1; 61 intArg++; 62 } 63 } 64 boolean bool_2 = true; 65 intArg++; 66 } 67 { 68 { 69 { 70 { char char_1 = '1'; 71 intArg++; 72 } 73 short short_1 = 1; 74 intArg++; 75 } 76 } 77 { short short_2 = 2; 78 intArg++; 79 } 80 char char_2 = '2'; 81 intArg++; 82 } 83 { 84 { int int_1 = 1; 85 intArg++; 86 } 87 long long_1 = 1; 88 intArg++; 89 } 90 { 91 { float float_1 = 1; 92 intArg++; 93 } 94 double double_2 = 2; 95 intArg++; 96 } 97 { 98 { String string_1 = "1"; 99 intArg++; 100 } 101 Object obj_2 = new Object(); 102 intArg++; 103 } 104 result = 10; 105 System.out.println("staticMeth: Finished"); 106 return result; 107 } 108 } 109 110 111 /********** test program **********/ 112 113 public class LocalVariableEqual extends TestScaffold { 114 ReferenceType targetClass; 115 ThreadReference mainThread; 116 117 LocalVariableEqual (String args[]) { 118 super(args); 119 } 120 121 public static void main(String[] args) throws Exception { 122 new LocalVariableEqual(args).startTests(); 123 } 124 125 /********** test assist **********/ 126 127 Method getMethod(String className, String methodName) { 128 List refs = vm().classesByName(className); 129 if (refs.size() != 1) { 130 failure("Test failure: " + refs.size() + 131 " ReferenceTypes named: " + className); 132 return null; 133 } 134 ReferenceType refType = (ReferenceType)refs.get(0); 135 List meths = refType.methodsByName(methodName); 136 if (meths.size() != 1) { 137 failure("Test failure: " + meths.size() + 138 " methods named: " + methodName); 139 return null; 140 } 141 return (Method)meths.get(0); 142 } 143 144 void printVariable(LocalVariable lv, int index) throws Exception { 145 if (lv == null) { 146 println(" Var name: null"); 147 return; 148 } 149 String tyname = lv.typeName(); 150 println(" Var: " + lv.name() + ", index: " + index + ", type: " + tyname + 151 ", Signature: " + lv.type().signature()); 152 // Sorry, there is no way to take local variable slot numbers using JDI! 153 // It is because method LocalVariableImpl.slot() is private. 154 } 155 156 void compareTwoEqualVars(LocalVariable lv1, LocalVariable lv2) { 157 if (lv1.equals(lv2)) { 158 println(" Success: equality of local vars detected"); 159 } else { 160 failure(" Failure: equality of local vars is NOT detected"); 161 } 162 if (lv1.hashCode() == lv2.hashCode()) { 163 println(" Success: hashCode's of equal local vars are equal"); 164 } else { 165 failure(" Failure: hashCode's of equal local vars differ"); 166 } 167 if (lv1.compareTo(lv2) == 0) { 168 println(" Success: compareTo() is correct for equal local vars"); 169 } else { 170 failure(" Failure: compareTo() is NOT correct for equal local vars"); 171 } 172 } 173 174 void compareTwoDifferentVars(LocalVariable lv1, LocalVariable lv2) { 175 if (!lv1.equals(lv2)) { 176 println(" Success: difference of local vars detected"); 177 } else { 178 failure(" Failure: difference of local vars is NOT detected"); 179 } 180 if (lv1.hashCode() != lv2.hashCode()) { 181 println(" Success: hashCode's of different local vars differ"); 182 } else { 183 failure(" Failure: hashCode's of different local vars are equal"); 184 } 185 if (lv1.compareTo(lv2) != 0) { 186 println(" Success: compareTo() is correct for different local vars"); 187 } else { 188 failure(" Failure: compareTo() is NOT correct for different local vars"); 189 } 190 } 191 192 void compareAllVariables(String className, String methodName) throws Exception { 193 println("compareAllVariables for method: " + className + "." + methodName); 194 Method method = getMethod(className, methodName); 195 List localVars; 196 try { 197 localVars = method.variables(); 198 println("\n Success: got a list of all method variables: " + methodName); 199 } 200 catch (com.sun.jdi.AbsentInformationException ex) { 201 failure("\n AbsentInformationException has been thrown"); 202 return; 203 } 204 205 // We consider N*N combinations for set of N variables 206 int index1 = 0; 207 for (Iterator it1 = localVars.iterator(); it1.hasNext(); index1++) { 208 LocalVariable lv1 = (LocalVariable) it1.next(); 209 210 int index2 = 0; 211 for (Iterator it2 = localVars.iterator(); it2.hasNext(); index2++) { 212 LocalVariable lv2 = (LocalVariable) it2.next(); 213 214 println("\n Two variables:"); 215 printVariable(lv1, index1); 216 printVariable(lv2, index2); 217 println(""); 218 if (index1 == index2) { 219 compareTwoEqualVars(lv1, lv2); 220 } else { 221 compareTwoDifferentVars(lv1, lv2); 222 } 223 } 224 } 225 println(""); 226 return; 227 } 228 229 /********** test core **********/ 230 231 protected void runTests() throws Exception { 232 233 /* 234 * Get to the top of main() to determine targetClass and mainThread 235 */ 236 BreakpointEvent bpe = startToMain("LocalVariableEqualTarg"); 237 println("startToMain(LocalVariableEqualTarg)"); 238 239 compareAllVariables("LocalVariableEqualTarg", "staticMeth"); 240 241 /* 242 * resume until the end 243 */ 244 listenUntilVMDisconnect(); 245 246 /* 247 * deal with results of test 248 * if anything has called failure("foo") testFailed will be true 249 */ 250 if (!testFailed) { 251 println("\nLocalVariableEqual: passed"); 252 } else { 253 throw new Exception("\nLocalVariableEqual: FAILED"); 254 } 255 } 256 }