FileDocCategorySizeDatePackage
Types.javaAPI DocAndroid 1.5 API24071Wed May 06 22:41:16 BST 2009com.vladium.jcd.lib

Types

public abstract class Types extends Object
Utility methods for manipulating type signatures and descriptors. TODO: fix usage of chars in parsers
author
(C) 2001, Vlad Roubtsov

Fields Summary
Constructors Summary
Methods Summary
public static java.lang.StringaccessFlagsToString(int flags, boolean isClass)

        final StringBuffer result = new StringBuffer ();
      
        boolean first = true;
        
        if (isClass)
        {
            for (int f = 0; f < IAccessFlags.ALL_ACC.length; ++ f)
            {
                final int bit = IAccessFlags.ALL_ACC [f];
                
                if ((flags & bit) != 0)
                {
                    if (first)
                        first = false;
                    else
                        result.append (" ");
                    
                    if (bit == IAccessFlags.ACC_SUPER)
                        result.append ("super");
                    else
                        result.append (IAccessFlags.ALL_ACC_NAMES [f]);
                }
            }
        }
        else
        {
            for (int f = 0; f < IAccessFlags.ALL_ACC.length; ++ f)
            {
                final int bit = IAccessFlags.ALL_ACC [f];
                
                if ((flags & bit) != 0)
                {
                    if (first)
                        first = false;
                    else
                        result.append (" ");
                        
                    result.append (IAccessFlags.ALL_ACC_NAMES [f]);
                }
            }
        }
        
        return result.toString ();
    
public static java.lang.String[]descriptorToParameterTypes(java.lang.String methoddescriptor)

        //System.out.println ("METHOD DESCRIPTOR: [" + methoddescriptor + "]");
    
        try
        {
            final methodDescriptorCompiler compiler = new methodDescriptorCompiler (methoddescriptor);
            compiler.methodDescriptor ();
            return compiler.getResult ();
        }
        catch (IOException e)
        {
            throw new IllegalArgumentException ("error parsing [" + methoddescriptor + "]: " + e.toString ());
        }
        
        /*
        final java.util.Vector _result = new java.util.Vector ();
        final StringBuffer token = new StringBuffer ();

        char c = '*';
        int scan = 0;
        
        for (int state = 0; state != 4; )
        {
            try
            {
                switch (state)
                {
                case 0:
                    c = methoddescriptor.charAt (scan++);
                    if (c == '(')
                        state = 1;
                    else
                        throw new IllegalArgumentException ("malformed method descriptor: [" + methoddescriptor + "]");
                    break;
                    
                case 1:
                    c = methoddescriptor.charAt (scan);
                    switch (c)
                    {
                    case 'B':
                    case 'C':
                    case 'D':
                    case 'F':
                    case 'I':
                    case 'J':
                    case 'S':
                    case 'Z':
                        token.append (c);
                        _result.addElement (token.toString ());
                        token.setLength (0);
                        scan++;
                        break;
                        
                    case 'L':
                        state = 2;
                        token.append (c);
                        scan++;
                        break;
                        
                    case '[':
                        state = 3;    
                        token.append (c);
                        scan++;
                        break;
                        
                    case ')':
                        if (token.length () > 0)
                        {
                            _result.addElement (token.toString ());
                            token.setLength (0);
                        }
                        state = 4;
                        break;
                            
                        
                    default:
                        throw new IllegalArgumentException ("[state = " + state + ", c = " + c + "] malformed method descriptor: [" + methoddescriptor + "]");
                        
                    } // end of nested switch
                    break;
                    
                case 2:
                    c = methoddescriptor.charAt (scan++);
                    token.append (c);
                    if (c == ';')
                    {
                        _result.addElement (token.toString ());
                        token.setLength (0);
                        state = 1;
                    }
                    break;
                    
                case 3:
                    c = methoddescriptor.charAt (scan++);
                    token.append (c);
                    if (c != '[')
                    {
                        state = 1;
                    }
                    break;    
                    
                } // end of switch
                
                //System.out.println ("[state = " + state + ", c = " + c + "]");
            }
            catch (StringIndexOutOfBoundsException e)
            {
                throw new IllegalArgumentException ("malformed method descriptor: [" + methoddescriptor + "]");
            }
        }
        
        String [] result = new String [_result.size ()];
        _result.copyInto (result);
        
        return result;
        */
    
public static java.lang.StringdescriptorToReturnType(java.lang.String methoddescriptor)

        final int i1 = methoddescriptor.indexOf ('(");
        final int i2 = methoddescriptor.lastIndexOf (')");
        
        if ((i1 < 0) || (i2 <= 0) || (i1 >= i2) || (i2 >= methoddescriptor.length () - 1))
            throw new IllegalArgumentException ("malformed method descriptor: [" + methoddescriptor + "]");

        return methoddescriptor.substring (i2 + 1);                                                                                                
    
public static java.lang.ClassdescriptorToType(java.lang.String typedescriptor)
Converts a VM descriptor to the corresponding type.

Example:
descriptorToType("[[I") = int[][].class
descriptorToType("B") = byte.class

Note the invariant descriptorToType(typeToDescriptor(c)) == c.

see
#descriptorToType

        return new typeDescriptorCompiler ().descriptorToClass (typedescriptor);
    
public static java.lang.StringfullMethodDescriptorToFullUserName(java.lang.String classJavaName, java.lang.String methodName, java.lang.String methoddescriptor)

        if ("<init>".equals (methodName))
            methodName = simpleClassName (classJavaName);
        if ("<clinit>".equals (methodName))
            methodName = "<static class initializer>";
        
        return classJavaName + '." + methodName + ' " + methodDescriptorToUserName (methoddescriptor);
    
public static java.lang.StringfullMethodDescriptorToUserName(java.lang.String classJavaName, java.lang.String methodName, java.lang.String methoddescriptor)

        if ("<init>".equals (methodName))
            methodName = simpleClassName (classJavaName);
        if ("<clinit>".equals (methodName))
            methodName = "<static class initializer>";
        
        return methodName + ' " + methodDescriptorToUserName (methoddescriptor);
    
public static java.lang.StringgetClassPackageName(java.lang.Class c)
Returns 'c''s package name [does not include trailing '.'] or "" if 'c' is in the default package.

        // TODO: handle array and other types
        
        final String className = c.getName ();
        final int lastDot = className.lastIndexOf ('.");
        return lastDot >= 0 ? className.substring (0, lastDot) : "";
    
public static java.lang.StringjavaNameToVMName(java.lang.String javaName)
Converts Java-styled package/class name to how it would be represented in the VM.

Example:
javaNameToVMName("java.lang.Object") = "java/lang/Object"

see
#vmNameToJavaName

        if (javaName == null) return null;
        return javaName.replace ('.", '/");
    
public static java.lang.StringmethodDescriptorToUserName(java.lang.String methoddescriptor)

        final String [] parameterTypes = descriptorToParameterTypes (methoddescriptor);
        
        final StringBuffer result = new StringBuffer ("(");
        
        for (int p = 0; p < parameterTypes.length; p++)
        {
            //System.out.println ("DESCRIPTOR: [" + parameterTypes [p] + "]");
            
            if (p > 0) result.append (", ");
            
            final String typeUserName = typeDescriptorToUserName (parameterTypes [p]);
            int lastDot = typeUserName.lastIndexOf ('.");
            
            if ((lastDot < 0) || ! "java.lang.".equals (typeUserName.substring (0, lastDot + 1)))
                result.append (typeUserName);
            else
                result.append (typeUserName.substring (lastDot + 1));
        }
        
        result.append (')");
        return result.toString (); 
    
public static java.lang.StringsignatureToDescriptor(java.lang.reflect.Method method)
Converts a method signature to its VM descriptor representation. See $4.3 of the VM spec 1.0 for the descriptor grammar.

Example:
signatureToDescriptor(new Object().getClass().getMethod("equals" ,new Class[0])) = "(Ljava/lang/Object;)Z"

Equivalent to signatureToDescriptor(method.getParameterTypes (), method.getReturnType ()).

        if (method == null) throw new IllegalArgumentException ("null input: method");
        return signatureToDescriptor (method.getParameterTypes (), method.getReturnType ());
    
public static java.lang.StringsignatureToDescriptor(java.lang.Class[] parameterTypes, java.lang.Class returnType)
Converts a method signature (parameter types + return type) to its VM descriptor representation. See $4.3 of the VM spec 1.0 for the descriptor grammar.

        return new signatureCompiler ().signatureDescriptor (parameterTypes, returnType);
    
public static java.lang.StringsignatureToMethodDescriptor(java.lang.String[] parameterTypeDescriptors, java.lang.String returnTypeDescriptor)

        final StringBuffer result = new StringBuffer ("(");
        
        for (int p = 0; p < parameterTypeDescriptors.length; p++)
        {
            result.append (parameterTypeDescriptors [p]);
        }
        
        result.append (')");
        result.append (returnTypeDescriptor);
        
        return result.toString (); 
    
private static java.lang.StringsimpleClassName(java.lang.String classJavaName)

        int lastDot = classJavaName.lastIndexOf ('.");
        
        if (lastDot < 0)
            return classJavaName;
        else
            return classJavaName.substring (lastDot + 1);
    
public static java.lang.StringtypeDescriptorToUserName(java.lang.String typedescriptor)

        return new typeDescriptorCompiler2 ().descriptorToClass (typedescriptor);
    
public static java.lang.StringtypeToDescriptor(java.lang.Class type)
Converts a type (a Class) to its VM descriptor representation.

Example:
typeToDescriptor(Object.class) = "Ljava/lang/Object;"
typeToDescriptor(boolean.class) = "Z"

Note the invariant typeToDescriptor(descriptorToType(desc)) == desc.

see
#descriptorToType

        return new signatureCompiler ().typeDescriptor (type);
    
public static java.lang.StringvmNameToJavaName(java.lang.String vmName)
Converts a VM-styled package/class name to how it would be represented in Java.

Example:
vmNameToJavaName("java/lang/Object") = "java.lang.Object"

see
#javaNameToVMName

        if (vmName == null) return null;
        return vmName.replace ('/", '.");