FileDocCategorySizeDatePackage
SignatureSourcer.javaAPI DocAndroid 1.5 API8783Wed May 06 22:41:10 BST 2009com.android.mkstubs.sourcer

SignatureSourcer

public class SignatureSourcer extends Object implements org.objectweb.asm.signature.SignatureVisitor
A signature visitor that can be used to generate Java source corresponding to various types of signatures.

Terminology: a "signature" is a type descriptor for generics. There are different types of signatures depending on the context where they are used, e.g. method declarations, method parameters, class declarations, etc..

Note: most of the implementation is a duplicate of ASM's SignatureWriter with some slight variations.

Note: When processing a method's signature, the signature order is the reverse of the source order, e.g. the signature is written as "(parameters)return-type" where we want to generate "return-type method-name (parameters)". To hanlde this case, the return-type and parameters are not output directly but are instead accumulated in internal variables that you can get later using {@link #getReturnType()}, {@link #getParameters()}, {@link #getSuperClass()} and {@link #formalsToString()}.

Fields Summary
private final StringBuilder
mBuf
Buffer used to construct the signature.
private final StringBuilder
mFormalsBuf
Buffer used to construct the formals signature.
private boolean
mWritingFormals
Indicates if the signature is currently processing formal type parameters.
private int
mArgumentStack
Stack used to keep track of class types that have arguments. Each element of this stack is a boolean encoded in one bit. The top of the stack is the lowest order bit. Pushing false = *2, pushing true = *2+1, popping = /2.
private SignatureSourcer
mReturnType
{@link SignatureSourcer} generated when parsing the return type of this signature. Initially null.
private SignatureSourcer
mSuperClass
{@link SignatureSourcer} generated when parsing the super class of this signature. Initially null.
private ArrayList
mParameters
{@link SignatureSourcer}s for each parameters generated when parsing the method parameters of this signature. Initially empty but not null.
Constructors Summary
public SignatureSourcer()
Constructs a new {@link SignatureWriter} object.



    
               
      
    
Methods Summary
private voidendArguments()
Ends the type arguments of a class or inner class type.

        if (mArgumentStack % 2 != 0) {
            getBuf().append('>");
        }
        mArgumentStack /= 2;
    
private voidendFormals()
Ends the formal type parameters section of the signature.

        if (mWritingFormals) {
            getBuf().append('>");
            mWritingFormals = false;
        }
    
public java.lang.StringformalsToString()

        return mFormalsBuf.toString();
    
private java.lang.StringBuildergetBuf()

        if (mWritingFormals) {
            return mFormalsBuf;
        } else {
            return mBuf;
        }
    
public java.util.ArrayListgetParameters()
Will be non-empty if a parameters were processed by {@link SignatureReader#accept(SignatureVisitor)}

        return mParameters;
    
public com.android.mkstubs.sourcer.SignatureSourcergetReturnType()
Will be non-null if a return type was processed by {@link SignatureReader#accept(SignatureVisitor)}

        return mReturnType;
    
public com.android.mkstubs.sourcer.SignatureSourcergetSuperClass()
Will be non-null if a super class was processed by {@link SignatureReader#accept(SignatureVisitor)}

        return mSuperClass;
    
public booleanhasFormalsContent()
True if the signature contains formal type parameters, which are available via {@link #formalsToString()} after calling {@link SignatureReader#accept(SignatureVisitor)}

        return mFormalsBuf.length() > 0;
    
public java.lang.StringtoString()
Contains the whole signature type when called by {@link SignatureReader#acceptType(SignatureVisitor)} or just the formals if called by {@link SignatureReader#accept(SignatureVisitor)}.

        return mBuf.toString();
    
public org.objectweb.asm.signature.SignatureVisitorvisitArrayType()

        getBuf().append('[");
        return this;
    
public voidvisitBaseType(char descriptor)

        getBuf().append(Type.getType(Character.toString(descriptor)).getClassName());
    
public org.objectweb.asm.signature.SignatureVisitorvisitClassBound()

        // we don't differentiate between visiting a sub class or interface type 
        return this;
    
public voidvisitClassType(java.lang.String name)

        getBuf().append(name.replace('/", '."));
        mArgumentStack *= 2;
    
public voidvisitEnd()

        endArguments();
    
public org.objectweb.asm.signature.SignatureVisitorvisitExceptionType()

        getBuf().append('^");
        return this;
    
public voidvisitFormalTypeParameter(java.lang.String name)

        if (!mWritingFormals) {
            mWritingFormals = true;
            getBuf().append('<");
        } else {
            getBuf().append(", ");
        }
        getBuf().append(name);
        getBuf().append(" extends ");
    
public voidvisitInnerClassType(java.lang.String name)

        endArguments();
        getBuf().append('.");
        getBuf().append(name.replace('/", '."));
        mArgumentStack *= 2;
    
public org.objectweb.asm.signature.SignatureVisitorvisitInterface()

        return this;
    
public org.objectweb.asm.signature.SignatureVisitorvisitInterfaceBound()

        // we don't differentiate between visiting a sub class or interface type 
        return this;
    
public org.objectweb.asm.signature.SignatureVisitorvisitParameterType()

        endFormals();
        SignatureSourcer sourcer = new SignatureSourcer();
        mParameters.add(sourcer);
        return sourcer;
    
public org.objectweb.asm.signature.SignatureVisitorvisitReturnType()

        endFormals();
        SignatureSourcer sourcer = new SignatureSourcer();
        assert mReturnType == null;
        mReturnType = sourcer;
        return sourcer;
    
public org.objectweb.asm.signature.SignatureVisitorvisitSuperclass()

        endFormals();
        SignatureSourcer sourcer = new SignatureSourcer();
        assert mSuperClass == null;
        mSuperClass = sourcer;
        return sourcer;
    
public voidvisitTypeArgument()

        if (mArgumentStack % 2 == 0) {
            ++mArgumentStack;
            getBuf().append('<");
        } else {
            getBuf().append(", ");
        }
        getBuf().append('*");
    
public org.objectweb.asm.signature.SignatureVisitorvisitTypeArgument(char wildcard)

        if (mArgumentStack % 2 == 0) {
            ++mArgumentStack;
            getBuf().append('<");
        } else {
            getBuf().append(", ");
        }
        if (wildcard != '=") {
            if (wildcard == '+") {
                getBuf().append("? extends ");
            } else if (wildcard == '-") {
                getBuf().append("? super ");
            } else {
                // can this happen?
                getBuf().append(wildcard);
            }
        }
        return this;
    
public voidvisitTypeVariable(java.lang.String name)

        getBuf().append(name.replace('/", '."));