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

The following examples show how to use org.apache.bcel.Const#CONSTANT_Package . 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: 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 2
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 3
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 4
Source File: ConstantPackage.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * @param nameIndex Name index in constant pool.  Should refer to a
 * ConstantUtf8.
 */
public ConstantPackage(final int nameIndex) {
    super(Const.CONSTANT_Package);
    this.nameIndex = nameIndex;
}