1 17 package org.apache.bcel.generic; 18 19 import org.apache.bcel.Constants; 20 21 28 public final class PUSH implements CompoundInstruction, VariableLengthInstruction, 29 InstructionConstants { 30 31 private Instruction instruction; 32 33 34 40 public PUSH(ConstantPoolGen cp, int value) { 41 if ((value >= -1) && (value <= 5)) { 42 instruction = INSTRUCTIONS[Constants.ICONST_0 + value]; 43 } else if ((value >= -128) && (value <= 127)) { 44 instruction = new BIPUSH((byte) value); 45 } else if ((value >= -32768) && (value <= 32767)) { 46 instruction = new SIPUSH((short) value); 47 } else { 48 instruction = new LDC(cp.addInteger(value)); 49 } 50 } 51 52 53 57 public PUSH(ConstantPoolGen cp, boolean value) { 58 instruction = INSTRUCTIONS[Constants.ICONST_0 + (value ? 1 : 0)]; 59 } 60 61 62 66 public PUSH(ConstantPoolGen cp, float value) { 67 if (value == 0.0) { 68 instruction = FCONST_0; 69 } else if (value == 1.0) { 70 instruction = FCONST_1; 71 } else if (value == 2.0) { 72 instruction = FCONST_2; 73 } else { 74 instruction = new LDC(cp.addFloat(value)); 75 } 76 } 77 78 79 83 public PUSH(ConstantPoolGen cp, long value) { 84 if (value == 0) { 85 instruction = LCONST_0; 86 } else if (value == 1) { 87 instruction = LCONST_1; 88 } else { 89 instruction = new LDC2_W(cp.addLong(value)); 90 } 91 } 92 93 94 98 public PUSH(ConstantPoolGen cp, double value) { 99 if (value == 0.0) { 100 instruction = DCONST_0; 101 } else if (value == 1.0) { 102 instruction = DCONST_1; 103 } else { 104 instruction = new LDC2_W(cp.addDouble(value)); 105 } 106 } 107 108 109 113 public PUSH(ConstantPoolGen cp, String value) { 114 if (value == null) { 115 instruction = ACONST_NULL; 116 } else { 117 instruction = new LDC(cp.addString(value)); 118 } 119 } 120 121 122 126 public PUSH(ConstantPoolGen cp, Number value) { 127 if ((value instanceof Integer ) || (value instanceof Short ) || (value instanceof Byte )) { 128 instruction = new PUSH(cp, value.intValue()).instruction; 129 } else if (value instanceof Double ) { 130 instruction = new PUSH(cp, value.doubleValue()).instruction; 131 } else if (value instanceof Float ) { 132 instruction = new PUSH(cp, value.floatValue()).instruction; 133 } else if (value instanceof Long ) { 134 instruction = new PUSH(cp, value.longValue()).instruction; 135 } else { 136 throw new ClassGenException("What's this: " + value); 137 } 138 } 139 140 141 148 public PUSH(ConstantPoolGen cp, Character value) { 149 this(cp, value.charValue()); 150 } 151 152 153 157 public PUSH(ConstantPoolGen cp, Boolean value) { 158 this(cp, value.booleanValue()); 159 } 160 161 162 public final InstructionList getInstructionList() { 163 return new InstructionList(instruction); 164 } 165 166 167 public final Instruction getInstruction() { 168 return instruction; 169 } 170 171 172 175 public String toString() { 176 return instruction.toString() + " (PUSH)"; 177 } 178 } 179 | Popular Tags |