Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jdi/FinalLocalsTest.java
38855 views
/*1* Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4326648 476832926* @summary Test to verify that table entries are generated for all final27* locals when a class is built for debug, even if they could be28* inlined otherwise.29*30* @author Tim Bell31*32* @run build TestScaffold VMConnection TargetListener TargetAdapter33* @run compile -g FinalLocalsTest.java34* @run main FinalLocalsTest35*/36import com.sun.jdi.*;37import com.sun.jdi.event.*;38import com.sun.jdi.request.*;3940import java.util.*;4142/********** target program **********/4344class FinalLocalsTarg {45public void test1 (final int t, int k){46String s1 = "first";47final int z = 0;48if (true) {49final float r = 10.00f;50boolean b = true;51System.out.println(r);52}53}54public void hi(){55return;56}57public static void main(String[] args) {58System.out.print("in FinalLocalsTarg:");59new FinalLocalsTarg().hi();60return;61}62}6364/********** test program **********/6566public class FinalLocalsTest extends TestScaffold {67ReferenceType targetClass;68ThreadReference mainThread;6970FinalLocalsTest (String args[]) {71super(args);72}7374public static void main(String[] args) throws Exception {75new FinalLocalsTest(args).startTests();76}7778/********** test core **********/79static final int VARIABLES = 1;80static final int BYNAME = 2;81static final int ARGUMENTS = 3;82/*83* Take a String containing comma separated values84* and return those values in a TreeSet.85*/86private TreeSet buildSet(String in) {87TreeSet result = new TreeSet();88StringTokenizer tt = new StringTokenizer(in, ",");89while (tt.hasMoreTokens()) {90String ss = tt.nextToken();91if (! result.add(ss)) {92failure ("Duplicate entry \"" + ss + "\" in string: " +93in + " is not allowed");94}95}96return result;97}9899private void test(Method method, int which, String name, String expected) {100String got = testCase(method, which);101System.out.println(" test() comparing expected = " + expected +102" to got = " + got);103TreeSet expectedSet = buildSet(expected);104TreeSet gotSet = buildSet(got);105106while (! expectedSet.isEmpty()) {107String ee = (String)expectedSet.first();108expectedSet.remove(ee);109if (gotSet.contains(ee)) {110gotSet.remove(ee);111} else {112failure (name + " Expected entry \"" + ee + "\" not found");113}114}115116//assert expectedSet.isEmpty() : name + " expected set should have been emptied";117118if (! gotSet.isEmpty()) {119StringBuffer sb = new StringBuffer();120Iterator it = gotSet.iterator();121while (it.hasNext()) {122sb.append(it.next());123if (it.hasNext()) {124sb.append(",");125}126}127failure (name + " Unexpected entries found: " + sb.toString());128}129}130131String testCase(Method method, int which) {132try {133List vars;134switch (which) {135case VARIABLES:136vars = method.variables();137break;138case BYNAME:139vars = method.variablesByName("s1");140break;141case ARGUMENTS:142vars = method.arguments();143break;144default:145throw new InternalException("should not happen");146}147StringBuffer sb = new StringBuffer();148for (Iterator it = vars.iterator(); it.hasNext(); ) {149LocalVariable lv = (LocalVariable)it.next();150if (sb.length() > 0) {151sb.append(",");152}153sb.append(lv.name());154}155return sb.toString();156} catch (Exception exc) {157String st = exc.getClass().getName();158int inx = st.lastIndexOf('.');159return st.substring(inx+1);160}161}162163protected void runTests() throws Exception {164/*165* Get to the top of main()166* to determine targetClass and mainThread167*/168BreakpointEvent bpe = startToMain("FinalLocalsTarg");169targetClass = bpe.location().declaringType();170mainThread = bpe.thread();171EventRequestManager erm = vm().eventRequestManager();172173/*174* Get to a point where the classes are loaded.175*/176BreakpointEvent bp = resumeTo("FinalLocalsTarg", "hi", "()V");177178ReferenceType rt = findReferenceType("FinalLocalsTarg");179if (rt == null) {180throw new Exception("FinalLocalsTarg: not loaded");181}182183/*184* Inspect the LocalVariableTable attributes for method "test1"185* NOTE: .class files compiled with some versions of javac will186* give results listed in different order. That's OK.187*/188Method method = findMethod(rt, "test1", "(II)V");189if (method == null) {190throw new Exception("Method not found");191}192test(method, VARIABLES, "VARIABLES",193"t,k,s1,z,r,b");194test(method, BYNAME, "BYNAME",195"s1");196test(method, ARGUMENTS, "ARGUMENTS",197"t,k");198199/*200* All Done. Resume the target listening for events201*/202listenUntilVMDisconnect();203204/*205* deal with results of test206* if anything has called failure("foo") testFailed will be true207*/208if (!testFailed) {209println("FinalLocalsTest: passed");210} else {211throw new Exception("FinalLocalsTest: failed");212}213}214}215216217