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