View Javadoc
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  
19  import org.apache.bcel.Const;
20  import org.apache.bcel.classfile.ConstantCP;
21  import org.apache.bcel.classfile.ConstantNameAndType;
22  import org.apache.bcel.classfile.ConstantPool;
23  import org.apache.bcel.classfile.ConstantUtf8;
24  import org.apache.bcel.classfile.Utility;
25  
26  /**
27   * Super class for InvokeInstruction and FieldInstruction, since they have some methods in common!
28   */
29  public abstract class FieldOrMethod extends CPInstruction implements LoadClass {
30  
31      /**
32       * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
33       */
34      FieldOrMethod() {
35          // no init
36      }
37  
38      /**
39       * @param index to constant pool
40       */
41      protected FieldOrMethod(final short opcode, final int index) {
42          super(opcode, index);
43      }
44  
45      /**
46       * @return name of the referenced class/interface
47       * @deprecated If the instruction references an array class, this method will return "java.lang.Object". For code
48       *             generated by Java 1.5, this answer is sometimes wrong (e.g., if the "clone()" method is called on an
49       *             array). A better idea is to use the {@link #getReferenceType(ConstantPoolGen)} method, which correctly
50       *             distinguishes between class types and array types.
51       */
52      @Deprecated
53      public String getClassName(final ConstantPoolGen cpg) {
54          final ConstantPool cp = cpg.getConstantPool();
55          final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
56          final String className = cp.getConstantString(cmr.getClassIndex(), Const.CONSTANT_Class);
57          if (className.startsWith("[")) {
58              // Turn array classes into java.lang.Object.
59              return "java.lang.Object";
60          }
61          return Utility.pathToPackage(className);
62      }
63  
64      /**
65       * @return type of the referenced class/interface
66       * @deprecated If the instruction references an array class, the ObjectType returned will be invalid. Use
67       *             getReferenceType() instead.
68       */
69      @Deprecated
70      public ObjectType getClassType(final ConstantPoolGen cpg) {
71          return ObjectType.getInstance(getClassName(cpg));
72      }
73  
74      /**
75       * Gets the ObjectType of the method return or field.
76       *
77       * @return type of the referenced class/interface
78       * @throws ClassGenException when the field is (or method returns) an array,
79       */
80      @Override
81      public ObjectType getLoadClassType(final ConstantPoolGen cpg) {
82          final ReferenceType rt = getReferenceType(cpg);
83          if (rt instanceof ObjectType) {
84              return (ObjectType) rt;
85          }
86          if (rt instanceof ArrayType) {
87              return Type.OBJECT;
88          }
89          throw new ClassGenException(rt.getClass().getCanonicalName() + " " + rt.getSignature() + " does not represent an ObjectType");
90      }
91  
92      /**
93       * @return name of referenced method/field.
94       */
95      public String getName(final ConstantPoolGen cpg) {
96          final ConstantPool cp = cpg.getConstantPool();
97          final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
98          final ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
99          return ((ConstantUtf8) cp.getConstant(cnat.getNameIndex())).getBytes();
100     }
101 
102     /**
103      * Gets the reference type representing the class, interface, or array class referenced by the instruction.
104      *
105      * @param cpg the ConstantPoolGen used to create the instruction
106      * @return an ObjectType (if the referenced class type is a class or interface), or an ArrayType (if the referenced
107      *         class type is an array class)
108      */
109     public ReferenceType getReferenceType(final ConstantPoolGen cpg) {
110         final ConstantPool cp = cpg.getConstantPool();
111         final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
112         String className = cp.getConstantString(cmr.getClassIndex(), Const.CONSTANT_Class);
113         if (className.startsWith("[")) {
114             return (ArrayType) Type.getType(className);
115         }
116         className = Utility.pathToPackage(className);
117         return ObjectType.getInstance(className);
118     }
119 
120     /**
121      * @return signature of referenced method/field.
122      */
123     public String getSignature(final ConstantPoolGen cpg) {
124         final ConstantPool cp = cpg.getConstantPool();
125         final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
126         final ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
127         return ((ConstantUtf8) cp.getConstant(cnat.getSignatureIndex())).getBytes();
128     }
129 }