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

The following examples show how to use org.apache.bcel.Const#T_VOID . 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: TypeFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Work around some weirdness in BCEL (inherited from JVM Spec 1): BCEL
 * considers long and double types to consume two slots on the stack. This
 * method ensures that we push two types for each double or long value.
 */
protected void pushValue(Type type) {
    if (type.getType() == Const.T_VOID) {
        throw new IllegalArgumentException("Can't push void");
    }
    TypeFrame frame = getFrame();
    if (type.getType() == Const.T_LONG) {
        frame.pushValue(Type.LONG);
        frame.pushValue(TypeFrame.getLongExtraType());
    } else if (type.getType() == Const.T_DOUBLE) {
        frame.pushValue(Type.DOUBLE);
        frame.pushValue(TypeFrame.getDoubleExtraType());
    } else {
        frame.pushValue(type);
    }
}
 
Example 2
Source File: ArrayType.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor for array of given type
 *
 * @param type type of array (may be an array itself)
 */
public ArrayType(final Type type, final int dimensions) {
    super(Const.T_ARRAY, "<dummy>");
    if ((dimensions < 1) || (dimensions > Const.MAX_BYTE)) {
        throw new ClassGenException("Invalid number of dimensions: " + dimensions);
    }
    switch (type.getType()) {
        case Const.T_ARRAY:
            final ArrayType array = (ArrayType) type;
            this.dimensions = dimensions + array.dimensions;
            basicType = array.basicType;
            break;
        case Const.T_VOID:
            throw new ClassGenException("Invalid type: void[]");
        default: // Basic type or reference
            this.dimensions = dimensions;
            basicType = type;
            break;
    }
    final StringBuilder buf = new StringBuilder();
    for (int i = 0; i < this.dimensions; i++) {
        buf.append('[');
    }
    buf.append(basicType.getSignature());
    super.setSignature(buf.toString());
}
 
Example 3
Source File: InstructionFactory.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/** Create typed return
 */
public static ReturnInstruction createReturn( final Type type ) {
    switch (type.getType()) {
        case Const.T_ARRAY:
        case Const.T_OBJECT:
            return InstructionConst.ARETURN;
        case Const.T_INT:
        case Const.T_SHORT:
        case Const.T_BOOLEAN:
        case Const.T_CHAR:
        case Const.T_BYTE:
            return InstructionConst.IRETURN;
        case Const.T_FLOAT:
            return InstructionConst.FRETURN;
        case Const.T_DOUBLE:
            return InstructionConst.DRETURN;
        case Const.T_LONG:
            return InstructionConst.LRETURN;
        case Const.T_VOID:
            return InstructionConst.RETURN;
        default:
            throw new IllegalArgumentException("Invalid type: " + type);
    }
}
 
Example 4
Source File: InstructionFactory.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/** Create "null" value for reference types, 0 for basic types like int
 */
public static Instruction createNull( final Type type ) {
    switch (type.getType()) {
        case Const.T_ARRAY:
        case Const.T_OBJECT:
            return InstructionConst.ACONST_NULL;
        case Const.T_INT:
        case Const.T_SHORT:
        case Const.T_BOOLEAN:
        case Const.T_CHAR:
        case Const.T_BYTE:
            return InstructionConst.ICONST_0;
        case Const.T_FLOAT:
            return InstructionConst.FCONST_0;
        case Const.T_DOUBLE:
            return InstructionConst.DCONST_0;
        case Const.T_LONG:
            return InstructionConst.LCONST_0;
        case Const.T_VOID:
            return InstructionConst.NOP;
        default:
            throw new IllegalArgumentException("Invalid type: " + type);
    }
}
 
Example 5
Source File: Type.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
static int getTypeSize( final String signature ) throws StringIndexOutOfBoundsException {
    final byte type = Utility.typeOfSignature(signature);
    if (type <= Const.T_VOID) {
        return encode(BasicType.getType(type).getSize(), 1);
    } else if (type == Const.T_ARRAY) {
        int dim = 0;
        do { // Count dimensions
            dim++;
        } while (signature.charAt(dim) == '[');
        // Recurse, but just once, if the signature is ok
        final int consumed = consumed(getTypeSize(signature.substring(dim)));
        return encode(1, dim + consumed);
    } else { // type == T_REFERENCE
        final int index = signature.indexOf(';'); // Look for closing `;'
        if (index < 0) {
            throw new ClassFormatException("Invalid signature: " + signature);
        }
        return encode(1, index + 1);
    }
}
 
Example 6
Source File: BasicType.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
public static BasicType getType( final byte type ) {
    switch (type) {
        case Const.T_VOID:
            return VOID;
        case Const.T_BOOLEAN:
            return BOOLEAN;
        case Const.T_BYTE:
            return BYTE;
        case Const.T_SHORT:
            return SHORT;
        case Const.T_CHAR:
            return CHAR;
        case Const.T_INT:
            return INT;
        case Const.T_LONG:
            return LONG;
        case Const.T_DOUBLE:
            return DOUBLE;
        case Const.T_FLOAT:
            return FLOAT;
        default:
            throw new ClassGenException("Invalid type: " + type);
    }
}
 
Example 7
Source File: BCELifier.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
static String printType( final String signature ) {
    final Type type = Type.getType(signature);
    final byte t = type.getType();
    if (t <= Const.T_VOID) {
        return "Type." + Const.getTypeName(t).toUpperCase(Locale.ENGLISH);
    } else if (type.toString().equals("java.lang.String")) {
        return "Type.STRING";
    } else if (type.toString().equals("java.lang.Object")) {
        return "Type.OBJECT";
    } else if (type.toString().equals("java.lang.StringBuffer")) {
        return "Type.STRINGBUFFER";
    } else if (type instanceof ArrayType) {
        final ArrayType at = (ArrayType) type;
        return "new ArrayType(" + printType(at.getBasicType()) + ", " + at.getDimensions()
                + ")";
    } else {
        return "new ObjectType(\"" + Utility.signatureToString(signature, false) + "\")";
    }
}
 
Example 8
Source File: TypeFrameModelingVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Helper for pushing the return type of an invoke instruction.
 */
protected void pushReturnType(InvokeInstruction ins) {
    ConstantPoolGen cpg = getCPG();
    Type type = ins.getType(cpg);
    if (type.getType() != Const.T_VOID) {
        pushValue(type);
    }
}
 
Example 9
Source File: Type.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * @return stack size of this type (2 for long and double, 0 for void, 1 otherwise)
 */
public int getSize() {
    switch (type) {
        case Const.T_DOUBLE:
        case Const.T_LONG:
            return 2;
        case Const.T_VOID:
            return 0;
        default:
            return 1;
    }
}
 
Example 10
Source File: Type.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Convert signature to a Type object.
 * @param signature signature string such as Ljava/lang/String;
 * @return type object
 */
// @since 6.0 no longer final
public static Type getType( final String signature ) throws StringIndexOutOfBoundsException {
    final byte type = Utility.typeOfSignature(signature);
    if (type <= Const.T_VOID) {
        //corrected concurrent private static field acess
        wrap(consumed_chars, 1);
        return BasicType.getType(type);
    } else if (type == Const.T_ARRAY) {
        int dim = 0;
        do { // Count dimensions
            dim++;
        } while (signature.charAt(dim) == '[');
        // Recurse, but just once, if the signature is ok
        final Type t = getType(signature.substring(dim));
        //corrected concurrent private static field acess
        //  consumed_chars += dim; // update counter - is replaced by
        final int _temp = unwrap(consumed_chars) + dim;
        wrap(consumed_chars, _temp);
        return new ArrayType(t, dim);
    } else { // type == T_REFERENCE
        // Utility.typeSignatureToString understands how to parse generic types.
        final String parsedSignature = Utility.typeSignatureToString(signature, false);
        wrap(consumed_chars, parsedSignature.length() + 2); // "Lblabla;" `L' and `;' are removed
        return ObjectType.getInstance(parsedSignature.replace('/', '.'));
    }
}
 
Example 11
Source File: BasicType.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor for basic types such as int, long, `void'
 *
 * @param type one of T_INT, T_BOOLEAN, ..., T_VOID
 * @see Const
 */
BasicType(final byte type) {
    super(type, Const.getShortTypeName(type));
    if ((type < Const.T_BOOLEAN) || (type > Const.T_VOID)) {
        throw new ClassGenException("Invalid type: " + type);
    }
}
 
Example 12
Source File: Utility.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Return type of signature as a byte value as defined in <em>Constants</em>
 *
 * @param  signature in format described above
 * @return type of signature
 * @see    Const
 *
 * @throws ClassFormatException if signature isn't a known type
 */
public static byte typeOfSignature( final String signature ) throws ClassFormatException {
    try {
        switch (signature.charAt(0)) {
            case 'B':
                return Const.T_BYTE;
            case 'C':
                return Const.T_CHAR;
            case 'D':
                return Const.T_DOUBLE;
            case 'F':
                return Const.T_FLOAT;
            case 'I':
                return Const.T_INT;
            case 'J':
                return Const.T_LONG;
            case 'L':
            case 'T':
                return Const.T_REFERENCE;
            case '[':
                return Const.T_ARRAY;
            case 'V':
                return Const.T_VOID;
            case 'Z':
                return Const.T_BOOLEAN;
            case 'S':
                return Const.T_SHORT;
            case '!':
            case '+':
            case '*':
                return typeOfSignature(signature.substring(1));
            default:
                throw new ClassFormatException("Invalid method signature: " + signature);
        }
    } catch (final StringIndexOutOfBoundsException e) {
        throw new ClassFormatException("Invalid method signature: " + signature, e);
    }
}
 
Example 13
Source File: Utility.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/** Parse Java type such as "char", or "java.lang.String[]" and return the
 * signature in byte code format, e.g. "C" or "[Ljava/lang/String;" respectively.
 *
 * @param  type Java type
 * @return byte code signature
 */
public static String getSignature( String type ) {
    final StringBuilder buf = new StringBuilder();
    final char[] chars = type.toCharArray();
    boolean char_found = false;
    boolean delim = false;
    int index = -1;
    loop: for (int i = 0; i < chars.length; i++) {
        switch (chars[i]) {
            case ' ':
            case '\t':
            case '\n':
            case '\r':
            case '\f':
                if (char_found) {
                    delim = true;
                }
                break;
            case '[':
                if (!char_found) {
                    throw new IllegalArgumentException("Illegal type: " + type);
                }
                index = i;
                break loop;
            default:
                char_found = true;
                if (!delim) {
                    buf.append(chars[i]);
                }
        }
    }
    int brackets = 0;
    if (index > 0) {
        brackets = countBrackets(type.substring(index));
    }
    type = buf.toString();
    buf.setLength(0);
    for (int i = 0; i < brackets; i++) {
        buf.append('[');
    }
    boolean found = false;
    for (int i = Const.T_BOOLEAN; (i <= Const.T_VOID) && !found; i++) {
        if (Const.getTypeName(i).equals(type)) {
            found = true;
            buf.append(Const.getShortTypeName(i));
        }
    }
    if (!found) {
        buf.append('L').append(type.replace('.', '/')).append(';');
    }
    return buf.toString();
}