FinalLocalsTest.java revision 10093:056cd206a147
1101242Srwatson/* 2101242Srwatson * Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved. 3101242Srwatson * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4101242Srwatson * 5101242Srwatson * This code is free software; you can redistribute it and/or modify it 6101242Srwatson * under the terms of the GNU General Public License version 2 only, as 7101242Srwatson * published by the Free Software Foundation. 8101242Srwatson * 9101242Srwatson * This code is distributed in the hope that it will be useful, but WITHOUT 10101242Srwatson * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11101242Srwatson * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12101242Srwatson * version 2 for more details (a copy is included in the LICENSE file that 13101242Srwatson * accompanied this code). 14101242Srwatson * 15101242Srwatson * You should have received a copy of the GNU General Public License version 16101242Srwatson * 2 along with this work; if not, write to the Free Software Foundation, 17101242Srwatson * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18101242Srwatson * 19101242Srwatson * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20101242Srwatson * or visit www.oracle.com if you need additional information or have any 21101242Srwatson * questions. 22101242Srwatson */ 23101242Srwatson 24101242Srwatson/** 25101242Srwatson * @test 26101242Srwatson * @bug 4326648 4768329 27101242Srwatson * @summary Test to verify that table entries are generated for all final 28101242Srwatson * locals when a class is built for debug, even if they could be 29101242Srwatson * inlined otherwise. 30101242Srwatson * 31101242Srwatson * @author Tim Bell 32176901Srwatson * 33176901Srwatson * @run build TestScaffold VMConnection TargetListener TargetAdapter 34176901Srwatson * @run compile -g FinalLocalsTest.java 35101242Srwatson * @run driver FinalLocalsTest 36101242Srwatson */ 37122810Srwatsonimport com.sun.jdi.*; 38101242Srwatsonimport com.sun.jdi.event.*; 39111010Snectarimport com.sun.jdi.request.*; 40111010Snectar 41111010Snectarimport java.util.*; 42111010Snectar 43111010Snectar /********** target program **********/ 44111010Snectar 45105698Srwatsonclass FinalLocalsTarg { 46105698Srwatson public void test1 (final int t, int k){ 47101242Srwatson String s1 = "first"; 48101242Srwatson final int z = 0; 49105698Srwatson if (true) { 50105698Srwatson final float r = 10.00f; 51101242Srwatson boolean b = true; 52105698Srwatson System.out.println(r); 53105698Srwatson } 54105698Srwatson } 55101242Srwatson public void hi(){ 56105698Srwatson return; 57101242Srwatson } 58101242Srwatson public static void main(String[] args) { 59105698Srwatson System.out.print("in FinalLocalsTarg:"); 60105698Srwatson new FinalLocalsTarg().hi(); 61101242Srwatson return; 62101242Srwatson } 63105698Srwatson} 64105698Srwatson 65101242Srwatson /********** test program **********/ 66105698Srwatson 67122810Srwatsonpublic class FinalLocalsTest extends TestScaffold { 68122810Srwatson ReferenceType targetClass; 69122810Srwatson ThreadReference mainThread; 70122810Srwatson 71122810Srwatson FinalLocalsTest (String args[]) { 72122810Srwatson super(args); 73122810Srwatson } 74140664Srwatson 75122810Srwatson public static void main(String[] args) throws Exception { 76105698Srwatson new FinalLocalsTest(args).startTests(); 77105698Srwatson } 78101242Srwatson 79105698Srwatson /********** test core **********/ 80101242Srwatson static final int VARIABLES = 1; 81101242Srwatson static final int BYNAME = 2; 82105698Srwatson static final int ARGUMENTS = 3; 83105698Srwatson /* 84101242Srwatson * Take a String containing comma separated values 85101242Srwatson * and return those values in a TreeSet. 86105698Srwatson */ 87101242Srwatson private TreeSet buildSet(String in) { 88 TreeSet result = new TreeSet(); 89 StringTokenizer tt = new StringTokenizer(in, ","); 90 while (tt.hasMoreTokens()) { 91 String ss = tt.nextToken(); 92 if (! result.add(ss)) { 93 failure ("Duplicate entry \"" + ss + "\" in string: " + 94 in + " is not allowed"); 95 } 96 } 97 return result; 98 } 99 100 private void test(Method method, int which, String name, String expected) { 101 String got = testCase(method, which); 102 System.out.println(" test() comparing expected = " + expected + 103 " to got = " + got); 104 TreeSet expectedSet = buildSet(expected); 105 TreeSet gotSet = buildSet(got); 106 107 while (! expectedSet.isEmpty()) { 108 String ee = (String)expectedSet.first(); 109 expectedSet.remove(ee); 110 if (gotSet.contains(ee)) { 111 gotSet.remove(ee); 112 } else { 113 failure (name + " Expected entry \"" + ee + "\" not found"); 114 } 115 } 116 117 //assert expectedSet.isEmpty() : name + " expected set should have been emptied"; 118 119 if (! gotSet.isEmpty()) { 120 StringBuffer sb = new StringBuffer(); 121 Iterator it = gotSet.iterator(); 122 while (it.hasNext()) { 123 sb.append(it.next()); 124 if (it.hasNext()) { 125 sb.append(","); 126 } 127 } 128 failure (name + " Unexpected entries found: " + sb.toString()); 129 } 130 } 131 132 String testCase(Method method, int which) { 133 try { 134 List vars; 135 switch (which) { 136 case VARIABLES: 137 vars = method.variables(); 138 break; 139 case BYNAME: 140 vars = method.variablesByName("s1"); 141 break; 142 case ARGUMENTS: 143 vars = method.arguments(); 144 break; 145 default: 146 throw new InternalException("should not happen"); 147 } 148 StringBuffer sb = new StringBuffer(); 149 for (Iterator it = vars.iterator(); it.hasNext(); ) { 150 LocalVariable lv = (LocalVariable)it.next(); 151 if (sb.length() > 0) { 152 sb.append(","); 153 } 154 sb.append(lv.name()); 155 } 156 return sb.toString(); 157 } catch (Exception exc) { 158 String st = exc.getClass().getName(); 159 int inx = st.lastIndexOf('.'); 160 return st.substring(inx+1); 161 } 162 } 163 164 protected void runTests() throws Exception { 165 /* 166 * Get to the top of main() 167 * to determine targetClass and mainThread 168 */ 169 BreakpointEvent bpe = startToMain("FinalLocalsTarg"); 170 targetClass = bpe.location().declaringType(); 171 mainThread = bpe.thread(); 172 EventRequestManager erm = vm().eventRequestManager(); 173 174 /* 175 * Get to a point where the classes are loaded. 176 */ 177 BreakpointEvent bp = resumeTo("FinalLocalsTarg", "hi", "()V"); 178 179 ReferenceType rt = findReferenceType("FinalLocalsTarg"); 180 if (rt == null) { 181 throw new Exception("FinalLocalsTarg: not loaded"); 182 } 183 184 /* 185 * Inspect the LocalVariableTable attributes for method "test1" 186 * NOTE: .class files compiled with some versions of javac will 187 * give results listed in different order. That's OK. 188 */ 189 Method method = findMethod(rt, "test1", "(II)V"); 190 if (method == null) { 191 throw new Exception("Method not found"); 192 } 193 test(method, VARIABLES, "VARIABLES", 194 "t,k,s1,z,r,b"); 195 test(method, BYNAME, "BYNAME", 196 "s1"); 197 test(method, ARGUMENTS, "ARGUMENTS", 198 "t,k"); 199 200 /* 201 * All Done. Resume the target listening for events 202 */ 203 listenUntilVMDisconnect(); 204 205 /* 206 * deal with results of test 207 * if anything has called failure("foo") testFailed will be true 208 */ 209 if (!testFailed) { 210 println("FinalLocalsTest: passed"); 211 } else { 212 throw new Exception("FinalLocalsTest: failed"); 213 } 214 } 215} 216