jdk/test/com/sun/jdi/Vars.java
author dcubed
Mon, 23 Nov 2009 10:29:33 -0700
changeset 4325 38a035021ba1
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6903102: 3/3 fixes in nightly testing version of ShellScaffold.sh need to be committed Summary: Merge Jim's ShellScaffold.sh fixes with Tim's ShellScaffold.sh fixes. Reviewed-by: tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2002 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
 *  @summary Test Method.variables() and the like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  @author Robert Field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *  @library scaffold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  @run build JDIScaffold VMConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  @run compile -g Vars.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run main/othervm Vars
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class is internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
abstract class AbstractTestVars {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    abstract float test1(String blah, int i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    native int test2(double k, boolean b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    String test3(short sh, long lo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        String st = "roses";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        return st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * This class is internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
class TestVars extends AbstractTestVars {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    float test1(String blah, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        return (float)1.1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    void 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
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        new TestVars().hi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * "Vars" test runs TestVars and makes LocalVariable queries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public class Vars extends JDIScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    final String[] args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    Vars(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.args = args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        new Vars(args).runTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static final int VARIABLES = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static final int BYNAME = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static final int ARGUMENTS = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    String testCase(Method method, int which) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            List vars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            switch (which) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                case VARIABLES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    vars = method.variables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                case BYNAME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    vars = method.variablesByName("st");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                case ARGUMENTS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    vars = method.arguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    throw new InternalException("should not happen");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            for (Iterator it = vars.iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                LocalVariable lv = (LocalVariable)it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                if (sb.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    sb.append(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                sb.append(lv.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            String st = exc.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            int inx = st.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return st.substring(inx+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Sets failed if fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    void test(Method method, int which, String name, String expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        String got = testCase(method, which);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (got.equals(expected)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            System.out.println(name + ": got expected: " + got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            System.out.println(name + ": ERROR expected: " + expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            System.out.println("      got: " + got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    void test2(Method method, int which, String name, String expected, String expected2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        String got = testCase(method, which);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (got.equals(expected) || got.equals(expected2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            System.out.println(name + ": got expected: " + got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            System.out.println(name + ": ERROR expected: " + expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            System.out.println("      got: " + got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        List argList = new ArrayList(Arrays.asList(args));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        argList.add("TestVars");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        System.out.println("run args: " + argList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        connect((String[])argList.toArray(args));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        waitForVMStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         * Get to a point where the classes are loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        BreakpointEvent bp = resumeTo("TestVars", "hi", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         * These classes should have no line numbers, except for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         * one in the implicit constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        ReferenceType rt = findReferenceType("AbstractTestVars");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (rt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new Exception("AbstractTestVars: not loaded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        Method method = findMethod(rt, "test1", "(Ljava/lang/String;I)F");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new Exception("Method not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        test(method, VARIABLES, "abstract/variables",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
             "AbsentInformationException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        test(method, BYNAME, "abstract/variablesByName",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
             "AbsentInformationException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        test(method, ARGUMENTS, "abstract/arguments",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
             "AbsentInformationException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        method = findMethod(rt, "test2", "(DZ)I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new Exception("Method not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        test(method, VARIABLES, "native/variables",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
             "AbsentInformationException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        test(method, BYNAME, "native/variablesByName",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
             "AbsentInformationException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        test(method, ARGUMENTS, "native/arguments",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
             "AbsentInformationException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        method = findMethod(rt, "test3", "(SJ)Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new Exception("Method not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // javac can put these in whatever order it desires.  hopper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // does it one way and mantis another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        test2(method, VARIABLES, "normal/variables", "sh,lo,st", "st,sh,lo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        test(method, BYNAME, "normal/variablesByName", "st");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        test(method, ARGUMENTS, "normal/arguments", "sh,lo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // Allow application to complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        resumeToVMDeath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new Exception("Vars: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            System.out.println("Vars: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
}