Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Proxy/Boxing.java
38828 views
/*1* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 488934225* @summary This test verifies that when a primitive boolean value is26* passed by a dynamic proxy class to an invocation handler, it is27* boxed as one of the canonical Boolean instances (Boolean.TRUE or28* Boolean.FALSE). This test also exercises a dynamic proxy class's29* boxing of all primitive types.30* @author Peter Jones31*32* @run main Boxing33*/3435import java.lang.reflect.InvocationHandler;36import java.lang.reflect.Method;37import java.lang.reflect.Proxy;38import java.util.Random;3940public class Boxing {4142private interface Test {43void m(byte b,44char c,45double d,46float f,47int i,48long j,49short s,50boolean z);51}5253private static final int REPS = 100000;5455private byte b;56private char c;57private double d;58private float f;59private int i;60private long j;61private short s;62private boolean z;6364public static void main(String[] args) {65(new Boxing()).run();66System.err.println("TEST PASSED");67}6869private void run() {70Random random = new Random(42); // ensure consistent test domain7172Test proxy = (Test) Proxy.newProxyInstance(73Test.class.getClassLoader(),74new Class<?>[] { Test.class },75new TestHandler());7677for (int rep = 0; rep < REPS; rep++) {78b = (byte) random.nextInt();79c = (char) random.nextInt();80d = random.nextDouble();81f = random.nextFloat();82i = random.nextInt();83j = random.nextLong();84s = (short) random.nextInt();85z = random.nextBoolean();86proxy.m(b,c,d,f,i,j,s,z);87}88}8990private class TestHandler implements InvocationHandler {91public Object invoke(Object proxy, Method method, Object[] args)92throws Throwable93{94if (!method.getName().equals("m")) {95throw new AssertionError();96}9798byte b2 = ((Byte) args[0]).byteValue();99if (b2 != b) {100throw new RuntimeException("TEST FAILED: " +101"wrong byte, expected " + b + " but got " + b2);102}103104char c2 = ((Character) args[1]).charValue();105if (c2 != c) {106throw new RuntimeException("TEST FAILED: " +107"wrong char, expected " + c + " but got " + c2);108}109110double d2 = ((Double) args[2]).doubleValue();111if (d2 != d) {112throw new RuntimeException("TEST FAILED: " +113"wrong double, expected " + d + " but got " + d2);114}115116float f2 = ((Float) args[3]).floatValue();117if (f2 != f) {118throw new RuntimeException("TEST FAILED: " +119"wrong float, expected " + f + " but got " + f2);120}121122int i2 = ((Integer) args[4]).intValue();123if (i2 != i) {124throw new RuntimeException("TEST FAILED: " +125"wrong int, expected " + i + " but got " + i2);126}127128long j2 = ((Long) args[5]).longValue();129if (j2 != j) {130throw new RuntimeException("TEST FAILED: " +131"wrong long, expected " + j + " but got " + j2);132}133134short s2 = ((Short) args[6]).shortValue();135if (s2 != s) {136throw new RuntimeException("TEST FAILED: " +137"wrong short, expected " + s + " but got " + s2);138}139140Boolean Z = Boolean.valueOf(z);141Boolean Z2 = (Boolean) args[7];142if (Z2 != Z) {143throw new RuntimeException("TEST FAILED: " +144"wrong Boolean instance, expected " +145identityToString(Z) + " but got " + identityToString(Z2));146}147148return null;149}150}151152private static String identityToString(Object obj) {153return obj.toString() + "@" + System.identityHashCode(obj);154}155}156157158