Skip to content

Commit 4fd8675

Browse files
committed
Remove redundant object allocation in cglib proxy method calls
* Fixes gh-35542
1 parent 5df0821 commit 4fd8675

File tree

1 file changed

+51
-18
lines changed

1 file changed

+51
-18
lines changed

spring-core/src/main/java/org/springframework/cglib/core/CodeEmitter.java

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929
public class CodeEmitter extends LocalVariablesSorter {
3030
private static final Signature BOOLEAN_VALUE =
3131
TypeUtils.parseSignature("boolean booleanValue()");
32+
private static final Signature BOOLEAN_VALUE_OF =
33+
TypeUtils.parseSignature("Boolean valueOf(boolean)");
34+
private static final Signature INTEGER_VALUE_OF =
35+
TypeUtils.parseSignature("Integer valueOf(int)");
36+
private static final Signature LONG_VALUE_OF =
37+
TypeUtils.parseSignature("Long valueOf(long)");
38+
private static final Signature FLOAT_VALUE_OF =
39+
TypeUtils.parseSignature("Float valueOf(float)");
40+
private static final Signature DOUBLE_VALUE_OF =
41+
TypeUtils.parseSignature("Double valueOf(double)");
42+
private static final Signature CHARACTER_VALUE_OF =
43+
TypeUtils.parseSignature("Character valueOf(char)");
3244
private static final Signature CHAR_VALUE =
3345
TypeUtils.parseSignature("char charValue()");
3446
private static final Signature LONG_VALUE =
@@ -705,29 +717,50 @@ public void throw_exception(Type type, String msg) {
705717

706718
/**
707719
* If the argument is a primitive class, replaces the primitive value
708-
* on the top of the stack with the wrapped (Object) equivalent. For
709-
* example, char -> Character.
720+
* on the top of the stack with the wrapped (Object) equivalent using valueOf() methods.
721+
* For example, char -> Character.
710722
* If the class is Void, a null is pushed onto the stack instead.
711723
* @param type the class indicating the current type of the top stack value
712724
*/
713725
public void box(Type type) {
714726
if (TypeUtils.isPrimitive(type)) {
715-
if (type == Type.VOID_TYPE) {
716-
aconst_null();
717-
} else {
718-
Type boxed = TypeUtils.getBoxedType(type);
719-
new_instance(boxed);
720-
if (type.getSize() == 2) {
721-
// Pp -> Ppo -> oPpo -> ooPpo -> ooPp -> o
722-
dup_x2();
723-
dup_x2();
724-
pop();
725-
} else {
726-
// p -> po -> opo -> oop -> o
727-
dup_x1();
728-
swap();
729-
}
730-
invoke_constructor(boxed, new Signature(Constants.CONSTRUCTOR_NAME, Type.VOID_TYPE, new Type[]{ type }));
727+
switch (type.getSort()) {
728+
case Type.VOID:
729+
aconst_null();
730+
break;
731+
case Type.BOOLEAN:
732+
invoke_static(Constants.TYPE_BOOLEAN, BOOLEAN_VALUE_OF);
733+
break;
734+
case Type.INT:
735+
invoke_static(Constants.TYPE_INTEGER, INTEGER_VALUE_OF);
736+
break;
737+
case Type.LONG:
738+
invoke_static(Constants.TYPE_LONG, LONG_VALUE_OF);
739+
break;
740+
case Type.FLOAT:
741+
invoke_static(Constants.TYPE_FLOAT, FLOAT_VALUE_OF);
742+
break;
743+
case Type.DOUBLE:
744+
invoke_static(Constants.TYPE_DOUBLE, DOUBLE_VALUE_OF);
745+
break;
746+
case Type.CHAR:
747+
invoke_static(Constants.TYPE_CHARACTER, CHARACTER_VALUE_OF);
748+
break;
749+
750+
default:
751+
Type boxed = TypeUtils.getBoxedType(type);
752+
new_instance(boxed);
753+
if (type.getSize() == 2) {
754+
// Pp -> Ppo -> oPpo -> ooPpo -> ooPp -> o
755+
dup_x2();
756+
dup_x2();
757+
pop();
758+
} else {
759+
// p -> po -> opo -> oop -> o
760+
dup_x1();
761+
swap();
762+
}
763+
invoke_constructor(boxed, new Signature(Constants.CONSTRUCTOR_NAME, Type.VOID_TYPE, new Type[]{ type }));
731764
}
732765
}
733766
}

0 commit comments

Comments
 (0)