PUSH.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.generic;

  18. import java.util.Objects;

  19. import org.apache.bcel.Const;

  20. /**
  21.  * Wrapper class for push operations, which are implemented either as BIPUSH, LDC or xCONST_n instructions.
  22.  */
  23. public final class PUSH implements CompoundInstruction, VariableLengthInstruction, InstructionConstants {

  24.     private final Instruction instruction;

  25.     /**
  26.      * Pushes an array type constant, for example {@code int[].class}, {@code String[].class}, and so on.
  27.      *
  28.      * @param cp generated constant pool.
  29.      * @param value to be pushed.
  30.      * @since 6.7.0
  31.      */
  32.     public PUSH(final ConstantPoolGen cp, final ArrayType value) {
  33.         if (value == null) {
  34.             instruction = InstructionConst.ACONST_NULL;
  35.         } else {
  36.             instruction = new LDC(cp.addArrayClass(value));
  37.         }
  38.     }

  39.    /**
  40.      * @param cp Constant pool
  41.      * @param value to be pushed
  42.      */
  43.     public PUSH(final ConstantPoolGen cp, final boolean value) {
  44.         Objects.requireNonNull(cp, "cp");
  45.         instruction = InstructionConst.getInstruction(Const.ICONST_0 + (value ? 1 : 0));
  46.     }

  47.     /**
  48.      * @param cp Constant pool
  49.      * @param value to be pushed
  50.      */
  51.     public PUSH(final ConstantPoolGen cp, final Boolean value) {
  52.         this(cp, value.booleanValue());
  53.     }

  54.     /**
  55.      * creates a push object from a Character value. Warning: Make sure not to attempt to allow autoboxing to create this
  56.      * value parameter, as an alternative constructor will be called
  57.      *
  58.      * @param cp Constant pool
  59.      * @param value to be pushed
  60.      */
  61.     public PUSH(final ConstantPoolGen cp, final Character value) {
  62.         this(cp, value.charValue());
  63.     }

  64.     /**
  65.      * @param cp Constant pool
  66.      * @param value to be pushed
  67.      */
  68.     public PUSH(final ConstantPoolGen cp, final double value) {
  69.         if (value == 0.0) {
  70.             instruction = InstructionConst.DCONST_0;
  71.         } else if (value == 1.0) {
  72.             instruction = InstructionConst.DCONST_1;
  73.         } else {
  74.             instruction = new LDC2_W(cp.addDouble(value));
  75.         }
  76.     }

  77.     /**
  78.      * @param cp Constant pool
  79.      * @param value to be pushed
  80.      */
  81.     public PUSH(final ConstantPoolGen cp, final float value) {
  82.         if (value == 0.0) {
  83.             instruction = InstructionConst.FCONST_0;
  84.         } else if (value == 1.0) {
  85.             instruction = InstructionConst.FCONST_1;
  86.         } else if (value == 2.0) {
  87.             instruction = InstructionConst.FCONST_2;
  88.         } else {
  89.             instruction = new LDC(cp.addFloat(value));
  90.         }
  91.     }

  92.     /**
  93.      * This constructor also applies for values of type short, char, byte
  94.      *
  95.      * @param cp Constant pool
  96.      * @param value to be pushed
  97.      */
  98.     public PUSH(final ConstantPoolGen cp, final int value) {
  99.         if (value >= -1 && value <= 5) {
  100.             instruction = InstructionConst.getInstruction(Const.ICONST_0 + value);
  101.         } else if (Instruction.isValidByte(value)) {
  102.             instruction = new BIPUSH((byte) value);
  103.         } else if (Instruction.isValidShort(value)) {
  104.             instruction = new SIPUSH((short) value);
  105.         } else {
  106.             instruction = new LDC(cp.addInteger(value));
  107.         }
  108.     }

  109.     /**
  110.      * @param cp Constant pool
  111.      * @param value to be pushed
  112.      */
  113.     public PUSH(final ConstantPoolGen cp, final long value) {
  114.         if (value == 0) {
  115.             instruction = InstructionConst.LCONST_0;
  116.         } else if (value == 1) {
  117.             instruction = InstructionConst.LCONST_1;
  118.         } else {
  119.             instruction = new LDC2_W(cp.addLong(value));
  120.         }
  121.     }

  122.     /**
  123.      * @param cp Constant pool
  124.      * @param value to be pushed
  125.      */
  126.     public PUSH(final ConstantPoolGen cp, final 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.      * @param cp
  142.      * @param value
  143.      * @since 6.0
  144.      */
  145.     public PUSH(final ConstantPoolGen cp, final ObjectType value) {
  146.         if (value == null) {
  147.             instruction = InstructionConst.ACONST_NULL;
  148.         } else {
  149.             instruction = new LDC(cp.addClass(value));
  150.         }
  151.     }

  152.     /**
  153.      * @param cp Constant pool
  154.      * @param value to be pushed
  155.      */
  156.     public PUSH(final ConstantPoolGen cp, final String value) {
  157.         if (value == null) {
  158.             instruction = InstructionConst.ACONST_NULL;
  159.         } else {
  160.             instruction = new LDC(cp.addString(value));
  161.         }
  162.     }

  163.     public Instruction getInstruction() {
  164.         return instruction;
  165.     }

  166.     @Override
  167.     public InstructionList getInstructionList() {
  168.         return new InstructionList(instruction);
  169.     }

  170.     /**
  171.      * @return mnemonic for instruction
  172.      */
  173.     @Override
  174.     public String toString() {
  175.         return instruction + " (PUSH)";
  176.     }
  177. }