< prev index next >

src/java.xml/share/classes/com/sun/org/apache/bcel/internal/generic/FieldOrMethod.java

Print this page




   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 import com.sun.org.apache.bcel.internal.classfile.*;




  25 
  26 /**
  27  * Super class for InvokeInstruction and FieldInstruction, since they have
  28  * some methods in common!
  29  *
  30  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  31  */
  32 public abstract class FieldOrMethod extends CPInstruction implements LoadClass {

  33   /**
  34    * Empty constructor needed for the Class.newInstance() statement in
  35    * Instruction.readInstruction(). Not to be used otherwise.
  36    */
  37   FieldOrMethod() {}


  38 
  39   /**
  40    * @param index to constant pool
  41    */
  42   protected FieldOrMethod(short opcode, int index) {
  43     super(opcode, index);
  44   }
  45 

  46   /** @return signature of referenced method/field.
  47    */
  48   public String getSignature(ConstantPoolGen cpg) {
  49     ConstantPool        cp   = cpg.getConstantPool();
  50     ConstantCP          cmr  = (ConstantCP)cp.getConstant(index);
  51     ConstantNameAndType cnat = (ConstantNameAndType)cp.getConstant(cmr.getNameAndTypeIndex());
  52 
  53     return ((ConstantUtf8)cp.getConstant(cnat.getSignatureIndex())).getBytes();
  54   }
  55 

  56   /** @return name of referenced method/field.
  57    */
  58   public String getName(ConstantPoolGen cpg) {
  59     ConstantPool        cp   = cpg.getConstantPool();
  60     ConstantCP          cmr  = (ConstantCP)cp.getConstant(index);
  61     ConstantNameAndType cnat = (ConstantNameAndType)cp.getConstant(cmr.getNameAndTypeIndex());
  62     return ((ConstantUtf8)cp.getConstant(cnat.getNameIndex())).getBytes();
  63   }
  64 
  65   /** @return name of the referenced class/interface
  66    */
  67   public String getClassName(ConstantPoolGen cpg) {
  68     ConstantPool cp  = cpg.getConstantPool();
  69     ConstantCP   cmr = (ConstantCP)cp.getConstant(index);
  70     return cp.getConstantString(cmr.getClassIndex(), com.sun.org.apache.bcel.internal.Constants.CONSTANT_Class).replace('/', '.');
  71   }
  72 
  73   /** @return type of the referenced class/interface









  74    */
  75   public ObjectType getClassType(ConstantPoolGen cpg) {
  76     return new ObjectType(getClassName(cpg));








  77   }
  78 

  79   /** @return type of the referenced class/interface



































  80    */
  81   public ObjectType getLoadClassType(ConstantPoolGen cpg) {
  82     return getClassType(cpg);





  83   }
  84 }


   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 import com.sun.org.apache.bcel.internal.Const;
  25 import com.sun.org.apache.bcel.internal.classfile.ConstantCP;
  26 import com.sun.org.apache.bcel.internal.classfile.ConstantNameAndType;
  27 import com.sun.org.apache.bcel.internal.classfile.ConstantPool;
  28 import com.sun.org.apache.bcel.internal.classfile.ConstantUtf8;
  29 
  30 /**
  31  * Super class for InvokeInstruction and FieldInstruction, since they have
  32  * some methods in common!
  33  *
  34  * @version $Id: FieldOrMethod.java 1749603 2016-06-21 20:50:19Z ggregory $
  35  */
  36 public abstract class FieldOrMethod extends CPInstruction implements LoadClass {
  37 
  38     /**
  39      * Empty constructor needed for the Class.newInstance() statement in
  40      * Instruction.readInstruction(). Not to be used otherwise.
  41      */
  42     FieldOrMethod() {
  43     }
  44 
  45 
  46     /**
  47      * @param index to constant pool
  48      */
  49     protected FieldOrMethod(final short opcode, final int index) {
  50         super(opcode, index);
  51     }
  52 
  53 
  54     /** @return signature of referenced method/field.
  55      */
  56     public String getSignature( final ConstantPoolGen cpg ) {
  57         final ConstantPool cp = cpg.getConstantPool();
  58         final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
  59         final ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
  60         return ((ConstantUtf8) cp.getConstant(cnat.getSignatureIndex())).getBytes();

  61     }
  62 
  63 
  64     /** @return name of referenced method/field.
  65      */
  66     public String getName( final ConstantPoolGen cpg ) {
  67         final ConstantPool cp = cpg.getConstantPool();
  68         final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
  69         final ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
  70         return ((ConstantUtf8) cp.getConstant(cnat.getNameIndex())).getBytes();
  71     }
  72 







  73 
  74     /**
  75      * @return name of the referenced class/interface
  76      * @deprecated If the instruction references an array class,
  77      *    this method will return "java.lang.Object".
  78      *    For code generated by Java 1.5, this answer is
  79      *    sometimes wrong (e.g., if the "clone()" method is
  80      *    called on an array).  A better idea is to use
  81      *    the {@link #getReferenceType(ConstantPoolGen)} method, which correctly distinguishes
  82      *    between class types and array types.
  83      *
  84      */
  85     @Deprecated
  86     public String getClassName( final ConstantPoolGen cpg ) {
  87         final ConstantPool cp = cpg.getConstantPool();
  88         final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
  89         final String className = cp.getConstantString(cmr.getClassIndex(), Const.CONSTANT_Class);
  90         if (className.startsWith("[")) {
  91             // Turn array classes into java.lang.Object.
  92             return "java.lang.Object";
  93         }
  94         return className.replace('/', '.');
  95     }
  96 
  97 
  98     /** @return type of the referenced class/interface
  99      * @deprecated If the instruction references an array class,
 100      *    the ObjectType returned will be invalid.  Use
 101      *    getReferenceType() instead.
 102      */
 103     @Deprecated
 104     public ObjectType getClassType( final ConstantPoolGen cpg ) {
 105         return ObjectType.getInstance(getClassName(cpg));
 106     }
 107 
 108 
 109     /**
 110      * Return the reference type representing the class, interface,
 111      * or array class referenced by the instruction.
 112      * @param cpg the ConstantPoolGen used to create the instruction
 113      * @return an ObjectType (if the referenced class type is a class
 114      *   or interface), or an ArrayType (if the referenced class
 115      *   type is an array class)
 116      */
 117     public ReferenceType getReferenceType( final ConstantPoolGen cpg ) {
 118         final ConstantPool cp = cpg.getConstantPool();
 119         final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
 120         String className = cp.getConstantString(cmr.getClassIndex(), Const.CONSTANT_Class);
 121         if (className.startsWith("[")) {
 122             return (ArrayType) Type.getType(className);
 123         }
 124         className = className.replace('/', '.');
 125         return ObjectType.getInstance(className);
 126     }
 127 
 128 
 129     /**
 130      * Get the ObjectType of the method return or field.
 131      *
 132      * @return type of the referenced class/interface
 133      * @throws ClassGenException when the field is (or method returns) an array,
 134      */
 135     @Override
 136     public ObjectType getLoadClassType( final ConstantPoolGen cpg ) {
 137         final ReferenceType rt = getReferenceType(cpg);
 138         if(rt instanceof ObjectType) {
 139             return (ObjectType)rt;
 140         }
 141         throw new ClassGenException(rt.getSignature() + " does not represent an ObjectType");
 142     }
 143 }
< prev index next >