001: /*
002: * Copyright 2000-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package org.apache.bcel.generic;
018:
019: import org.apache.bcel.Constants;
020:
021: /**
022: * Wrapper class for push operations, which are implemented either as BIPUSH,
023: * LDC or xCONST_n instructions.
024: *
025: * @version $Id: PUSH.java 386056 2006-03-15 11:31:56Z tcurdt $
026: * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
027: */
028: public final class PUSH implements CompoundInstruction,
029: VariableLengthInstruction, InstructionConstants {
030:
031: private Instruction instruction;
032:
033: /**
034: * This constructor also applies for values of type short, char, byte
035: *
036: * @param cp Constant pool
037: * @param value to be pushed
038: */
039: public PUSH(ConstantPoolGen cp, int value) {
040: if ((value >= -1) && (value <= 5)) {
041: instruction = INSTRUCTIONS[Constants.ICONST_0 + value];
042: } else if ((value >= -128) && (value <= 127)) {
043: instruction = new BIPUSH((byte) value);
044: } else if ((value >= -32768) && (value <= 32767)) {
045: instruction = new SIPUSH((short) value);
046: } else {
047: instruction = new LDC(cp.addInteger(value));
048: }
049: }
050:
051: /**
052: * @param cp Constant pool
053: * @param value to be pushed
054: */
055: public PUSH(ConstantPoolGen cp, boolean value) {
056: instruction = INSTRUCTIONS[Constants.ICONST_0 + (value ? 1 : 0)];
057: }
058:
059: /**
060: * @param cp Constant pool
061: * @param value to be pushed
062: */
063: public PUSH(ConstantPoolGen cp, float value) {
064: if (value == 0.0) {
065: instruction = FCONST_0;
066: } else if (value == 1.0) {
067: instruction = FCONST_1;
068: } else if (value == 2.0) {
069: instruction = FCONST_2;
070: } else {
071: instruction = new LDC(cp.addFloat(value));
072: }
073: }
074:
075: /**
076: * @param cp Constant pool
077: * @param value to be pushed
078: */
079: public PUSH(ConstantPoolGen cp, long value) {
080: if (value == 0) {
081: instruction = LCONST_0;
082: } else if (value == 1) {
083: instruction = LCONST_1;
084: } else {
085: instruction = new LDC2_W(cp.addLong(value));
086: }
087: }
088:
089: /**
090: * @param cp Constant pool
091: * @param value to be pushed
092: */
093: public PUSH(ConstantPoolGen cp, double value) {
094: if (value == 0.0) {
095: instruction = DCONST_0;
096: } else if (value == 1.0) {
097: instruction = DCONST_1;
098: } else {
099: instruction = new LDC2_W(cp.addDouble(value));
100: }
101: }
102:
103: /**
104: * @param cp Constant pool
105: * @param value to be pushed
106: */
107: public PUSH(ConstantPoolGen cp, String value) {
108: if (value == null) {
109: instruction = ACONST_NULL;
110: } else {
111: instruction = new LDC(cp.addString(value));
112: }
113: }
114:
115: /**
116: * @param cp Constant pool
117: * @param value to be pushed
118: */
119: public PUSH(ConstantPoolGen cp, Number value) {
120: if ((value instanceof Integer) || (value instanceof Short)
121: || (value instanceof Byte)) {
122: instruction = new PUSH(cp, value.intValue()).instruction;
123: } else if (value instanceof Double) {
124: instruction = new PUSH(cp, value.doubleValue()).instruction;
125: } else if (value instanceof Float) {
126: instruction = new PUSH(cp, value.floatValue()).instruction;
127: } else if (value instanceof Long) {
128: instruction = new PUSH(cp, value.longValue()).instruction;
129: } else {
130: throw new ClassGenException("What's this: " + value);
131: }
132: }
133:
134: /**
135: * creates a push object from a Character value. Warning: Make sure not to attempt to allow
136: * autoboxing to create this value parameter, as an alternative constructor will be called
137: *
138: * @param cp Constant pool
139: * @param value to be pushed
140: */
141: public PUSH(ConstantPoolGen cp, Character value) {
142: this (cp, value.charValue());
143: }
144:
145: /**
146: * @param cp Constant pool
147: * @param value to be pushed
148: */
149: public PUSH(ConstantPoolGen cp, Boolean value) {
150: this (cp, value.booleanValue());
151: }
152:
153: public final InstructionList getInstructionList() {
154: return new InstructionList(instruction);
155: }
156:
157: public final Instruction getInstruction() {
158: return instruction;
159: }
160:
161: /**
162: * @return mnemonic for instruction
163: */
164: public String toString() {
165: return instruction.toString() + " (PUSH)";
166: }
167: }
|