Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
ASM: a very small and fast Java bytecode manipulation framework Copyright (c) 2000-2007 INRIA, France Telecom All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 package org.objectweb.asm;
 
A Java type. This class can be used to make it easier to manipulate type and method descriptors.

Author(s):
Eric Bruneton
Chris Nokleberg
 
 public class Type {

    
The sort of the void type. See getSort.
 
     public static final int VOID = 0;

    
The sort of the boolean type. See getSort.
 
     public static final int BOOLEAN = 1;

    
The sort of the char type. See getSort.
 
     public static final int CHAR = 2;

    
The sort of the byte type. See getSort.
 
     public static final int BYTE = 3;

    
The sort of the short type. See getSort.
 
     public static final int SHORT = 4;

    
The sort of the int type. See getSort.
 
     public static final int INT = 5;

    
The sort of the float type. See getSort.
 
     public static final int FLOAT = 6;

    
The sort of the long type. See getSort.
 
     public static final int LONG = 7;

    
The sort of the double type. See getSort.
 
     public static final int DOUBLE = 8;

    
The sort of array reference types. See getSort.
 
     public static final int ARRAY = 9;

    
The sort of object reference type. See getSort.
 
     public static final int OBJECT = 10;

    
The void type.
    public static final Type VOID_TYPE = new Type();

    
The boolean type.
    public static final Type BOOLEAN_TYPE = new Type();

    
The char type.
    public static final Type CHAR_TYPE = new Type();

    
The byte type.
    public static final Type BYTE_TYPE = new Type();

    
The short type.
    public static final Type SHORT_TYPE = new Type();

    
The int type.
    public static final Type INT_TYPE = new Type();

    
The float type.
    public static final Type FLOAT_TYPE = new Type();

    
The long type.
    public static final Type LONG_TYPE = new Type();

    
The double type.
    public static final Type DOUBLE_TYPE = new Type();
    // ------------------------------------------------------------------------
    // Fields
    // ------------------------------------------------------------------------

    
The sort of this Java type.
    private final int sort;

    
A buffer containing the internal name of this Java type. This field is only used for reference types.
    private final char[] buf;

    
The offset of the internal name of this Java type in buf. This field is only used for reference types.
    private final int off;

    
The length of the internal name of this Java type. This field is only used for reference types.
    private final int len;
    // ------------------------------------------------------------------------
    // Constructors
    // ------------------------------------------------------------------------

    
Constructs a primitive type.

Parameters:
sort the sort of the primitive type to be constructed.
    private Type(final int sort) {
        this(sortnull, 0, 1);
    }

    
Constructs a reference type.

Parameters:
sort the sort of the reference type to be constructed.
buf a buffer containing the descriptor of the previous type.
off the offset of this descriptor in the previous buffer.
len the length of this descriptor.
    private Type(final int sortfinal char[] buffinal int offfinal int len)
    {
        this. = sort;
        this. = buf;
        this. = off;
        this. = len;
    }

    
Returns the Java type corresponding to the given type descriptor.

Parameters:
typeDescriptor a type descriptor.
Returns:
the Java type corresponding to the given type descriptor.
    public static Type getType(final String typeDescriptor) {
        return getType(typeDescriptor.toCharArray(), 0);
    }

    
Returns the Java type corresponding to the given internal name.

Parameters:
internalName an internal name.
Returns:
the Java type corresponding to the given internal name.
    public static Type getObjectType(final String internalName) {
        char[] buf = internalName.toCharArray();
        return new Type(buf[0] == '[' ?  : buf, 0, buf.length);
    }

    
Returns the Java type corresponding to the given class.

Parameters:
c a class.
Returns:
the Java type corresponding to the given class.
    public static Type getType(final Class c) {
        if (c.isPrimitive()) {
            if (c == .) {
                return ;
            } else if (c == .) {
                return ;
            } else if (c == .) {
                return ;
            } else if (c == .) {
                return ;
            } else if (c == .) {
                return ;
            } else if (c == .) {
                return ;
            } else if (c == .) {
                return ;
            } else if (c == .) {
                return ;
            } else /* if (c == Long.TYPE) */{
                return ;
            }
        } else {
            return getType(getDescriptor(c));
        }
    }

    
Returns the Java types corresponding to the argument types of the given method descriptor.

Parameters:
methodDescriptor a method descriptor.
Returns:
the Java types corresponding to the argument types of the given method descriptor.
    public static Type[] getArgumentTypes(final String methodDescriptor) {
        char[] buf = methodDescriptor.toCharArray();
        int off = 1;
        int size = 0;
        while (true) {
            char car = buf[off++];
            if (car == ')') {
                break;
            } else if (car == 'L') {
                while (buf[off++] != ';') {
                }
                ++size;
            } else if (car != '[') {
                ++size;
            }
        }
        Type[] args = new Type[size];
        off = 1;
        size = 0;
        while (buf[off] != ')') {
            args[size] = getType(bufoff);
            off += args[size]. + (args[size]. ==  ? 2 : 0);
            size += 1;
        }
        return args;
    }

    
Returns the Java types corresponding to the argument types of the given method.

Parameters:
method a method.
Returns:
the Java types corresponding to the argument types of the given method.
    public static Type[] getArgumentTypes(final Method method) {
        Class[] classes = method.getParameterTypes();
        Type[] types = new Type[classes.length];
        for (int i = classes.length - 1; i >= 0; --i) {
            types[i] = getType(classes[i]);
        }
        return types;
    }

    
Returns the Java type corresponding to the return type of the given method descriptor.

Parameters:
methodDescriptor a method descriptor.
Returns:
the Java type corresponding to the return type of the given method descriptor.
    public static Type getReturnType(final String methodDescriptor) {
        char[] buf = methodDescriptor.toCharArray();
        return getType(bufmethodDescriptor.indexOf(')') + 1);
    }

    
Returns the Java type corresponding to the return type of the given method.

Parameters:
method a method.
Returns:
the Java type corresponding to the return type of the given method.
    public static Type getReturnType(final Method method) {
        return getType(method.getReturnType());
    }

    
Returns the Java type corresponding to the given type descriptor.

Parameters:
buf a buffer containing a type descriptor.
off the offset of this descriptor in the previous buffer.
Returns:
the Java type corresponding to the given type descriptor.
    private static Type getType(final char[] buffinal int off) {
        int len;
        switch (buf[off]) {
            case 'V':
                return ;
            case 'Z':
                return ;
            case 'C':
                return ;
            case 'B':
                return ;
            case 'S':
                return ;
            case 'I':
                return ;
            case 'F':
                return ;
            case 'J':
                return ;
            case 'D':
                return ;
            case '[':
                len = 1;
                while (buf[off + len] == '[') {
                    ++len;
                }
                if (buf[off + len] == 'L') {
                    ++len;
                    while (buf[off + len] != ';') {
                        ++len;
                    }
                }
                return new Type(bufofflen + 1);
                // case 'L':
            default:
                len = 1;
                while (buf[off + len] != ';') {
                    ++len;
                }
                return new Type(bufoff + 1, len - 1);
        }
    }
    // ------------------------------------------------------------------------
    // Accessors
    // ------------------------------------------------------------------------

    
Returns the sort of this Java type.

Returns:
VOID, BOOLEAN, CHAR, BYTE, SHORT, INT, FLOAT, LONG, DOUBLE, ARRAY or OBJECT.
    public int getSort() {
        return ;
    }

    
Returns the number of dimensions of this array type. This method should only be used for an array type.

Returns:
the number of dimensions of this array type.
    public int getDimensions() {
        int i = 1;
        while ([ + i] == '[') {
            ++i;
        }
        return i;
    }

    
Returns the type of the elements of this array type. This method should only be used for an array type.

Returns:
Returns the type of the elements of this array type.
    public Type getElementType() {
        return getType( + getDimensions());
    }

    
Returns the name of the class corresponding to this type.

Returns:
the fully qualified name of the class corresponding to this type.
    public String getClassName() {
        switch () {
            case :
                return "void";
            case :
                return "boolean";
            case :
                return "char";
            case :
                return "byte";
            case :
                return "short";
            case :
                return "int";
            case :
                return "float";
            case :
                return "long";
            case :
                return "double";
            case :
                StringBuffer b = new StringBuffer(getElementType().getClassName());
                for (int i = getDimensions(); i > 0; --i) {
                    b.append("[]");
                }
                return b.toString();
                // case OBJECT:
            default:
                return new String().replace('/''.');
        }
    }

    
Returns the internal name of the class corresponding to this object or array type. The internal name of a class is its fully qualified name (as returned by Class.getName(), where '.' are replaced by '/'. This method should only be used for an object or array type.

Returns:
the internal name of the class corresponding to this object type.
    public String getInternalName() {
        return new String();
    }
    // ------------------------------------------------------------------------
    // Conversion to type descriptors
    // ------------------------------------------------------------------------

    
Returns the descriptor corresponding to this Java type.

Returns:
the descriptor corresponding to this Java type.
    public String getDescriptor() {
        StringBuffer buf = new StringBuffer();
        getDescriptor(buf);
        return buf.toString();
    }

    
Returns the descriptor corresponding to the given argument and return types.

Parameters:
returnType the return type of the method.
argumentTypes the argument types of the method.
Returns:
the descriptor corresponding to the given argument and return types.
    public static String getMethodDescriptor(
        final Type returnType,
        final Type[] argumentTypes)
    {
        StringBuffer buf = new StringBuffer();
        buf.append('(');
        for (int i = 0; i < argumentTypes.length; ++i) {
            argumentTypes[i].getDescriptor(buf);
        }
        buf.append(')');
        returnType.getDescriptor(buf);
        return buf.toString();
    }

    
Appends the descriptor corresponding to this Java type to the given string buffer.

Parameters:
buf the string buffer to which the descriptor must be appended.
    private void getDescriptor(final StringBuffer buf) {
        switch () {
            case :
                buf.append('V');
                return;
            case :
                buf.append('Z');
                return;
            case :
                buf.append('C');
                return;
            case :
                buf.append('B');
                return;
            case :
                buf.append('S');
                return;
            case :
                buf.append('I');
                return;
            case :
                buf.append('F');
                return;
            case :
                buf.append('J');
                return;
            case :
                buf.append('D');
                return;
            case :
                buf.append(this.);
                return;
                // case OBJECT:
            default:
                buf.append('L');
                buf.append(this.);
                buf.append(';');
        }
    }
    // ------------------------------------------------------------------------
    // Direct conversion from classes to type descriptors,
    // without intermediate Type objects
    // ------------------------------------------------------------------------

    
Returns the internal name of the given class. The internal name of a class is its fully qualified name, as returned by Class.getName(), where '.' are replaced by '/'.

Parameters:
c an object or array class.
Returns:
the internal name of the given class.
    public static String getInternalName(final Class c) {
        return c.getName().replace('.''/');
    }

    
Returns the descriptor corresponding to the given Java type.

Parameters:
c an object class, a primitive class or an array class.
Returns:
the descriptor corresponding to the given class.
    public static String getDescriptor(final Class c) {
        StringBuffer buf = new StringBuffer();
        getDescriptor(bufc);
        return buf.toString();
    }

    
Returns the descriptor corresponding to the given constructor.

Parameters:
c a Constructor object.
Returns:
the descriptor of the given constructor.
    public static String getConstructorDescriptor(final Constructor c) {
        Class[] parameters = c.getParameterTypes();
        StringBuffer buf = new StringBuffer();
        buf.append('(');
        for (int i = 0; i < parameters.length; ++i) {
            getDescriptor(bufparameters[i]);
        }
        return buf.append(")V").toString();
    }

    
Returns the descriptor corresponding to the given method.

Parameters:
m a Method object.
Returns:
the descriptor of the given method.
    public static String getMethodDescriptor(final Method m) {
        Class[] parameters = m.getParameterTypes();
        StringBuffer buf = new StringBuffer();
        buf.append('(');
        for (int i = 0; i < parameters.length; ++i) {
            getDescriptor(bufparameters[i]);
        }
        buf.append(')');
        getDescriptor(bufm.getReturnType());
        return buf.toString();
    }

    
Appends the descriptor of the given class to the given string buffer.

Parameters:
buf the string buffer to which the descriptor must be appended.
c the class whose descriptor must be computed.
    private static void getDescriptor(final StringBuffer buffinal Class c) {
        Class d = c;
        while (true) {
            if (d.isPrimitive()) {
                char car;
                if (d == .) {
                    car = 'I';
                } else if (d == .) {
                    car = 'V';
                } else if (d == .) {
                    car = 'Z';
                } else if (d == .) {
                    car = 'B';
                } else if (d == .) {
                    car = 'C';
                } else if (d == .) {
                    car = 'S';
                } else if (d == .) {
                    car = 'D';
                } else if (d == .) {
                    car = 'F';
                } else /* if (d == Long.TYPE) */{
                    car = 'J';
                }
                buf.append(car);
                return;
            } else if (d.isArray()) {
                buf.append('[');
                d = d.getComponentType();
            } else {
                buf.append('L');
                String name = d.getName();
                int len = name.length();
                for (int i = 0; i < len; ++i) {
                    char car = name.charAt(i);
                    buf.append(car == '.' ? '/' : car);
                }
                buf.append(';');
                return;
            }
        }
    }
    // ------------------------------------------------------------------------
    // Corresponding size and opcodes
    // ------------------------------------------------------------------------

    
Returns the size of values of this type.

Returns:
the size of values of this type, i.e., 2 for long and double, and 1 otherwise.
    public int getSize() {
        return  ==  ||  ==  ? 2 : 1;
    }

    
Returns a JVM instruction opcode adapted to this Java type.

Parameters:
opcode a JVM instruction opcode. This opcode must be one of ILOAD, ISTORE, IALOAD, IASTORE, IADD, ISUB, IMUL, IDIV, IREM, INEG, ISHL, ISHR, IUSHR, IAND, IOR, IXOR and IRETURN.
Returns:
an opcode that is similar to the given opcode, but adapted to this Java type. For example, if this type is float and opcode is IRETURN, this method returns FRETURN.
    public int getOpcode(final int opcode) {
        if (opcode == . || opcode == .) {
            switch () {
                case :
                case :
                    return opcode + 5;
                case :
                    return opcode + 6;
                case :
                    return opcode + 7;
                case :
                    return opcode;
                case :
                    return opcode + 2;
                case :
                    return opcode + 1;
                case :
                    return opcode + 3;
                    // case ARRAY:
                    // case OBJECT:
                default:
                    return opcode + 4;
            }
        } else {
            switch () {
                case :
                    return opcode + 5;
                case :
                case :
                case :
                case :
                case :
                    return opcode;
                case :
                    return opcode + 2;
                case :
                    return opcode + 1;
                case :
                    return opcode + 3;
                    // case ARRAY:
                    // case OBJECT:
                default:
                    return opcode + 4;
            }
        }
    }
    // ------------------------------------------------------------------------
    // Equals, hashCode and toString
    // ------------------------------------------------------------------------

    
Tests if the given object is equal to this type.

Parameters:
o the object to be compared to this type.
Returns:
true if the given object is equal to this type.
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Type)) {
            return false;
        }
        Type t = (Typeo;
        if ( != t.sort) {
            return false;
        }
        if ( ==  ||  == ) {
            if ( != t.len) {
                return false;
            }
            for (int i = j = t.offend = i + i < endi++, j++) {
                if ([i] != t.buf[j]) {
                    return false;
                }
            }
        }
        return true;
    }

    
Returns a hash code value for this type.

Returns:
a hash code value for this type.
    public int hashCode() {
        int hc = 13 * ;
        if ( ==  ||  == ) {
            for (int i = end = i + i < endi++) {
                hc = 17 * (hc + [i]);
            }
        }
        return hc;
    }

    
Returns a string representation of this type.

Returns:
the descriptor of this type.
    public String toString() {
        return getDescriptor();
    }
New to GrepCode? Check out our FAQ X