Java Code Examples for org.apache.bcel.Const#CONSTANT_Fieldref

The following examples show how to use org.apache.bcel.Const#CONSTANT_Fieldref . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantFieldref(final ConstantFieldref obj) {
    if (obj.getTag() != Const.CONSTANT_Fieldref) {
        throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
    }
    checkIndex(obj, obj.getClassIndex(), CONST_Class);
    checkIndex(obj, obj.getNameAndTypeIndex(), CONST_NameAndType);
}
 
Example 2
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantFieldref(final ConstantFieldref obj) {
    if (obj.getTag() != Const.CONSTANT_Fieldref) {
        throw new ClassConstraintException("ConstantFieldref '"+tostring(obj)+"' has wrong tag!");
    }
    final int name_and_type_index = obj.getNameAndTypeIndex();
    final ConstantNameAndType cnat = (ConstantNameAndType) (cp.getConstant(name_and_type_index));
    final String name = ((ConstantUtf8) (cp.getConstant(cnat.getNameIndex()))).getBytes(); // Field or Method name
    if (!validFieldName(name)) {
        throw new ClassConstraintException("Invalid field name '"+name+"' referenced by '"+tostring(obj)+"'.");
    }

    final int class_index = obj.getClassIndex();
    final ConstantClass cc = (ConstantClass) (cp.getConstant(class_index));
    final String className = ((ConstantUtf8) (cp.getConstant(cc.getNameIndex()))).getBytes(); // Class Name in internal form
    if (! validClassName(className)) {
        throw new ClassConstraintException("Illegal class name '"+className+"' used by '"+tostring(obj)+"'.");
    }

    final String sig  = ((ConstantUtf8) (cp.getConstant(cnat.getSignatureIndex()))).getBytes(); // Field or Method sig.(=descriptor)

    try{
        Type.getType(sig); /* Don't need the return value */
    }
    catch (final ClassFormatException cfe) {
        throw new ClassConstraintException("Illegal descriptor (==signature) '"+sig+"' used by '"+tostring(obj)+"'.", cfe);
    }
}
 
Example 3
Source File: ClassDumper.java    From cloud-opensource-java with Apache License 2.0 4 votes vote down vote up
private static SymbolReferences.Builder findSymbolReferences(
    ClassFile source, JavaClass javaClass) {
  SymbolReferences.Builder builder = new SymbolReferences.Builder();

  ConstantPool constantPool = javaClass.getConstantPool();
  Constant[] constants = constantPool.getConstantPool();
  for (Constant constant : constants) {
    if (constant == null) {
       continue;
    }

    byte constantTag = constant.getTag();
    switch (constantTag) {
      case Const.CONSTANT_Class:
        ConstantClass constantClass = (ConstantClass) constant;
        ClassSymbol classSymbol = makeSymbol(constantClass, constantPool, javaClass);
        // skip array class because it is provided by runtime
        if (classSymbol.getClassBinaryName().startsWith("[")) {
          break;
        }
        builder.addClassReference(source, classSymbol);
        break;
      case Const.CONSTANT_Methodref:
      case Const.CONSTANT_InterfaceMethodref:
        // Both ConstantMethodref and ConstantInterfaceMethodref are subclass of ConstantCP
        ConstantCP constantMethodref = (ConstantCP) constant;
        builder.addMethodReference(source, makeSymbol(constantMethodref, constantPool));
        break;
      case Const.CONSTANT_Fieldref:
        ConstantFieldref constantFieldref = (ConstantFieldref) constant;
        builder.addFieldReference(source, makeSymbol(constantFieldref, constantPool));
        break;
      default:
        break;
    }
  }

  for (String interfaceName : javaClass.getInterfaceNames()) {
    builder.addClassReference(source, new InterfaceSymbol(interfaceName));
  }

  return builder;
}
 
Example 4
Source File: ClassModel.java    From aparapi with Apache License 2.0 4 votes vote down vote up
public ConstantPool(JavaClass cls) {
    org.apache.bcel.classfile.ConstantPool cp = cls.getConstantPool();
   final int size = cp.getLength();
   add(new EmptyEntry(0)); // slot 0

   for (int i = 1; i < size; i++) {
      final Constant constant = cp.getConstant(i);
      if (constant != null) {
          final byte tag = constant.getTag();
          switch (tag) {
              case Const.CONSTANT_Class:
                  final int ni = ((ConstantClass) constant).getNameIndex();
                  add(new ClassEntry(ni, i));
                  break;
              case Const.CONSTANT_String:
                  final int si = ((ConstantString) constant).getStringIndex();
                  add(new StringEntry(si, i));
                  break;
              case Const.CONSTANT_Utf8:
                  add(new UTF8Entry(((ConstantUtf8) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_Double:
                  add(new DoubleEntry(((ConstantDouble) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_Float:
                  add(new FloatEntry(((ConstantFloat) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_Long:
                  add(new LongEntry(((ConstantLong) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_Integer:
                  add(new IntegerEntry(((ConstantInteger) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_NameAndType:
                  final int ntInd = ((ConstantNameAndType) constant).getNameIndex();
                  final int siNT = ((ConstantNameAndType) constant).getSignatureIndex();
                  add(new NameAndTypeEntry(ntInd, siNT, i));
                  break;
              case Const.CONSTANT_InterfaceMethodref:
                  final int refClassIndexIMR = ((ConstantCP) constant).getClassIndex();
                  final int nameAndTypeIndIMR = ((ConstantCP) constant).getNameAndTypeIndex();
                  add(new InterfaceMethodEntry(refClassIndexIMR, nameAndTypeIndIMR, i));
                  break;
              case Const.CONSTANT_Methodref:
                  final int refClassIndexM = ((ConstantCP) constant).getClassIndex();
                  final int nameAndTypeIndM = ((ConstantCP) constant).getNameAndTypeIndex();
                  add(new MethodEntry(refClassIndexM, nameAndTypeIndM, i));
                  break;
              case Const.CONSTANT_Fieldref:
                  final int refClassIndex = ((ConstantCP) constant).getClassIndex();
                  final int nameAndTypeInd = ((ConstantCP) constant).getNameAndTypeIndex();
                  add(new FieldEntry(refClassIndex, nameAndTypeInd, i));
                  break;
              case Const.CONSTANT_MethodHandle:
                  final ConstantMethodHandle cmh = (ConstantMethodHandle) constant;
                  final int refKind = cmh.getReferenceKind();
                  final int refInd = cmh.getReferenceIndex();
                  add(new MethodHandleEntry(refKind, refInd, i));
                  break;
              case Const.CONSTANT_MethodType:
                  final ConstantMethodType cmt = (ConstantMethodType) constant;
                  final int descInd = cmt.getDescriptorIndex();
                  add(new MethodTypeEntry(descInd, i));
                  break;
              case Const.CONSTANT_InvokeDynamic:
                  final ConstantInvokeDynamic cid = (ConstantInvokeDynamic) constant;
                  final int bsMethAttInd = cid.getBootstrapMethodAttrIndex();
                  final int ntIndID = cid.getNameAndTypeIndex();
                  add(new InvokeDynamicEntry(bsMethAttInd, ntIndID, i));
                  break;
              default:
                  System.out.printf("slot %04x unexpected Constant constantPoolType = %s\n", i, constant);
          }
      } else {
          add(new EmptyEntry(i));
      }
   }
}
 
Example 5
Source File: ConstantFieldref.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize from another object.
 */
public ConstantFieldref(final ConstantFieldref c) {
    super(Const.CONSTANT_Fieldref, c.getClassIndex(), c.getNameAndTypeIndex());
}
 
Example 6
Source File: ConstantPool.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Resolves constant to a string representation.
 *
 * @param  c Constant to be printed
 * @return String representation
 */
public String constantToString( Constant c ) throws ClassFormatException {
    String str;
    int i;
    final byte tag = c.getTag();
    switch (tag) {
        case Const.CONSTANT_Class:
            i = ((ConstantClass) c).getNameIndex();
            c = getConstant(i, Const.CONSTANT_Utf8);
            str = Utility.compactClassName(((ConstantUtf8) c).getBytes(), false);
            break;
        case Const.CONSTANT_String:
            i = ((ConstantString) c).getStringIndex();
            c = getConstant(i, Const.CONSTANT_Utf8);
            str = "\"" + escape(((ConstantUtf8) c).getBytes()) + "\"";
            break;
        case Const.CONSTANT_Utf8:
            str = ((ConstantUtf8) c).getBytes();
            break;
        case Const.CONSTANT_Double:
            str = String.valueOf(((ConstantDouble) c).getBytes());
            break;
        case Const.CONSTANT_Float:
            str = String.valueOf(((ConstantFloat) c).getBytes());
            break;
        case Const.CONSTANT_Long:
            str = String.valueOf(((ConstantLong) c).getBytes());
            break;
        case Const.CONSTANT_Integer:
            str = String.valueOf(((ConstantInteger) c).getBytes());
            break;
        case Const.CONSTANT_NameAndType:
            str = constantToString(((ConstantNameAndType) c).getNameIndex(),
                    Const.CONSTANT_Utf8)
                    + " " + constantToString(((ConstantNameAndType) c).getSignatureIndex(),
                    Const.CONSTANT_Utf8);
            break;
        case Const.CONSTANT_InterfaceMethodref:
        case Const.CONSTANT_Methodref:
        case Const.CONSTANT_Fieldref:
            str = constantToString(((ConstantCP) c).getClassIndex(), Const.CONSTANT_Class)
                    + "." + constantToString(((ConstantCP) c).getNameAndTypeIndex(),
                    Const.CONSTANT_NameAndType);
            break;
        case Const.CONSTANT_MethodHandle:
            // Note that the ReferenceIndex may point to a Fieldref, Methodref or
            // InterfaceMethodref - so we need to peek ahead to get the actual type.
            final ConstantMethodHandle cmh = (ConstantMethodHandle) c;
            str = Const.getMethodHandleName(cmh.getReferenceKind())
                    + " " + constantToString(cmh.getReferenceIndex(),
                    getConstant(cmh.getReferenceIndex()).getTag());
            break;
        case Const.CONSTANT_MethodType:
            final ConstantMethodType cmt = (ConstantMethodType) c;
            str = constantToString(cmt.getDescriptorIndex(), Const.CONSTANT_Utf8);
            break;
        case Const.CONSTANT_InvokeDynamic:
            final ConstantInvokeDynamic cid = (ConstantInvokeDynamic) c;
            str = cid.getBootstrapMethodAttrIndex()
                    + ":" + constantToString(cid.getNameAndTypeIndex(),
                    Const.CONSTANT_NameAndType);
            break;
        case Const.CONSTANT_Module:
            i = ((ConstantModule) c).getNameIndex();
            c = getConstant(i, Const.CONSTANT_Utf8);
            str = Utility.compactClassName(((ConstantUtf8) c).getBytes(), false);
            break;
        case Const.CONSTANT_Package:
            i = ((ConstantPackage) c).getNameIndex();
            c = getConstant(i, Const.CONSTANT_Utf8);
            str = Utility.compactClassName(((ConstantUtf8) c).getBytes(), false);
            break;
        default: // Never reached
            throw new IllegalArgumentException("Unknown constant type " + tag);
    }
    return str;
}
 
Example 7
Source File: Constant.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Reads one constant from the given input, the type depends on a tag byte.
 *
 * @param dataInput Input stream
 * @return Constant object
 * @throws IOException if an I/O error occurs reading from the given {@code dataInput}.
 * @throws ClassFormatException if the next byte is not recognized
 * @since 6.0 made public
 */
public static Constant readConstant(final DataInput dataInput) throws IOException, ClassFormatException {
    final byte b = dataInput.readByte(); // Read tag byte
    switch (b) {
    case Const.CONSTANT_Class:
        return new ConstantClass(dataInput);
    case Const.CONSTANT_Fieldref:
        return new ConstantFieldref(dataInput);
    case Const.CONSTANT_Methodref:
        return new ConstantMethodref(dataInput);
    case Const.CONSTANT_InterfaceMethodref:
        return new ConstantInterfaceMethodref(dataInput);
    case Const.CONSTANT_String:
        return new ConstantString(dataInput);
    case Const.CONSTANT_Integer:
        return new ConstantInteger(dataInput);
    case Const.CONSTANT_Float:
        return new ConstantFloat(dataInput);
    case Const.CONSTANT_Long:
        return new ConstantLong(dataInput);
    case Const.CONSTANT_Double:
        return new ConstantDouble(dataInput);
    case Const.CONSTANT_NameAndType:
        return new ConstantNameAndType(dataInput);
    case Const.CONSTANT_Utf8:
        return ConstantUtf8.getInstance(dataInput);
    case Const.CONSTANT_MethodHandle:
        return new ConstantMethodHandle(dataInput);
    case Const.CONSTANT_MethodType:
        return new ConstantMethodType(dataInput);
    case Const.CONSTANT_Dynamic:
        return new ConstantDynamic(dataInput);
    case Const.CONSTANT_InvokeDynamic:
        return new ConstantInvokeDynamic(dataInput);
    case Const.CONSTANT_Module:
        return new ConstantModule(dataInput);
    case Const.CONSTANT_Package:
        return new ConstantPackage(dataInput);
    default:
        throw new ClassFormatException("Invalid byte tag in constant pool: " + b);
    }
}
 
Example 8
Source File: ConstantFieldref.java    From commons-bcel with Apache License 2.0 2 votes vote down vote up
/**
 * Initialize instance from input data.
 *
 * @param input input stream
 * @throws IOException
 */
ConstantFieldref(final DataInput input) throws IOException {
    super(Const.CONSTANT_Fieldref, input);
}
 
Example 9
Source File: ConstantFieldref.java    From commons-bcel with Apache License 2.0 2 votes vote down vote up
/**
 * @param class_index Reference to the class containing the Field
 * @param name_and_type_index and the Field signature
 */
public ConstantFieldref(final int class_index, final int name_and_type_index) {
    super(Const.CONSTANT_Fieldref, class_index, name_and_type_index);
}