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

MethodSourcer

public class MethodSourcer extends Object implements org.objectweb.asm.MethodVisitor
A method visitor that generates the Java source for a whole method.

Fields Summary
private final Output
mOutput
private final int
mAccess
private final String
mClassName
private final String
mName
private final String
mDesc
private final String
mSignature
private final String[]
mExceptions
private boolean
mNeedDeclaration
private boolean
mIsConstructor
Constructors Summary
public MethodSourcer(Output output, String className, int access, String name, String desc, String signature, String[] exceptions)

        mOutput = output;
        mClassName = className;
        mAccess = access;
        mName = name;
        mDesc = desc;
        mSignature = signature;
        mExceptions = exceptions;
        
        mNeedDeclaration = true;
        mIsConstructor = "<init>".equals(name);
    
Methods Summary
public org.objectweb.asm.AnnotationVisitorvisitAnnotation(java.lang.String desc, boolean visible)

        mOutput.write("@%s", desc);
        return new AnnotationSourcer(mOutput);
    
public org.objectweb.asm.AnnotationVisitorvisitAnnotationDefault()

        // pass
        return null;
    
public voidvisitAttribute(org.objectweb.asm.Attribute attr)

        mOutput.write("%s /* non-standard method attribute */ ", attr.type);
    
public voidvisitCode()

        writeHeader();
        
        // write the stub itself
        mOutput.write("throw new RuntimeException(\"Stub\");");
    
public voidvisitEnd()

        writeHeader();
        mOutput.write("\n}\n");
    
public voidvisitFieldInsn(int opcode, java.lang.String owner, java.lang.String name, java.lang.String desc)

        // pass
    
public voidvisitFrame(int type, int local, java.lang.Object[] local2, int stack, java.lang.Object[] stack2)

        // pass
    
public voidvisitIincInsn(int var, int increment)

        // pass
    
public voidvisitInsn(int opcode)

        // pass
    
public voidvisitIntInsn(int opcode, int operand)

        // pass
    
public voidvisitJumpInsn(int opcode, org.objectweb.asm.Label label)

        // pass
    
public voidvisitLabel(org.objectweb.asm.Label label)

        // pass
    
public voidvisitLdcInsn(java.lang.Object cst)

        // pass
    
public voidvisitLineNumber(int line, org.objectweb.asm.Label start)

        // pass
    
public voidvisitLocalVariable(java.lang.String name, java.lang.String desc, java.lang.String signature, org.objectweb.asm.Label start, org.objectweb.asm.Label end, int index)

        // pass
    
public voidvisitLookupSwitchInsn(org.objectweb.asm.Label dflt, int[] keys, org.objectweb.asm.Label[] labels)

        // pass
    
public voidvisitMaxs(int maxStack, int maxLocals)

        // pass
    
public voidvisitMethodInsn(int opcode, java.lang.String owner, java.lang.String name, java.lang.String desc)

        // pass
    
public voidvisitMultiANewArrayInsn(java.lang.String desc, int dims)

        // pass
    
public org.objectweb.asm.AnnotationVisitorvisitParameterAnnotation(int parameter, java.lang.String desc, boolean visible)

        // pass
        return null;
    
public voidvisitTableSwitchInsn(int min, int max, org.objectweb.asm.Label dflt, org.objectweb.asm.Label[] labels)

        // pass
    
public voidvisitTryCatchBlock(org.objectweb.asm.Label start, org.objectweb.asm.Label end, org.objectweb.asm.Label handler, java.lang.String type)

        // pass
    
public voidvisitTypeInsn(int opcode, java.lang.String type)

        // pass
    
public voidvisitVarInsn(int opcode, int var)

        // pass
    
private voidwriteHeader()

        
        if (!mNeedDeclaration) {
            return;
        }
        
        AccessSourcer as = new AccessSourcer(mOutput);
        as.write(mAccess, AccessSourcer.IS_METHOD);

        // preprocess the signature to get the return type and the arguments
        SignatureSourcer sigSourcer = null;
        if (mSignature != null) {
            SignatureReader sigReader = new SignatureReader(mSignature);
            sigSourcer = new SignatureSourcer();
            sigReader.accept(sigSourcer);
            
            if (sigSourcer.hasFormalsContent()) {
                // dump formal template parameter definitions
                mOutput.write(" %s", sigSourcer.formalsToString());
            }
        }
        
        // output return type (constructor have no return type)
        if (!mIsConstructor) {
            // The signature overrides desc, if present
            if (sigSourcer == null || sigSourcer.getReturnType() == null) {
                mOutput.write(" %s", Type.getReturnType(mDesc).getClassName());
                
            } else {
                mOutput.write(" %s", sigSourcer.getReturnType().toString());
            }
        }

        // output name
        mOutput.write(" %s(", mIsConstructor ? mClassName : mName);
            
        // output arguments. The signature overrides desc, if present
        if (mSignature == null) {
            Type[] types = Type.getArgumentTypes(mDesc);
            
            for(int i = 0; i < types.length; i++) {
                if (i > 0) {
                    mOutput.write(", ");
                }
                mOutput.write("%s arg%d", types[i].getClassName(), i);
            }
        } else {
            ArrayList<SignatureSourcer> params = sigSourcer.getParameters();
            
            for(int i = 0; i < params.size(); i++) {
                if (i > 0) {
                    mOutput.write(", ");
                }
                mOutput.write("%s arg%d", params.get(i).toString(), i);
            }
        }
        mOutput.write(")");

        // output throwable exceptions
        if (mExceptions != null && mExceptions.length > 0) {
            mOutput.write(" throws ");
            
            for (int i = 0; i < mExceptions.length; i++) {
                if (i > 0) {
                    mOutput.write(", ");
                }
                mOutput.write(mExceptions[i].replace('/", '."));
            }
        }

        mOutput.write(" {\n");

        mNeedDeclaration = false;