jdk/test/com/sun/jdi/FinalLocalsTest.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/com/sun/jdi/FinalLocalsTest.java	Sat Dec 01 00:00:00 2007 +0000
@@ -0,0 +1,215 @@
+/*
+ * Copyright 2000-2002 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/**
+ *  @test
+ *  @bug 4326648 4768329
+ *  @summary Test to verify that table entries are generated for all final
+ *           locals when a class is built for debug, even if they could be
+ *           inlined otherwise.
+ *
+ *  @author Tim Bell
+ *
+ *  @run build TestScaffold VMConnection TargetListener TargetAdapter
+ *  @run compile -g FinalLocalsTest.java
+ *  @run main FinalLocalsTest
+ */
+import com.sun.jdi.*;
+import com.sun.jdi.event.*;
+import com.sun.jdi.request.*;
+
+import java.util.*;
+
+    /********** target program **********/
+
+class FinalLocalsTarg {
+    public void test1 (final int t, int k){
+        String s1 = "first";
+        final int z = 0;
+        if (true) {
+            final float r = 10.00f;
+            boolean b = true;
+            System.out.println(r);
+        }
+    }
+    public void hi(){
+        return;
+    }
+    public static void main(String[] args) {
+        System.out.print("in FinalLocalsTarg:");
+        new FinalLocalsTarg().hi();
+        return;
+    }
+}
+
+    /********** test program **********/
+
+public class FinalLocalsTest extends TestScaffold {
+    ReferenceType targetClass;
+    ThreadReference mainThread;
+
+    FinalLocalsTest (String args[]) {
+        super(args);
+    }
+
+    public static void main(String[] args)      throws Exception {
+        new FinalLocalsTest(args).startTests();
+    }
+
+    /********** test core **********/
+    static final int VARIABLES = 1;
+    static final int BYNAME = 2;
+    static final int ARGUMENTS = 3;
+    /*
+     * Take a String containing comma separated values
+     * and return those values in a TreeSet.
+     */
+    private TreeSet buildSet(String in) {
+        TreeSet result = new TreeSet();
+        StringTokenizer tt = new StringTokenizer(in, ",");
+        while (tt.hasMoreTokens()) {
+            String ss = tt.nextToken();
+            if (! result.add(ss)) {
+                failure ("Duplicate entry \"" + ss + "\" in string: " +
+                         in + " is not allowed");
+            }
+        }
+        return result;
+    }
+
+    private void test(Method method, int which, String name, String expected) {
+        String got = testCase(method, which);
+        System.out.println(" test() comparing expected = " + expected +
+                           " to got = " + got);
+        TreeSet expectedSet = buildSet(expected);
+        TreeSet gotSet = buildSet(got);
+
+        while (! expectedSet.isEmpty()) {
+            String ee = (String)expectedSet.first();
+            expectedSet.remove(ee);
+            if (gotSet.contains(ee)) {
+                gotSet.remove(ee);
+            } else {
+                failure (name + " Expected entry \"" + ee + "\" not found");
+            }
+        }
+
+        //assert expectedSet.isEmpty() : name + " expected set should have been emptied";
+
+        if (! gotSet.isEmpty()) {
+            StringBuffer sb = new StringBuffer();
+            Iterator it = gotSet.iterator();
+            while (it.hasNext()) {
+                sb.append(it.next());
+                if (it.hasNext()) {
+                    sb.append(",");
+                }
+            }
+            failure (name + " Unexpected entries found: " + sb.toString());
+        }
+    }
+
+    String testCase(Method method, int which) {
+        try {
+            List vars;
+            switch (which) {
+                case VARIABLES:
+                    vars = method.variables();
+                    break;
+                case BYNAME:
+                    vars = method.variablesByName("s1");
+                    break;
+                case ARGUMENTS:
+                    vars = method.arguments();
+                    break;
+                default:
+                    throw new InternalException("should not happen");
+            }
+            StringBuffer sb = new StringBuffer();
+            for (Iterator it = vars.iterator(); it.hasNext(); ) {
+                LocalVariable lv = (LocalVariable)it.next();
+                if (sb.length() > 0) {
+                    sb.append(",");
+                }
+                sb.append(lv.name());
+            }
+            return sb.toString();
+        } catch (Exception exc) {
+            String st = exc.getClass().getName();
+            int inx = st.lastIndexOf('.');
+            return st.substring(inx+1);
+        }
+    }
+
+    protected void runTests() throws Exception {
+        /*
+         * Get to the top of main()
+         * to determine targetClass and mainThread
+         */
+        BreakpointEvent bpe = startToMain("FinalLocalsTarg");
+        targetClass = bpe.location().declaringType();
+        mainThread = bpe.thread();
+        EventRequestManager erm = vm().eventRequestManager();
+
+        /*
+         * Get to a point where the classes are loaded.
+         */
+        BreakpointEvent bp = resumeTo("FinalLocalsTarg", "hi", "()V");
+
+        ReferenceType rt = findReferenceType("FinalLocalsTarg");
+        if (rt == null) {
+            throw new Exception("FinalLocalsTarg: not loaded");
+        }
+
+        /*
+         * Inspect the LocalVariableTable attributes for method "test1"
+         * NOTE: .class files compiled with some versions of javac will
+         * give results listed in different order.  That's OK.
+         */
+        Method method = findMethod(rt, "test1", "(II)V");
+        if (method == null) {
+            throw new Exception("Method not found");
+        }
+        test(method, VARIABLES, "VARIABLES",
+             "t,k,s1,z,r,b");
+        test(method, BYNAME, "BYNAME",
+             "s1");
+        test(method, ARGUMENTS, "ARGUMENTS",
+             "t,k");
+
+        /*
+         * All Done.  Resume the target listening for events
+         */
+        listenUntilVMDisconnect();
+
+        /*
+         * deal with results of test
+         * if anything has called failure("foo") testFailed will be true
+         */
+        if (!testFailed) {
+            println("FinalLocalsTest: passed");
+        } else {
+            throw new Exception("FinalLocalsTest: failed");
+        }
+    }
+}