jdk/test/com/sun/jdi/VarargsTest.java
author jjg
Thu, 22 May 2008 15:51:41 -0700
changeset 655 1ebc7ce89018
parent 2 90ce3da70b43
child 3432 8acd97c69118
permissions -rw-r--r--
6705945: com.sun.tools.javac.zip files do not have valid copyright Reviewed-by: mcimadamore
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 2003 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
 *  @bug 4870984
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @summary  JPDA: Add support for RFE 4856541 - varargs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  @author jjh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  @run compile -g -source 1.5 -target 1.5 VarargsTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run main VarargsTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /********** target program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class VarargsTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // These are args that will get passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static String[] strArray = new String[] {"a", "b"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static int[] intArray = new int[] {1, 2};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // We will pass these to a varargs instance method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static VarargsTarg vt1 = new VarargsTarg("vt1", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static VarargsTarg vt2 = new VarargsTarg("vt2", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    String iname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    VarargsTarg(String ... name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        iname = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        for (int ii = 0; ii < name.length; ii++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            iname += name[ii];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static void main(String[] args){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        System.out.println("Howdy!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
         * This isn't really part of the test, it just shows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         * the kinds of calls the debugger test will do and lets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         * you verify how javac handles these calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        System.out.println("debuggee: " + varString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        System.out.println("debuggee: " + varString(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        System.out.println("debuggee: " + varString("a"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.out.println("debuggee: " + varString("b", "c"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        System.out.println("debuggee: " + fixedString(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        System.out.println("debuggee: " + vt1.varStringInstance(vt1, vt2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        System.out.println("debuggge: " + varInt(1, 2, 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.out.println("debuggee: " + varInteger( new Integer(89)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // Should be autoboxed: javac converts the ints to Integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // Needs a new method in java.lang.Integer which is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // in the generics workspace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        System.out.println("debugggee: " + varInteger(3, 5, 6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        System.out.println("Goodbye from VarargsTarg!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        bkpt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static void bkpt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Define the methods to be called from the debugger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static String fixedInt(int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return "" + p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static String fixedInteger(Integer p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return "" + p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     static String varInt(int... ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         String retVal = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         for (int ii = 0; ii < ss.length; ii++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
             retVal += ss[ii];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
         return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static String varInteger(Integer... ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        String retVal = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        for (int ii = 0; ii < ss.length; ii++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            retVal += ss[ii];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    static String varString(String... ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (ss == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return "-null-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        String retVal = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (int ii = 0; ii < ss.length; ii++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            retVal += ss[ii];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    static String varString2(int p1, String... ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return p1 + varString(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    static String fixedString(String ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return "-fixed-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    String varStringInstance(VarargsTarg... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (args == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return "-null-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        //System.out.println("debugee: ss length = " + ss.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        String retVal = iname + ": ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        for (int ii = 0; ii < args.length; ii++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            retVal += args[ii].iname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /********** test program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
public class VarargsTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    ClassType targetClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    ThreadReference mainThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    VarargsTest (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public static void main(String[] args)      throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        new VarargsTest(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    void fail(String reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        failure(reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Call a method in the debuggee and verify the return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    void doInvoke(Object ct, Method mm, List args, Object expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        StringReference returnValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            returnValue = doInvokeNoVerify(ct, mm, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } catch (Exception ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            fail("failure: invokeMethod got exception : " + ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            ee.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (!returnValue.value().equals(expected)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            fail("failure: expected \"" + expected + "\", got \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                 returnValue.value() + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Call a method in the debuggee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    StringReference doInvokeNoVerify(Object ct, Method mm, List args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        StringReference returnValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (ct instanceof ClassType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            returnValue = (StringReference)((ClassType)ct).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                invokeMethod(mainThread, mm, args, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            returnValue = (StringReference)((ObjectReference)ct).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                invokeMethod(mainThread, mm, args, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return returnValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /********** test core **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         * Get to the top of main()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * to determine targetClass and mainThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        BreakpointEvent bpe = startToMain("VarargsTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        targetClass = (ClassType)bpe.location().declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        mainThread = bpe.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * Run past the calls the debuggee makes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * just to see what they do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        bpe = resumeTo("VarargsTarg", "bkpt", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         * Find Method objects for varString and varString2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * Both are tested just to show that the code works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         * if there is just one param or if there is more than one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        ReferenceType rt = findReferenceType("VarargsTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        List mList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         * The test consists of calling the varargs static and instance methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
         * (and constructor) passing primitives, Strings, and Objects, and also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         * passing arrays of the above instead of individual args.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         * The same code is used in the underlying JDI implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         * for calling instance methods, static methods, and constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         * so this test doesn't have to try all possible argument configurations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
         * with each type of method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        mList = rt.methodsByName("varString");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        Method varString = (Method)mList.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        mList = rt.methodsByName("varString2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        Method varString2 = (Method)mList.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (!varString.isVarArgs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            fail("failure: varString is not flagged as being var args");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (!varString2.isVarArgs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            fail("failure: varString2 is not flagged as being var args");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
         * Setup arg lists for both varString and varString2 that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         * have null in the varargs position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            // call varString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            ArrayList nullArg1 = new ArrayList(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            doInvoke(targetClass, varString, nullArg1,  "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            // call varString(null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            ArrayList nullArg1 = new ArrayList(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            nullArg1.add(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            doInvoke(targetClass, varString, nullArg1,  "-null-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            // call varString(9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            ArrayList nullArg2 = new ArrayList(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            nullArg2.add(vm().mirrorOf(9));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            doInvoke(targetClass, varString2, nullArg2,  "9");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            // call varString(9, null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            ArrayList nullArg2 = new ArrayList(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            nullArg2.add(vm().mirrorOf(9));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            nullArg2.add(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            doInvoke(targetClass, varString2, nullArg2,  "9-null-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            ArrayList args1 = new ArrayList(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            args1.add(vm().mirrorOf("1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // call varString("1")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            doInvoke(targetClass, varString, args1, "1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // call varString("1", "2")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            args1.add(vm().mirrorOf("2"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            args1.add(vm().mirrorOf("3"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            args1.add(vm().mirrorOf("4"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            doInvoke(targetClass, varString, args1, "1234");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            ArrayList args2 = new ArrayList(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            args2.add(vm().mirrorOf(9));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            args2.add(vm().mirrorOf("1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            // call varString2(9, "1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            doInvoke(targetClass, varString2, args2, "91");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            // call varString2(9, "1", "2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            args2.add(vm().mirrorOf("2"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            doInvoke(targetClass, varString2, args2, "912");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
             * Passing an array of Strings should work too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            Field ff = targetClass.fieldByName("strArray");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            Value vv1 = targetClass.getValue(ff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            // call varString(new String[] {"a", "b"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            ArrayList argsArray = new ArrayList(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            argsArray.add(vv1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            doInvoke(targetClass, varString, argsArray, "ab");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
             * But passing an array of Strings and another String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
             * should fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            argsArray.add(vm().mirrorOf("x"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            boolean isOk = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                // call varString(new String[] {"a", "b"}, "x")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                doInvokeNoVerify(targetClass, varString, argsArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            } catch (Exception ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                 * Since the number of args passed is > than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                 * the number of params, JDI assumes they are var args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                 * and tries to put the array containing the "a" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                 * "be" elements into a the first element of an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                 * of Strings.  This fails because you can't store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                 * an array into a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                isOk = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                //ee.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (!isOk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                fail("failure: an array and a String didn't cause an exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
             * Test calling instance method instead of static method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
             * and passing non-String objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            Field vtField = targetClass.fieldByName("vt1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            Value vv1 = targetClass.getValue(vtField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            vtField = targetClass.fieldByName("vt2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            Value vv2 = targetClass.getValue(vtField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            /* Create a new instance by calling the varargs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
             * ctor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
             * call new VarargsTarg("vt3", "xx");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            Value vv3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                mList = rt.methodsByName("<init>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                Method ctor = (Method)mList.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                if (!ctor.isVarArgs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    fail("failure: Constructor is not varargs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                ArrayList argsArray = new ArrayList(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                argsArray.add(vm().mirrorOf("vt3"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                argsArray.add(vm().mirrorOf("xx"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                vv3 = targetClass.newInstance(mainThread, ctor, argsArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            // call vt1.varStringInstance(vv1, vv2, vv3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            mList = rt.methodsByName("varStringInstance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            Method varStringInstance = (Method)mList.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            ArrayList argsArray = new ArrayList(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            argsArray.add(vv1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            argsArray.add(vv2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            argsArray.add(vv3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            doInvoke(vv1, varStringInstance, argsArray, "vt1: vt1vt2vt3xx");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
             * tests with primitive types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            List mlist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            Method mm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            ArrayList ll = new ArrayList(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            // call fixedInt(21)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            mlist = rt.methodsByName("fixedInt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            mm = (Method)mlist.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            ll.add(vm().mirrorOf(21));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            doInvoke(targetClass, mm, ll, "21");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            // autoboxing is not implemented in JDI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            // call fixedInteger(21)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            //mlist = rt.methodsByName("fixedInteger");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            //mm = (Method)mlist.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            //doInvoke(targetClass, mm, ll, "21");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            mlist = rt.methodsByName("varInt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            mm = (Method)mlist.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            // call varInt( new int[] {1, 2});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            Field ff = targetClass.fieldByName("intArray");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            Value vv1 = targetClass.getValue(ff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            ll.set(0, vv1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            doInvoke(targetClass, mm, ll, "12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            // call varInt(21, 22)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            ll.set(0, vm().mirrorOf(21));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            ll.add(vm().mirrorOf(22));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            doInvoke(targetClass, mm, ll, "2122");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            mlist = rt.methodsByName("varInteger");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            mm = (Method)mlist.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            // call varInteger(1, 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            // autoboxing is not implemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            //doInvoke(targetClass, mm, ll, "2122");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
         * We don't really need this for the test, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         * but without it, we sometimes hit 4728096.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         * deal with results of test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         * if anything has called failure("foo") testFailed will be true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            println("VarargsTest: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            throw new Exception("VarargsTest: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
}