|
29 | 29 | public class CodeEmitter extends LocalVariablesSorter {
|
30 | 30 | private static final Signature BOOLEAN_VALUE =
|
31 | 31 | 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)"); |
32 | 44 | private static final Signature CHAR_VALUE =
|
33 | 45 | TypeUtils.parseSignature("char charValue()");
|
34 | 46 | private static final Signature LONG_VALUE =
|
@@ -705,29 +717,50 @@ public void throw_exception(Type type, String msg) {
|
705 | 717 |
|
706 | 718 | /**
|
707 | 719 | * 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. |
710 | 722 | * If the class is Void, a null is pushed onto the stack instead.
|
711 | 723 | * @param type the class indicating the current type of the top stack value
|
712 | 724 | */
|
713 | 725 | public void box(Type type) {
|
714 | 726 | 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 })); |
731 | 764 | }
|
732 | 765 | }
|
733 | 766 | }
|
|
0 commit comments