1 /* 2 * Copyright (c) 2003, 2015, 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 * @modules jdk.jdi 32 * @run build TestScaffold VMConnection TargetListener TargetAdapter 33 * @run compile -g LocalVariableEqual.java 34 * @run driver LocalVariableEqual 35 */ 36 import com.sun.jdi.*; 37 import com.sun.jdi.event.*; 38 import com.sun.jdi.request.*; 39 import java.util.*; 40 41 /********** target program **********/ 42 43 class LocalVariableEqualTarg { 44 public static void main(String[] args){ 45 int intVar = 10; 46 System.out.println("LocalVariableEqualTarg: Started"); 47 intVar = staticMeth(intVar); 48 System.out.println("LocalVariableEqualTarg: Finished"); 49 } 50 51 public static int staticMeth(int intArg) { 52 System.out.println("staticMeth: Started"); 53 int result; 54 { 55 { boolean bool_1 = false; 56 intArg++; 57 { 58 { byte byte_2 = 2; 59 intArg++; 60 } 61 byte byte_1 = 1; 62 intArg++; 63 } 64 } 65 boolean bool_2 = true; 66 intArg++; 67 } 68 { 69 { 70 { 71 { char char_1 = '1'; 72 intArg++; 73 } 74 short short_1 = 1; 75 intArg++; 76 } 77 } 78 { short short_2 = 2; 79 intArg++; 80 } 81 char char_2 = '2'; 82 intArg++; 83 } 84 { 85 { int int_1 = 1; 86 intArg++; 87 } 88 long long_1 = 1; 89 intArg++; 90 } 91 { 92 { float float_1 = 1; 93 intArg++; 94 } 95 double double_2 = 2; 96 intArg++; 97 } 98 { 99 { String string_1 = "1"; 100 intArg++; 101 } 102 Object obj_2 = new Object(); 103 intArg++; 104 } 105 result = 10; 106 System.out.println("staticMeth: Finished"); 107 return result; 108 } 109 } 110 111 112 /********** test program **********/ 113 114 public class LocalVariableEqual extends TestScaffold { 115 ReferenceType targetClass; 116 ThreadReference mainThread; 117 118 LocalVariableEqual (String args[]) { 119 super(args); 120 } 121 122 public static void main(String[] args) throws Exception { 123 new LocalVariableEqual(args).startTests(); 124 } 125 126 /********** test assist **********/ 127 128 Method getMethod(String className, String methodName) { 129 List refs = vm().classesByName(className); 130 if (refs.size() != 1) { 131 failure("Test failure: " + refs.size() + 132 " ReferenceTypes named: " + className); 133 return null; 134 } 135 ReferenceType refType = (ReferenceType)refs.get(0); 136 List meths = refType.methodsByName(methodName); 137 if (meths.size() != 1) { 138 failure("Test failure: " + meths.size() + 139 " methods named: " + methodName); 140 return null; 141 } 142 return (Method)meths.get(0); 143 } 144 145 void printVariable(LocalVariable lv, int index) throws Exception { 146 if (lv == null) { 147 println(" Var name: null"); 148 return; 149 } 150 String tyname = lv.typeName(); 151 println(" Var: " + lv.name() + ", index: " + index + ", type: " + tyname + 152 ", Signature: " + lv.type().signature()); 153 // Sorry, there is no way to take local variable slot numbers using JDI! 154 // It is because method LocalVariableImpl.slot() is private. 155 } 156 157 void compareTwoEqualVars(LocalVariable lv1, LocalVariable lv2) { 158 if (lv1.equals(lv2)) { 159 println(" Success: equality of local vars detected"); 160 } else { 161 failure(" Failure: equality of local vars is NOT detected"); 162 } 163 if (lv1.hashCode() == lv2.hashCode()) { 164 println(" Success: hashCode's of equal local vars are equal"); 165 } else { 166 failure(" Failure: hashCode's of equal local vars differ"); 167 } 168 if (lv1.compareTo(lv2) == 0) { 169 println(" Success: compareTo() is correct for equal local vars"); 170 } else { 171 failure(" Failure: compareTo() is NOT correct for equal local vars"); 172 } 173 } 174 175 void compareTwoDifferentVars(LocalVariable lv1, LocalVariable lv2) { 176 if (!lv1.equals(lv2)) { 177 println(" Success: difference of local vars detected"); 178 } else { 179 failure(" Failure: difference of local vars is NOT detected"); 180 } 181 if (lv1.hashCode() != lv2.hashCode()) { 182 println(" Success: hashCode's of different local vars differ"); 183 } else { 184 failure(" Failure: hashCode's of different local vars are equal"); 185 } 186 if (lv1.compareTo(lv2) != 0) { 187 println(" Success: compareTo() is correct for different local vars"); 188 } else { 189 failure(" Failure: compareTo() is NOT correct for different local vars"); 190 } 191 } 192 193 void compareAllVariables(String className, String methodName) throws Exception { 194 println("compareAllVariables for method: " + className + "." + methodName); 195 Method method = getMethod(className, methodName); 196 List localVars; 197 try { 198 localVars = method.variables(); 199 println("\n Success: got a list of all method variables: " + methodName); 200 } 201 catch (com.sun.jdi.AbsentInformationException ex) { 202 failure("\n AbsentInformationException has been thrown"); 203 return; 204 } 205 206 // We consider N*N combinations for set of N variables 207 int index1 = 0; 208 for (Iterator it1 = localVars.iterator(); it1.hasNext(); index1++) { 209 LocalVariable lv1 = (LocalVariable) it1.next(); 210 211 int index2 = 0; 212 for (Iterator it2 = localVars.iterator(); it2.hasNext(); index2++) { 213 LocalVariable lv2 = (LocalVariable) it2.next(); 214 215 println("\n Two variables:"); 216 printVariable(lv1, index1); 217 printVariable(lv2, index2); 218 println(""); 219 if (index1 == index2) { 220 compareTwoEqualVars(lv1, lv2); 221 } else { 222 compareTwoDifferentVars(lv1, lv2); 223 } 224 } 225 } 226 println(""); 227 return; 228 } 229 230 /********** test core **********/ 231 232 protected void runTests() throws Exception { 233 234 /* 235 * Get to the top of main() to determine targetClass and mainThread 236 */ 237 BreakpointEvent bpe = startToMain("LocalVariableEqualTarg"); 238 println("startToMain(LocalVariableEqualTarg)"); 239 240 compareAllVariables("LocalVariableEqualTarg", "staticMeth"); 241 242 /* 243 * resume until the end 244 */ 245 listenUntilVMDisconnect(); 246 247 /* 248 * deal with results of test 249 * if anything has called failure("foo") testFailed will be true 250 */ 251 if (!testFailed) { 252 println("\nLocalVariableEqual: passed"); 253 } else { 254 throw new Exception("\nLocalVariableEqual: FAILED"); 255 } 256 } 257 }