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

The following examples show how to use org.apache.bcel.Const#CONSTANT_String . 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: ConstantValue.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * @return String representation of constant value.
 */
@Override
public String toString() {
    Constant c = super.getConstantPool().getConstant(constantValueIndex);
    String buf;
    int i;
    // Print constant to string depending on its type
    switch (c.getTag()) {
        case Const.CONSTANT_Long:
            buf = String.valueOf(((ConstantLong) c).getBytes());
            break;
        case Const.CONSTANT_Float:
            buf = String.valueOf(((ConstantFloat) c).getBytes());
            break;
        case Const.CONSTANT_Double:
            buf = String.valueOf(((ConstantDouble) c).getBytes());
            break;
        case Const.CONSTANT_Integer:
            buf = String.valueOf(((ConstantInteger) c).getBytes());
            break;
        case Const.CONSTANT_String:
            i = ((ConstantString) c).getStringIndex();
            c = super.getConstantPool().getConstant(i, Const.CONSTANT_Utf8);
            buf = "\"" + Utility.convertString(((ConstantUtf8) c).getBytes()) + "\"";
            break;
        default:
            throw new IllegalStateException("Type of ConstValue invalid: " + c);
    }
    return buf;
}
 
Example 2
Source File: ConstantPool.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Gets string from constant pool and bypass the indirection of
 * `ConstantClass' and `ConstantString' objects. I.e. these classes have
 * an index field that points to another entry of the constant pool of
 * type `ConstantUtf8' which contains the real data.
 *
 * @param  index Index in constant pool
 * @param  tag Tag of expected constant, either ConstantClass or ConstantString
 * @return Contents of string reference
 * @see    ConstantClass
 * @see    ConstantString
 * @throws  ClassFormatException
 */
public String getConstantString( final int index, final byte tag ) throws ClassFormatException {
    Constant c;
    int i;
    c = getConstant(index, tag);
    /* This switch() is not that elegant, since the four classes have the
     * same contents, they just differ in the name of the index
     * field variable.
     * But we want to stick to the JVM naming conventions closely though
     * we could have solved these more elegantly by using the same
     * variable name or by subclassing.
     */
    switch (tag) {
        case Const.CONSTANT_Class:
            i = ((ConstantClass) c).getNameIndex();
            break;
        case Const.CONSTANT_String:
            i = ((ConstantString) c).getStringIndex();
            break;
        case Const.CONSTANT_Module:
            i = ((ConstantModule) c).getNameIndex();
            break;
        case Const.CONSTANT_Package:
            i = ((ConstantPackage) c).getNameIndex();
            break;
        default:
            throw new IllegalArgumentException("getConstantString called with illegal tag " + tag);
    }
    // Finally get the string from the constant pool
    c = getConstant(i, Const.CONSTANT_Utf8);
    return ((ConstantUtf8) c).getBytes();
}
 
Example 3
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantString(final ConstantString obj) {
    if (obj.getTag() != Const.CONSTANT_String) {
        throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
    }
    checkIndex(obj, obj.getStringIndex(), CONST_Utf8);
}
 
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: ConstantString.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * @param stringIndex Index of Constant_Utf8 in constant pool
 */
public ConstantString(final int stringIndex) {
    super(Const.CONSTANT_String);
    this.stringIndex = stringIndex;
}
 
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);
    }
}