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 * @param index to constant pool
36 */
37 protected FieldInstruction(final short opcode, final int index) {
38 super(opcode, index);
39 }
40
41 /**
42 * @return name of referenced field.
43 */
44 public String getFieldName(final ConstantPoolGen cpg) {
45 return getName(cpg);
46 }
47
48 /**
49 * @return size of field (1 or 2)
50 */
51 protected int getFieldSize(final ConstantPoolGen cpg) {
52 return Type.size(Type.getTypeSize(getSignature(cpg)));
53 }
54
55 /**
56 * @return type of field
57 */
58 public Type getFieldType(final ConstantPoolGen cpg) {
59 return Type.getType(getSignature(cpg));
60 }
61
62 /**
63 * @return return type of referenced field
64 */
65 @Override
66 public Type getType(final ConstantPoolGen cpg) {
67 return getFieldType(cpg);
68 }
69
70 /**
71 * @return mnemonic for instruction with symbolic references resolved
72 */
73 @Override
74 public String toString(final ConstantPool cp) {
75 return org.apache.bcel.Const.getOpcodeName(super.getOpcode()) + " " + cp.constantToString(super.getIndex(), org.apache.bcel.Const.CONSTANT_Fieldref);
76 }
77 }