1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.bcel.internal.generic;
  22 
  23 
  24 /**
  25  * LDC2_W - Push long or double from constant pool
  26  *
  27  * <PRE>Stack: ... -&gt; ..., item.word1, item.word2</PRE>
  28  *
  29  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  30  */
  31 public class LDC2_W extends CPInstruction
  32   implements PushInstruction, TypedInstruction {
  33   /**
  34    * Empty constructor needed for the Class.newInstance() statement in
  35    * Instruction.readInstruction(). Not to be used otherwise.
  36    */
  37   LDC2_W() {}
  38 
  39   public LDC2_W(int index) {
  40     super(com.sun.org.apache.bcel.internal.Constants.LDC2_W, index);
  41   }
  42 
  43   public Type getType(ConstantPoolGen cpg) {
  44     switch(cpg.getConstantPool().getConstant(index).getTag()) {
  45     case com.sun.org.apache.bcel.internal.Constants.CONSTANT_Long:   return Type.LONG;
  46     case com.sun.org.apache.bcel.internal.Constants.CONSTANT_Double: return Type.DOUBLE;
  47     default: // Never reached
  48       throw new RuntimeException("Unknown constant type " + opcode);
  49     }
  50   }
  51 
  52   public Number getValue(ConstantPoolGen cpg) {
  53     com.sun.org.apache.bcel.internal.classfile.Constant c = cpg.getConstantPool().getConstant(index);
  54 
  55     switch(c.getTag()) {
  56     case com.sun.org.apache.bcel.internal.Constants.CONSTANT_Long:
  57         return Long.valueOf(((com.sun.org.apache.bcel.internal.classfile.ConstantLong)c).getBytes());
  58 
  59     case com.sun.org.apache.bcel.internal.Constants.CONSTANT_Double:
  60         return Double.valueOf(((com.sun.org.apache.bcel.internal.classfile.ConstantDouble)c).getBytes());
  61 
  62     default: // Never reached
  63       throw new RuntimeException("Unknown or invalid constant type at " + index);
  64       }
  65   }
  66 
  67   /**
  68    * Call corresponding visitor method(s). The order is:
  69    * Call visitor methods of implemented interfaces first, then
  70    * call methods according to the class hierarchy in descending order,
  71    * i.e., the most specific visitXXX() call comes last.
  72    *
  73    * @param v Visitor object
  74    */
  75   public void accept(Visitor v) {
  76     v.visitStackProducer(this);
  77     v.visitPushInstruction(this);
  78     v.visitTypedInstruction(this);
  79     v.visitCPInstruction(this);
  80     v.visitLDC2_W(this);
  81   }
  82 }