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