|
1 /* |
|
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 5043030 |
|
27 * @summary Verify that the method java.lang.reflect.Method.invoke(Object, Object...) |
|
28 * makes use of the same caching mechanism as used for autoboxing |
|
29 * when wrapping returned values of the primitive types. |
|
30 * @author Andrej Golovnin |
|
31 * @run main/othervm -Dsun.reflect.noInflation=true TestMethodReflectValueOf |
|
32 * @run main/othervm -Dsun.reflect.noInflation=false -Dsun.reflect.inflationThreshold=500 TestMethodReflectValueOf |
|
33 */ |
|
34 |
|
35 import java.lang.reflect.InvocationTargetException; |
|
36 import java.lang.reflect.Method; |
|
37 |
|
38 |
|
39 public class TestMethodReflectValueOf { |
|
40 |
|
41 public static void main(String[] args) { |
|
42 // When the inflation is disabled we compare values using "==" |
|
43 // as the returned values of the primitive types should be cached |
|
44 // by the same mechanism as used for autoboxing. When the inflation |
|
45 // is enabled we use "equals()"-method to compare values as the native |
|
46 // code still creates new instances to wrap values of the primitive |
|
47 // types. |
|
48 boolean checkIdentity = Boolean.getBoolean("sun.reflect.noInflation"); |
|
49 |
|
50 // Boolean#valueOf test |
|
51 testMethod(Boolean.TYPE, Boolean.FALSE, checkIdentity); |
|
52 testMethod(Boolean.TYPE, Boolean.TRUE, checkIdentity); |
|
53 |
|
54 // Byte#valueOf test |
|
55 for (int b = Byte.MIN_VALUE; b < (Byte.MAX_VALUE + 1); b++) { |
|
56 testMethod(Byte.TYPE, Byte.valueOf((byte) b), checkIdentity); |
|
57 } |
|
58 |
|
59 // Character#valueOf test |
|
60 for (char c = '\u0000'; c <= '\u007F'; c++) { |
|
61 testMethod(Character.TYPE, Character.valueOf(c), checkIdentity); |
|
62 } |
|
63 |
|
64 // Integer#valueOf test |
|
65 for (int i = -128; i <= 127; i++) { |
|
66 testMethod(Integer.TYPE, Integer.valueOf(i), checkIdentity); |
|
67 } |
|
68 |
|
69 // Long#valueOf test |
|
70 for (long l = -128L; l <= 127L; l++) { |
|
71 testMethod(Long.TYPE, Long.valueOf(l), checkIdentity); |
|
72 } |
|
73 |
|
74 // Short#valueOf test |
|
75 for (short s = -128; s <= 127; s++) { |
|
76 testMethod(Short.TYPE, Short.valueOf(s), checkIdentity); |
|
77 } |
|
78 } |
|
79 |
|
80 public static void testMethod(Class<?> primType, Object wrappedValue, |
|
81 boolean checkIdentity) |
|
82 { |
|
83 String methodName = primType.getName() + "Method"; |
|
84 try { |
|
85 Method method = TestMethodReflectValueOf.class.getMethod(methodName, primType); |
|
86 Object result = method.invoke(new TestMethodReflectValueOf(), wrappedValue); |
|
87 if (checkIdentity) { |
|
88 if (result != wrappedValue) { |
|
89 throw new RuntimeException("The value " + wrappedValue |
|
90 + " is not cached for the type " + primType); |
|
91 } |
|
92 } else { |
|
93 if (!result.equals(wrappedValue)) { |
|
94 throw new RuntimeException("The result value " + result |
|
95 + " is not equal to the expected value " |
|
96 + wrappedValue + " for the type " + primType); |
|
97 } |
|
98 } |
|
99 } catch ( NoSuchMethodException | SecurityException |
|
100 | IllegalAccessException | IllegalArgumentException |
|
101 | InvocationTargetException e) |
|
102 { |
|
103 throw new RuntimeException(e); |
|
104 } |
|
105 } |
|
106 |
|
107 public int intMethod(int value) { |
|
108 return value; |
|
109 } |
|
110 |
|
111 public long longMethod(long value) { |
|
112 return value; |
|
113 } |
|
114 |
|
115 public short shortMethod(short value) { |
|
116 return value; |
|
117 } |
|
118 |
|
119 public byte byteMethod(byte value) { |
|
120 return value; |
|
121 } |
|
122 |
|
123 public char charMethod(char value) { |
|
124 return value; |
|
125 } |
|
126 |
|
127 public boolean booleanMethod(boolean value) { |
|
128 return value; |
|
129 } |
|
130 |
|
131 } |