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
|
/*
* Copyright (c) 2003, 2010, 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 4889342
* @summary This test verifies that when a primitive boolean value is
* passed by a dynamic proxy class to an invocation handler, it is
* boxed as one of the canonical Boolean instances (Boolean.TRUE or
* Boolean.FALSE). This test also exercises a dynamic proxy class's
* boxing of all primitive types.
* @author Peter Jones
*
* @run main Boxing
*/
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Random;
public class Boxing {
private interface Test {
void m(byte b,
char c,
double d,
float f,
int i,
long j,
short s,
boolean z);
}
private static final int REPS = 100000;
private byte b;
private char c;
private double d;
private float f;
private int i;
private long j;
private short s;
private boolean z;
public static void main(String[] args) {
(new Boxing()).run();
System.err.println("TEST PASSED");
}
private void run() {
Random random = new Random(42); // ensure consistent test domain
Test proxy = (Test) Proxy.newProxyInstance(
Test.class.getClassLoader(),
new Class<?>[] { Test.class },
new TestHandler());
for (int rep = 0; rep < REPS; rep++) {
b = (byte) random.nextInt();
c = (char) random.nextInt();
d = random.nextDouble();
f = random.nextFloat();
i = random.nextInt();
j = random.nextLong();
s = (short) random.nextInt();
z = random.nextBoolean();
proxy.m(b,c,d,f,i,j,s,z);
}
}
private class TestHandler implements InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
if (!method.getName().equals("m")) {
throw new AssertionError();
}
byte b2 = ((Byte) args[0]).byteValue();
if (b2 != b) {
throw new RuntimeException("TEST FAILED: " +
"wrong byte, expected " + b + " but got " + b2);
}
char c2 = ((Character) args[1]).charValue();
if (c2 != c) {
throw new RuntimeException("TEST FAILED: " +
"wrong char, expected " + c + " but got " + c2);
}
double d2 = ((Double) args[2]).doubleValue();
if (d2 != d) {
throw new RuntimeException("TEST FAILED: " +
"wrong double, expected " + d + " but got " + d2);
}
float f2 = ((Float) args[3]).floatValue();
if (f2 != f) {
throw new RuntimeException("TEST FAILED: " +
"wrong float, expected " + f + " but got " + f2);
}
int i2 = ((Integer) args[4]).intValue();
if (i2 != i) {
throw new RuntimeException("TEST FAILED: " +
"wrong int, expected " + i + " but got " + i2);
}
long j2 = ((Long) args[5]).longValue();
if (j2 != j) {
throw new RuntimeException("TEST FAILED: " +
"wrong long, expected " + j + " but got " + j2);
}
short s2 = ((Short) args[6]).shortValue();
if (s2 != s) {
throw new RuntimeException("TEST FAILED: " +
"wrong short, expected " + s + " but got " + s2);
}
Boolean Z = Boolean.valueOf(z);
Boolean Z2 = (Boolean) args[7];
if (Z2 != Z) {
throw new RuntimeException("TEST FAILED: " +
"wrong Boolean instance, expected " +
identityToString(Z) + " but got " + identityToString(Z2));
}
return null;
}
}
private static String identityToString(Object obj) {
return obj.toString() + "@" + System.identityHashCode(obj);
}
}
|