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 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
|
/*
* Copyright (c) 2001, 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 4451941 4527072
* @summary Test argument types for invoke
* @author Robert Field
*
* @library ..
*
* @run build TestScaffold VMConnection TargetListener TargetAdapter
* @run compile -g InvokeTest.java
* @run driver InvokeTest
*/
import com.sun.jdi.*;
import com.sun.jdi.event.*;
import com.sun.jdi.request.*;
import java.util.*;
/********** target program **********/
class InvokeTarg {
static InvokeTarg myself = null;
boolean[] aBooleanArray = new boolean[] {true, true};
byte[] aByteArray = new byte[] {4, 2};
char[] aCharArray = new char[] {'k', 'p'};
short[] aShortArray = new short[] {55,12, 12};
int[] aIntArray = new int[] {6, 3, 1};
long[] aLongArray = new long[] {3423423};
float[] aFloatArray = new float[] {(float)2.1};
double[] aDoubleArray = new double[] {3.141595358979};
boolean[][] aBoolean2DArray = new boolean[][]
{{true, false}, {false, true}};
byte[][] aByte2DArray = new byte[][] {{22,66}, {8,9}};
char[][] aChar2DArray = new char[][] {{22,66}, {8,9}};
short[][] aShort2DArray = new short[][] {{22,66}, {8,9}};
int[][] aInt2DArray = new int[][] {{22,66}, {8,9}};
long[][] aLong2DArray = new long[][] {{22,66}, {8,9}};
float[][] aFloat2DArray = new float[][] {{22,66}, {8,9}};
double[][] aDouble2DArray = new double[][] {{22,66}, {8,9}};
String[] aStringArray = new String[] {"testing"};
String[][] aString2DArray = new String[][]
{{"hi", "there"}, {"oh"}};
Date aDate = new Date();
Date[] aDateArray = new Date[] {};
Date[][] aDate2DArray = new Date[][] {{}};
String aString = "jjxx";
long longCheck = 0;
boolean booleanCheck = false;
boolean voidCheck = false;
Object objectCheck = null;
public static void main(String[] args){
System.out.println("Howdy!");
(new InvokeTarg()).sayHi();
}
void sayHi() {
}
void checkIn() {
}
boolean invokeVoid() {
voidCheck = true;
checkIn();
return true;
}
boolean invokeBoolean(boolean val) {
booleanCheck = val;
checkIn();
return val;
}
byte invokeByte(byte val) {
longCheck = val;
checkIn();
return val;
}
char invokeChar(char val) {
longCheck = val;
checkIn();
return val;
}
short invokeShort(short val) {
longCheck = val;
checkIn();
return val;
}
int invokeInt(int val) {
longCheck = val;
checkIn();
return val;
}
long invokeLong(long val) {
longCheck = val;
checkIn();
return val;
}
float invokeFloat(float val) {
longCheck = (long)val;
checkIn();
return val;
}
double invokeDouble(double val) {
longCheck = (long)val;
checkIn();
return val;
}
boolean[] invokeBooleanArray(boolean[] val) {
objectCheck = val;
checkIn();
return val;
}
byte[] invokeByteArray(byte[] val) {
objectCheck = val;
checkIn();
return val;
}
char[] invokeCharArray(char[] val) {
objectCheck = val;
checkIn();
return val;
}
short[] invokeShortArray(short[] val) {
objectCheck = val;
checkIn();
return val;
}
int[] invokeIntArray(int[] val) {
objectCheck = val;
checkIn();
return val;
}
long[] invokeLongArray(long[] val) {
objectCheck = val;
checkIn();
return val;
}
float[] invokeFloatArray(float[] val) {
objectCheck = val;
checkIn();
return val;
}
double[] invokeDoubleArray(double[] val) {
objectCheck = val;
checkIn();
return val;
}
boolean[][] invokeBoolean2DArray(boolean[][] val) {
objectCheck = val;
checkIn();
return val;
}
byte[][] invokeByte2DArray(byte[][] val) {
objectCheck = val;
checkIn();
return val;
}
char[][] invokeChar2DArray(char[][] val) {
objectCheck = val;
checkIn();
return val;
}
short[][] invokeShort2DArray(short[][] val) {
objectCheck = val;
checkIn();
return val;
}
int[][] invokeInt2DArray(int[][] val) {
objectCheck = val;
checkIn();
return val;
}
long[][] invokeLong2DArray(long[][] val) {
objectCheck = val;
checkIn();
return val;
}
float[][] invokeFloat2DArray(float[][] val) {
objectCheck = val;
checkIn();
return val;
}
double[][] invokeDouble2DArray(double[][] val) {
objectCheck = val;
checkIn();
return val;
}
String invokeString(String val) {
objectCheck = val;
checkIn();
return val;
}
String[] invokeStringArray(String[] val) {
objectCheck = val;
checkIn();
return val;
}
String[][] invokeString2DArray(String[][] val) {
objectCheck = val;
checkIn();
return val;
}
Date invokeDate(Date val) {
objectCheck = val;
checkIn();
return val;
}
Date[] invokeDateArray(Date[] val) {
objectCheck = val;
checkIn();
return val;
}
Date[][] invokeDate2DArray(Date[][] val) {
objectCheck = val;
checkIn();
return val;
}
String invokeCombo(int[][] arr, String val) {
objectCheck = val;
checkIn();
return val;
}
int[][] invokeCombo2(int[][] val, String str) {
objectCheck = val;
checkIn();
return val;
}
}
/********** test program **********/
public class InvokeTest extends TestScaffold {
ReferenceType targetClass;
ThreadReference mainThread;
ObjectReference thisObject;
Field longCheckField;
Field booleanCheckField;
Field voidCheckField;
Field objectCheckField;
Value longValue;
Value booleanValue;
Value objectValue;
Value voidValue;
InvokeTest (String args[]) {
super(args);
}
public static void main(String[] args) throws Exception {
new InvokeTest(args).startTests();
}
/********** event handlers **********/
// not use now
public void breakpointReached(BreakpointEvent event) {
println("Got BreakpointEvent");
longValue = thisObject.getValue(longCheckField);
booleanValue = thisObject.getValue(booleanCheckField);
objectValue = thisObject.getValue(objectCheckField);
voidValue = thisObject.getValue(voidCheckField);
}
/********** test assist **********/
void invoke(Method method, List args, Value value) {
Value returnValue = null;
try {
returnValue = thisObject.invokeMethod(mainThread,
method, args, 0);
} catch ( Exception ee) {
println("Got Exception: " + ee);
ee.printStackTrace();
}
println(" return val = " + returnValue);
// It has to be the same value as what we passed in!
if (returnValue.equals(value)) {
println(" " + method.name() + " return value matches: "
+ value);
} else {
if (value != null) {
failure("FAIL: " + method.name() + " returned: " + returnValue +
" expected: " + value );
} else {
println(" " + method.name() + " return value : " + returnValue);
}
}
Value checkValue = (value instanceof PrimitiveValue)?
((value instanceof BooleanValue)?
booleanValue : longValue) :
objectValue;
}
void invoke(String methodName, String methodSig,
List args, Value value)
throws Exception {
Method method = findMethod(targetClass, methodName, methodSig);
if ( method == null) {
failure("FAILED: Can't find method: " + methodName + " for class = " + targetClass);
return;
}
invoke(method, args, value);
}
void invoke(String methodName, String methodSig, Value value)
throws Exception {
List args = new ArrayList(1);
args.add(value);
invoke(methodName, methodSig, args, value);
}
void invoke(String methodName, String methodSig, String fieldName)
throws Exception {
invoke(methodName, methodSig, fieldValue(fieldName));
}
private Method toStringMethod;
Method gettoStringMethod() {
if ( toStringMethod != null) {
return toStringMethod;
}
// We have to find it. First find java.lang.Object
List myClasses = vm().allClasses();
Iterator iter = myClasses.iterator();
ReferenceType objectMirror = null;
while (iter.hasNext()) {
ReferenceType xx = (ReferenceType)iter.next();
if (xx.name().equals("java.lang.Object")) {
objectMirror = xx;
break;
}
}
if (objectMirror == null) {
return null;
}
// Then find toSting
List meths = objectMirror.methods();
iter = meths.iterator();
while (iter.hasNext()) {
toStringMethod = (Method)iter.next();
if (toStringMethod.name().equals("toString")) {
return toStringMethod;
}
}
toStringMethod = null;
return null;
}
// This calls toString on a field
protected void callToString(String fieldName) throws Exception {
// Sorry for this kludgy use of global vars.
ObjectReference saveObject = thisObject;
Method toStringMethod = gettoStringMethod();
Field theField = targetClass.fieldByName(fieldName);
thisObject = (ObjectReference)thisObject.getValue( theField);
invoke(toStringMethod, new ArrayList(0), null);
thisObject = saveObject;
}
Value fieldValue(String fieldName) {
Field field = targetClass.fieldByName(fieldName);
return thisObject.getValue(field);
}
/********** test core **********/
protected void runTests() throws Exception {
/*
* Get to the top of sayHi()
* to determine targetClass and mainThread
*/
BreakpointEvent bpe = startTo("InvokeTarg", "sayHi", "()V");
targetClass = bpe.location().declaringType();
mainThread = bpe.thread();
StackFrame frame = mainThread.frame(0);
thisObject = frame.thisObject();
longCheckField = targetClass.fieldByName("longCheck");
booleanCheckField = targetClass.fieldByName("booleanCheck");
objectCheckField = targetClass.fieldByName("objectCheck");
voidCheckField = targetClass.fieldByName("voidCheck");
callToString("aBooleanArray");
invoke("invokeVoid", "()Z", new ArrayList(0), vm().mirrorOf(true));
invoke("invokeBoolean", "(Z)Z", vm().mirrorOf(true));
invoke("invokeByte", "(B)B", vm().mirrorOf((byte)14));
invoke("invokeChar", "(C)C", vm().mirrorOf('h'));
invoke("invokeShort", "(S)S", vm().mirrorOf((short)54));
invoke("invokeInt", "(I)I", vm().mirrorOf((int)414));
invoke("invokeLong", "(J)J", vm().mirrorOf((long)140000));
invoke("invokeFloat", "(F)F", vm().mirrorOf((float)315));
invoke("invokeDouble", "(D)D", vm().mirrorOf((double)181818));
invoke("invokeBooleanArray", "([Z)[Z", "aBooleanArray");
invoke("invokeByteArray", "([B)[B", "aByteArray");
invoke("invokeCharArray", "([C)[C", "aCharArray");
invoke("invokeShortArray", "([S)[S", "aShortArray");
invoke("invokeIntArray", "([I)[I", "aIntArray");
invoke("invokeLongArray", "([J)[J", "aLongArray");
invoke("invokeFloatArray", "([F)[F", "aFloatArray");
invoke("invokeDoubleArray", "([D)[D", "aDoubleArray");
invoke("invokeBoolean2DArray", "([[Z)[[Z", "aBoolean2DArray");
invoke("invokeByte2DArray", "([[B)[[B", "aByte2DArray");
invoke("invokeChar2DArray", "([[C)[[C", "aChar2DArray");
invoke("invokeShort2DArray", "([[S)[[S", "aShort2DArray");
invoke("invokeInt2DArray", "([[I)[[I", "aInt2DArray");
invoke("invokeLong2DArray", "([[J)[[J", "aLong2DArray");
invoke("invokeFloat2DArray", "([[F)[[F", "aFloat2DArray");
invoke("invokeDouble2DArray", "([[D)[[D", "aDouble2DArray");
invoke("invokeString", "(Ljava/lang/String;)Ljava/lang/String;",
vm().mirrorOf("Howdy"));
invoke("invokeStringArray", "([Ljava/lang/String;)[Ljava/lang/String;",
"aStringArray");
invoke("invokeString2DArray", "([[Ljava/lang/String;)[[Ljava/lang/String;",
"aString2DArray");
invoke("invokeDate", "(Ljava/util/Date;)Ljava/util/Date;",
"aDate");
invoke("invokeDateArray", "([Ljava/util/Date;)[Ljava/util/Date;",
"aDateArray");
invoke("invokeDate2DArray", "([[Ljava/util/Date;)[[Ljava/util/Date;",
"aDate2DArray");
Value i2 = fieldValue("aInt2DArray");
Value str = vm().mirrorOf("Later");
List args = new ArrayList(2);
args.add(i2);
args.add(str);
invoke("invokeCombo",
"([[ILjava/lang/String;)Ljava/lang/String;",
args, str);
invoke("invokeCombo2",
"([[ILjava/lang/String;)[[I",
args, i2);
/*
* resume the target listening for events
*/
listenUntilVMDisconnect();
/*
* deal with results of test
* if anything has called failure("foo") testFailed will be true
*/
if (!testFailed) {
println("InvokeTest: passed");
} else {
throw new Exception("InvokeTest: failed");
}
}
}
|