5043030: (reflect) unnecessary object creation in reflection

Use valueOf() instead of new for primitive wrappers.

Reviewed-by: jfranck
This commit is contained in:
Andrej Golovnin 2014-09-09 12:04:31 +02:00 committed by Joel Borggrén-Franck
parent febacf5319
commit fe519e6845
36 changed files with 396 additions and 85 deletions

View File

@ -69,33 +69,34 @@ class AccessorGenerator implements ClassFileConstants {
protected short codeIdx;
protected short exceptionsIdx;
// Boxing
protected short valueOfIdx;
protected short booleanIdx;
protected short booleanCtorIdx;
protected short booleanBoxIdx;
protected short booleanUnboxIdx;
protected short byteIdx;
protected short byteCtorIdx;
protected short byteBoxIdx;
protected short byteUnboxIdx;
protected short characterIdx;
protected short characterCtorIdx;
protected short characterBoxIdx;
protected short characterUnboxIdx;
protected short doubleIdx;
protected short doubleCtorIdx;
protected short doubleBoxIdx;
protected short doubleUnboxIdx;
protected short floatIdx;
protected short floatCtorIdx;
protected short floatBoxIdx;
protected short floatUnboxIdx;
protected short integerIdx;
protected short integerCtorIdx;
protected short integerBoxIdx;
protected short integerUnboxIdx;
protected short longIdx;
protected short longCtorIdx;
protected short longBoxIdx;
protected short longUnboxIdx;
protected short shortIdx;
protected short shortCtorIdx;
protected short shortBoxIdx;
protected short shortUnboxIdx;
protected final short NUM_COMMON_CPOOL_ENTRIES = (short) 30;
protected final short NUM_BOXING_CPOOL_ENTRIES = (short) 72;
protected final short NUM_BOXING_CPOOL_ENTRIES = (short) 73;
// Requires that superClass has been set up
protected void emitCommonConstantPoolEntries() {
@ -181,9 +182,10 @@ class AccessorGenerator implements ClassFileConstants {
/** Constant pool entries required to be able to box/unbox primitive
types. Note that we don't emit these if we don't need them. */
protected void emitBoxingContantPoolEntries() {
// * [UTF-8] "valueOf"
// * [UTF-8] "java/lang/Boolean"
// * [CONSTANT_Class_info] for above
// * [UTF-8] "(Z)V"
// * [UTF-8] "(Z)Ljava/lang/Boolean;"
// * [CONSTANT_NameAndType_info] for above
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "booleanValue"
@ -192,7 +194,7 @@ class AccessorGenerator implements ClassFileConstants {
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "java/lang/Byte"
// * [CONSTANT_Class_info] for above
// * [UTF-8] "(B)V"
// * [UTF-8] "(B)Ljava/lang/Byte;"
// * [CONSTANT_NameAndType_info] for above
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "byteValue"
@ -201,7 +203,7 @@ class AccessorGenerator implements ClassFileConstants {
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "java/lang/Character"
// * [CONSTANT_Class_info] for above
// * [UTF-8] "(C)V"
// * [UTF-8] "(C)Ljava/lang/Character;"
// * [CONSTANT_NameAndType_info] for above
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "charValue"
@ -210,7 +212,7 @@ class AccessorGenerator implements ClassFileConstants {
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "java/lang/Double"
// * [CONSTANT_Class_info] for above
// * [UTF-8] "(D)V"
// * [UTF-8] "(D)Ljava/lang/Double;"
// * [CONSTANT_NameAndType_info] for above
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "doubleValue"
@ -219,7 +221,7 @@ class AccessorGenerator implements ClassFileConstants {
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "java/lang/Float"
// * [CONSTANT_Class_info] for above
// * [UTF-8] "(F)V"
// * [UTF-8] "(F)Ljava/lang/Float;"
// * [CONSTANT_NameAndType_info] for above
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "floatValue"
@ -228,7 +230,7 @@ class AccessorGenerator implements ClassFileConstants {
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "java/lang/Integer"
// * [CONSTANT_Class_info] for above
// * [UTF-8] "(I)V"
// * [UTF-8] "(I)Ljava/lang/Integer;"
// * [CONSTANT_NameAndType_info] for above
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "intValue"
@ -237,7 +239,7 @@ class AccessorGenerator implements ClassFileConstants {
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "java/lang/Long"
// * [CONSTANT_Class_info] for above
// * [UTF-8] "(J)V"
// * [UTF-8] "(J)Ljava/lang/Long;"
// * [CONSTANT_NameAndType_info] for above
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "longValue"
@ -246,21 +248,26 @@ class AccessorGenerator implements ClassFileConstants {
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "java/lang/Short"
// * [CONSTANT_Class_info] for above
// * [UTF-8] "(S)V"
// * [UTF-8] "(S)Ljava/lang/Short;"
// * [CONSTANT_NameAndType_info] for above
// * [CONSTANT_Methodref_info] for above
// * [UTF-8] "shortValue"
// * [UTF-8] "()S"
// * [CONSTANT_NameAndType_info] for above
// * [CONSTANT_Methodref_info] for above
// valueOf-method name
asm.emitConstantPoolUTF8("valueOf");
valueOfIdx = asm.cpi();
// Boolean
asm.emitConstantPoolUTF8("java/lang/Boolean");
asm.emitConstantPoolClass(asm.cpi());
booleanIdx = asm.cpi();
asm.emitConstantPoolUTF8("(Z)V");
asm.emitConstantPoolNameAndType(initIdx, asm.cpi());
asm.emitConstantPoolUTF8("(Z)Ljava/lang/Boolean;");
asm.emitConstantPoolNameAndType(valueOfIdx, asm.cpi());
asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi());
booleanCtorIdx = asm.cpi();
booleanBoxIdx = asm.cpi();
asm.emitConstantPoolUTF8("booleanValue");
asm.emitConstantPoolUTF8("()Z");
asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi());
@ -271,10 +278,10 @@ class AccessorGenerator implements ClassFileConstants {
asm.emitConstantPoolUTF8("java/lang/Byte");
asm.emitConstantPoolClass(asm.cpi());
byteIdx = asm.cpi();
asm.emitConstantPoolUTF8("(B)V");
asm.emitConstantPoolNameAndType(initIdx, asm.cpi());
asm.emitConstantPoolUTF8("(B)Ljava/lang/Byte;");
asm.emitConstantPoolNameAndType(valueOfIdx, asm.cpi());
asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi());
byteCtorIdx = asm.cpi();
byteBoxIdx = asm.cpi();
asm.emitConstantPoolUTF8("byteValue");
asm.emitConstantPoolUTF8("()B");
asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi());
@ -285,10 +292,10 @@ class AccessorGenerator implements ClassFileConstants {
asm.emitConstantPoolUTF8("java/lang/Character");
asm.emitConstantPoolClass(asm.cpi());
characterIdx = asm.cpi();
asm.emitConstantPoolUTF8("(C)V");
asm.emitConstantPoolNameAndType(initIdx, asm.cpi());
asm.emitConstantPoolUTF8("(C)Ljava/lang/Character;");
asm.emitConstantPoolNameAndType(valueOfIdx, asm.cpi());
asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi());
characterCtorIdx = asm.cpi();
characterBoxIdx = asm.cpi();
asm.emitConstantPoolUTF8("charValue");
asm.emitConstantPoolUTF8("()C");
asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi());
@ -299,10 +306,10 @@ class AccessorGenerator implements ClassFileConstants {
asm.emitConstantPoolUTF8("java/lang/Double");
asm.emitConstantPoolClass(asm.cpi());
doubleIdx = asm.cpi();
asm.emitConstantPoolUTF8("(D)V");
asm.emitConstantPoolNameAndType(initIdx, asm.cpi());
asm.emitConstantPoolUTF8("(D)Ljava/lang/Double;");
asm.emitConstantPoolNameAndType(valueOfIdx, asm.cpi());
asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi());
doubleCtorIdx = asm.cpi();
doubleBoxIdx = asm.cpi();
asm.emitConstantPoolUTF8("doubleValue");
asm.emitConstantPoolUTF8("()D");
asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi());
@ -313,10 +320,10 @@ class AccessorGenerator implements ClassFileConstants {
asm.emitConstantPoolUTF8("java/lang/Float");
asm.emitConstantPoolClass(asm.cpi());
floatIdx = asm.cpi();
asm.emitConstantPoolUTF8("(F)V");
asm.emitConstantPoolNameAndType(initIdx, asm.cpi());
asm.emitConstantPoolUTF8("(F)Ljava/lang/Float;");
asm.emitConstantPoolNameAndType(valueOfIdx, asm.cpi());
asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi());
floatCtorIdx = asm.cpi();
floatBoxIdx = asm.cpi();
asm.emitConstantPoolUTF8("floatValue");
asm.emitConstantPoolUTF8("()F");
asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi());
@ -327,10 +334,10 @@ class AccessorGenerator implements ClassFileConstants {
asm.emitConstantPoolUTF8("java/lang/Integer");
asm.emitConstantPoolClass(asm.cpi());
integerIdx = asm.cpi();
asm.emitConstantPoolUTF8("(I)V");
asm.emitConstantPoolNameAndType(initIdx, asm.cpi());
asm.emitConstantPoolUTF8("(I)Ljava/lang/Integer;");
asm.emitConstantPoolNameAndType(valueOfIdx, asm.cpi());
asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi());
integerCtorIdx = asm.cpi();
integerBoxIdx = asm.cpi();
asm.emitConstantPoolUTF8("intValue");
asm.emitConstantPoolUTF8("()I");
asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi());
@ -341,10 +348,10 @@ class AccessorGenerator implements ClassFileConstants {
asm.emitConstantPoolUTF8("java/lang/Long");
asm.emitConstantPoolClass(asm.cpi());
longIdx = asm.cpi();
asm.emitConstantPoolUTF8("(J)V");
asm.emitConstantPoolNameAndType(initIdx, asm.cpi());
asm.emitConstantPoolUTF8("(J)Ljava/lang/Long;");
asm.emitConstantPoolNameAndType(valueOfIdx, asm.cpi());
asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi());
longCtorIdx = asm.cpi();
longBoxIdx = asm.cpi();
asm.emitConstantPoolUTF8("longValue");
asm.emitConstantPoolUTF8("()J");
asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi());
@ -355,10 +362,10 @@ class AccessorGenerator implements ClassFileConstants {
asm.emitConstantPoolUTF8("java/lang/Short");
asm.emitConstantPoolClass(asm.cpi());
shortIdx = asm.cpi();
asm.emitConstantPoolUTF8("(S)V");
asm.emitConstantPoolNameAndType(initIdx, asm.cpi());
asm.emitConstantPoolUTF8("(S)Ljava/lang/Short;");
asm.emitConstantPoolNameAndType(valueOfIdx, asm.cpi());
asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi());
shortCtorIdx = asm.cpi();
shortBoxIdx = asm.cpi();
asm.emitConstantPoolUTF8("shortValue");
asm.emitConstantPoolUTF8("()S");
asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi());
@ -515,23 +522,23 @@ class AccessorGenerator implements ClassFileConstants {
throw new InternalError("Should have found primitive type");
}
protected short ctorIndexForPrimitiveType(Class<?> type) {
protected short boxingMethodForPrimitiveType(Class<?> type) {
if (type == Boolean.TYPE) {
return booleanCtorIdx;
return booleanBoxIdx;
} else if (type == Byte.TYPE) {
return byteCtorIdx;
return byteBoxIdx;
} else if (type == Character.TYPE) {
return characterCtorIdx;
return characterBoxIdx;
} else if (type == Double.TYPE) {
return doubleCtorIdx;
return doubleBoxIdx;
} else if (type == Float.TYPE) {
return floatCtorIdx;
return floatBoxIdx;
} else if (type == Integer.TYPE) {
return integerCtorIdx;
return integerBoxIdx;
} else if (type == Long.TYPE) {
return longCtorIdx;
return longBoxIdx;
} else if (type == Short.TYPE) {
return shortCtorIdx;
return shortBoxIdx;
}
throw new InternalError("Should have found primitive type");
}

View File

@ -660,9 +660,9 @@ class MethodAccessorGenerator extends AccessorGenerator {
if (!isConstructor) {
// Box return value if necessary
if (isPrimitive(returnType)) {
cb.opc_invokespecial(ctorIndexForPrimitiveType(returnType),
typeSizeInStackSlots(returnType),
0);
cb.opc_invokestatic(boxingMethodForPrimitiveType(returnType),
typeSizeInStackSlots(returnType),
0);
} else if (returnType == Void.TYPE) {
cb.opc_aconst_null();
}

View File

@ -33,7 +33,7 @@ class UnsafeBooleanFieldAccessorImpl extends UnsafeFieldAccessorImpl {
}
public Object get(Object obj) throws IllegalArgumentException {
return new Boolean(getBoolean(obj));
return Boolean.valueOf(getBoolean(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeByteFieldAccessorImpl extends UnsafeFieldAccessorImpl {
}
public Object get(Object obj) throws IllegalArgumentException {
return new Byte(getByte(obj));
return Byte.valueOf(getByte(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeCharacterFieldAccessorImpl extends UnsafeFieldAccessorImpl {
}
public Object get(Object obj) throws IllegalArgumentException {
return new Character(getChar(obj));
return Character.valueOf(getChar(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeDoubleFieldAccessorImpl extends UnsafeFieldAccessorImpl {
}
public Object get(Object obj) throws IllegalArgumentException {
return new Double(getDouble(obj));
return Double.valueOf(getDouble(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeFloatFieldAccessorImpl extends UnsafeFieldAccessorImpl {
}
public Object get(Object obj) throws IllegalArgumentException {
return new Float(getFloat(obj));
return Float.valueOf(getFloat(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeIntegerFieldAccessorImpl extends UnsafeFieldAccessorImpl {
}
public Object get(Object obj) throws IllegalArgumentException {
return new Integer(getInt(obj));
return Integer.valueOf(getInt(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeLongFieldAccessorImpl extends UnsafeFieldAccessorImpl {
}
public Object get(Object obj) throws IllegalArgumentException {
return new Long(getLong(obj));
return Long.valueOf(getLong(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedBooleanFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Boolean(getBoolean(obj));
return Boolean.valueOf(getBoolean(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedByteFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Byte(getByte(obj));
return Byte.valueOf(getByte(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedCharacterFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Character(getChar(obj));
return Character.valueOf(getChar(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedDoubleFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Double(getDouble(obj));
return Double.valueOf(getDouble(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedFloatFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Float(getFloat(obj));
return Float.valueOf(getFloat(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedIntegerFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Integer(getInt(obj));
return Integer.valueOf(getInt(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedLongFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Long(getLong(obj));
return Long.valueOf(getLong(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedShortFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Short(getShort(obj));
return Short.valueOf(getShort(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedStaticBooleanFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Boolean(getBoolean(obj));
return Boolean.valueOf(getBoolean(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedStaticByteFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Byte(getByte(obj));
return Byte.valueOf(getByte(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedStaticCharacterFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Character(getChar(obj));
return Character.valueOf(getChar(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedStaticDoubleFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Double(getDouble(obj));
return Double.valueOf(getDouble(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedStaticFloatFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Float(getFloat(obj));
return Float.valueOf(getFloat(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedStaticIntegerFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Integer(getInt(obj));
return Integer.valueOf(getInt(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedStaticLongFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Long(getLong(obj));
return Long.valueOf(getLong(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -35,7 +35,7 @@ class UnsafeQualifiedStaticShortFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Short(getShort(obj));
return Short.valueOf(getShort(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeShortFieldAccessorImpl extends UnsafeFieldAccessorImpl {
}
public Object get(Object obj) throws IllegalArgumentException {
return new Short(getShort(obj));
return Short.valueOf(getShort(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeStaticBooleanFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Boolean(getBoolean(obj));
return Boolean.valueOf(getBoolean(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeStaticByteFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl {
}
public Object get(Object obj) throws IllegalArgumentException {
return new Byte(getByte(obj));
return Byte.valueOf(getByte(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeStaticCharacterFieldAccessorImpl extends UnsafeStaticFieldAccessorIm
}
public Object get(Object obj) throws IllegalArgumentException {
return new Character(getChar(obj));
return Character.valueOf(getChar(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeStaticDoubleFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Double(getDouble(obj));
return Double.valueOf(getDouble(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeStaticFloatFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl {
}
public Object get(Object obj) throws IllegalArgumentException {
return new Float(getFloat(obj));
return Float.valueOf(getFloat(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeStaticIntegerFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl
}
public Object get(Object obj) throws IllegalArgumentException {
return new Integer(getInt(obj));
return Integer.valueOf(getInt(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeStaticLongFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl {
}
public Object get(Object obj) throws IllegalArgumentException {
return new Long(getLong(obj));
return Long.valueOf(getLong(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -33,7 +33,7 @@ class UnsafeStaticShortFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl {
}
public Object get(Object obj) throws IllegalArgumentException {
return new Short(getShort(obj));
return Short.valueOf(getShort(obj));
}
public boolean getBoolean(Object obj) throws IllegalArgumentException {

View File

@ -0,0 +1,173 @@
/*
* Copyright (c) 2014, 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 5043030
* @summary Verify that the method java.lang.reflect.Field.get(Object) makes
* use of the same caching mechanism as used for autoboxing
* when wrapping values of the primitive types.
* @author Andrej Golovnin
*/
import java.lang.reflect.Field;
public class TestFieldReflectValueOf {
@SuppressWarnings("unused")
private static boolean booleanStaticField;
@SuppressWarnings("unused")
private static byte byteStaticField;
@SuppressWarnings("unused")
private static char charStaticField;
@SuppressWarnings("unused")
private static int intStaticField;
@SuppressWarnings("unused")
private static long longStaticField;
@SuppressWarnings("unused")
private static short shortStaticField;
@SuppressWarnings("unused")
private static volatile boolean booleanStaticVolatileField;
@SuppressWarnings("unused")
private static volatile byte byteStaticVolatileField;
@SuppressWarnings("unused")
private static volatile char charStaticVolatileField;
@SuppressWarnings("unused")
private static volatile int intStaticVolatileField;
@SuppressWarnings("unused")
private static volatile long longStaticVolatileField;
@SuppressWarnings("unused")
private static volatile short shortStaticVolatileField;
@SuppressWarnings("unused")
private boolean booleanField;
@SuppressWarnings("unused")
private byte byteField;
@SuppressWarnings("unused")
private char charField;
@SuppressWarnings("unused")
private int intField;
@SuppressWarnings("unused")
private long longField;
@SuppressWarnings("unused")
private short shortField;
@SuppressWarnings("unused")
private volatile boolean booleanVolatileField;
@SuppressWarnings("unused")
private volatile byte byteVolatileField;
@SuppressWarnings("unused")
private volatile char charVolatileField;
@SuppressWarnings("unused")
private volatile int intVolatileField;
@SuppressWarnings("unused")
private volatile long longVolatileField;
@SuppressWarnings("unused")
private volatile short shortVolatileField;
public static void main(String[] args) {
testUnsafeStaticFieldAccessors();
testUnsafeQualifiedStaticFieldAccessors();
testUnsafeFieldAccessors();
testUnsafeQualifiedFieldAccessors();
}
private static void testUnsafeStaticFieldAccessors() {
testFieldAccessors(true, false);
}
private static void testUnsafeQualifiedStaticFieldAccessors() {
testFieldAccessors(true, true);
}
private static void testUnsafeFieldAccessors() {
testFieldAccessors(false, false);
}
private static void testUnsafeQualifiedFieldAccessors() {
testFieldAccessors(false, true);
}
private static void testFieldAccessors(boolean checkStatic,
boolean checkVolatile)
{
// Boolean#valueOf test
testField(Boolean.TYPE, Boolean.FALSE, checkStatic, checkVolatile);
testField(Boolean.TYPE, Boolean.TRUE, checkStatic, checkVolatile);
// Byte#valueOf test
for (int b = Byte.MIN_VALUE; b < (Byte.MAX_VALUE + 1); b++) {
testField(Byte.TYPE, Byte.valueOf((byte) b), checkStatic, checkVolatile);
}
// Character#valueOf test
for (char c = '\u0000'; c <= '\u007F'; c++) {
testField(Character.TYPE, Character.valueOf(c), checkStatic, checkVolatile);
}
// Integer#valueOf test
for (int i = -128; i <= 127; i++) {
testField(Integer.TYPE, Integer.valueOf(i), checkStatic, checkVolatile);
}
// Long#valueOf test
for (long l = -128L; l <= 127L; l++) {
testField(Long.TYPE, Long.valueOf(l), checkStatic, checkVolatile);
}
// Short#valueOf test
for (short s = -128; s <= 127; s++) {
testField(Short.TYPE, Short.valueOf(s), checkStatic, checkVolatile);
}
}
private static void testField(Class<?> primType, Object wrappedValue,
boolean checkStatic, boolean checkVolatile)
{
String fieldName = primType.getName();
if (checkStatic) {
fieldName += "Static";
}
if (checkVolatile) {
fieldName += "Volatile";
}
fieldName += "Field";
try {
Field field = TestFieldReflectValueOf.class.getDeclaredField(fieldName);
field.setAccessible(true);
TestFieldReflectValueOf obj = new TestFieldReflectValueOf();
field.set(obj, wrappedValue);
Object result = field.get(obj);
if (result != wrappedValue) {
throw new RuntimeException("The value " + wrappedValue
+ " is not cached for the type " + primType);
}
} catch ( NoSuchFieldException | SecurityException
| IllegalAccessException | IllegalArgumentException e)
{
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,131 @@
/*
* Copyright (c) 2014, 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 5043030
* @summary Verify that the method java.lang.reflect.Method.invoke(Object, Object...)
* makes use of the same caching mechanism as used for autoboxing
* when wrapping returned values of the primitive types.
* @author Andrej Golovnin
* @run main/othervm -Dsun.reflect.noInflation=true TestMethodReflectValueOf
* @run main/othervm -Dsun.reflect.noInflation=false -Dsun.reflect.inflationThreshold=500 TestMethodReflectValueOf
*/
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestMethodReflectValueOf {
public static void main(String[] args) {
// When the inflation is disabled we compare values using "=="
// as the returned values of the primitive types should be cached
// by the same mechanism as used for autoboxing. When the inflation
// is enabled we use "equals()"-method to compare values as the native
// code still creates new instances to wrap values of the primitive
// types.
boolean checkIdentity = Boolean.getBoolean("sun.reflect.noInflation");
// Boolean#valueOf test
testMethod(Boolean.TYPE, Boolean.FALSE, checkIdentity);
testMethod(Boolean.TYPE, Boolean.TRUE, checkIdentity);
// Byte#valueOf test
for (int b = Byte.MIN_VALUE; b < (Byte.MAX_VALUE + 1); b++) {
testMethod(Byte.TYPE, Byte.valueOf((byte) b), checkIdentity);
}
// Character#valueOf test
for (char c = '\u0000'; c <= '\u007F'; c++) {
testMethod(Character.TYPE, Character.valueOf(c), checkIdentity);
}
// Integer#valueOf test
for (int i = -128; i <= 127; i++) {
testMethod(Integer.TYPE, Integer.valueOf(i), checkIdentity);
}
// Long#valueOf test
for (long l = -128L; l <= 127L; l++) {
testMethod(Long.TYPE, Long.valueOf(l), checkIdentity);
}
// Short#valueOf test
for (short s = -128; s <= 127; s++) {
testMethod(Short.TYPE, Short.valueOf(s), checkIdentity);
}
}
public static void testMethod(Class<?> primType, Object wrappedValue,
boolean checkIdentity)
{
String methodName = primType.getName() + "Method";
try {
Method method = TestMethodReflectValueOf.class.getMethod(methodName, primType);
Object result = method.invoke(new TestMethodReflectValueOf(), wrappedValue);
if (checkIdentity) {
if (result != wrappedValue) {
throw new RuntimeException("The value " + wrappedValue
+ " is not cached for the type " + primType);
}
} else {
if (!result.equals(wrappedValue)) {
throw new RuntimeException("The result value " + result
+ " is not equal to the expected value "
+ wrappedValue + " for the type " + primType);
}
}
} catch ( NoSuchMethodException | SecurityException
| IllegalAccessException | IllegalArgumentException
| InvocationTargetException e)
{
throw new RuntimeException(e);
}
}
public int intMethod(int value) {
return value;
}
public long longMethod(long value) {
return value;
}
public short shortMethod(short value) {
return value;
}
public byte byteMethod(byte value) {
return value;
}
public char charMethod(char value) {
return value;
}
public boolean booleanMethod(boolean value) {
return value;
}
}