1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.generic;
  23 
  24 
  25 import com.sun.org.apache.bcel.internal.classfile.ConstantPool;
  26 import com.sun.org.apache.bcel.internal.classfile.ConstantUtf8;
  27 import com.sun.org.apache.bcel.internal.classfile.ConstantNameAndType;
  28 import com.sun.org.apache.bcel.internal.classfile.ConstantCP;
  29 import com.sun.org.apache.bcel.internal.classfile.*;
  30 
  31 /**
  32  * Super class for the GET/PUTxxx family of instructions.
  33  *
  34  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  35  */
  36 public abstract class FieldInstruction extends FieldOrMethod
  37   implements TypedInstruction {
  38   /**
  39    * Empty constructor needed for the Class.newInstance() statement in
  40    * Instruction.readInstruction(). Not to be used otherwise.
  41    */
  42   FieldInstruction() {}
  43 
  44   /**
  45    * @param index to constant pool
  46    */
  47   protected FieldInstruction(short opcode, int index) {
  48     super(opcode, index);
  49   }
  50 
  51   /**
  52    * @return mnemonic for instruction with symbolic references resolved
  53    */
  54   public String toString(ConstantPool cp) {
  55     return com.sun.org.apache.bcel.internal.Constants.OPCODE_NAMES[opcode] + " " +
  56       cp.constantToString(index, com.sun.org.apache.bcel.internal.Constants.CONSTANT_Fieldref);
  57   }
  58 
  59   /** @return size of field (1 or 2)
  60    */
  61   protected int getFieldSize(ConstantPoolGen cpg) {
  62     return getType(cpg).getSize();
  63   }
  64 
  65   /** @return return type of referenced field
  66    */
  67   public Type getType(ConstantPoolGen cpg) {
  68     return getFieldType(cpg);
  69   }
  70 
  71   /** @return type of field
  72    */
  73   public Type getFieldType(ConstantPoolGen cpg) {
  74     return Type.getType(getSignature(cpg));
  75   }
  76 
  77   /** @return name of referenced field.
  78    */
  79   public String getFieldName(ConstantPoolGen cpg) {
  80     return getName(cpg);
  81   }
  82 }