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