jdk/test/com/sun/jdi/FinalLocalsTest.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 24973 8c4bc3fa4c4e
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *  @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *  @bug 4326648 4768329
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @summary Test to verify that table entries are generated for all final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *           locals when a class is built for debug, even if they could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *           inlined otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  @author Tim Bell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *  @run compile -g FinalLocalsTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *  @run main FinalLocalsTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /********** target program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class FinalLocalsTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public void test1 (final int t, int k){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        String s1 = "first";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        final int z = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            final float r = 10.00f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            boolean b = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            System.out.println(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public void hi(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        System.out.print("in FinalLocalsTarg:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        new FinalLocalsTarg().hi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /********** test program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
public class FinalLocalsTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    ReferenceType targetClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    ThreadReference mainThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    FinalLocalsTest (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static void main(String[] args)      throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        new FinalLocalsTest(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /********** test core **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static final int VARIABLES = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static final int BYNAME = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static final int ARGUMENTS = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Take a String containing comma separated values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * and return those values in a TreeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private TreeSet buildSet(String in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        TreeSet result = new TreeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        StringTokenizer tt = new StringTokenizer(in, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        while (tt.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            String ss = tt.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (! result.add(ss)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                failure ("Duplicate entry \"" + ss + "\" in string: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                         in + " is not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private void test(Method method, int which, String name, String expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        String got = testCase(method, which);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        System.out.println(" test() comparing expected = " + expected +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                           " to got = " + got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        TreeSet expectedSet = buildSet(expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        TreeSet gotSet = buildSet(got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        while (! expectedSet.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            String ee = (String)expectedSet.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            expectedSet.remove(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (gotSet.contains(ee)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                gotSet.remove(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                failure (name + " Expected entry \"" + ee + "\" not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        //assert expectedSet.isEmpty() : name + " expected set should have been emptied";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (! gotSet.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            Iterator it = gotSet.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                sb.append(it.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                if (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    sb.append(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            failure (name + " Unexpected entries found: " + sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    String testCase(Method method, int which) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            List vars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            switch (which) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                case VARIABLES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    vars = method.variables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                case BYNAME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    vars = method.variablesByName("s1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                case ARGUMENTS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    vars = method.arguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    throw new InternalException("should not happen");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            for (Iterator it = vars.iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                LocalVariable lv = (LocalVariable)it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (sb.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    sb.append(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                sb.append(lv.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            String st = exc.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            int inx = st.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return st.substring(inx+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         * Get to the top of main()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         * to determine targetClass and mainThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        BreakpointEvent bpe = startToMain("FinalLocalsTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        targetClass = bpe.location().declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        mainThread = bpe.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        EventRequestManager erm = vm().eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         * Get to a point where the classes are loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        BreakpointEvent bp = resumeTo("FinalLocalsTarg", "hi", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        ReferenceType rt = findReferenceType("FinalLocalsTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (rt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new Exception("FinalLocalsTarg: not loaded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         * Inspect the LocalVariableTable attributes for method "test1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         * NOTE: .class files compiled with some versions of javac will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         * give results listed in different order.  That's OK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        Method method = findMethod(rt, "test1", "(II)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw new Exception("Method not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        test(method, VARIABLES, "VARIABLES",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
             "t,k,s1,z,r,b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        test(method, BYNAME, "BYNAME",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
             "s1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        test(method, ARGUMENTS, "ARGUMENTS",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
             "t,k");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         * All Done.  Resume the target listening for events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         * deal with results of test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * if anything has called failure("foo") testFailed will be true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            println("FinalLocalsTest: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throw new Exception("FinalLocalsTest: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
}