Example usage for org.objectweb.asm.signature SignatureReader accept

List of usage examples for org.objectweb.asm.signature SignatureReader accept

Introduction

In this page you can find the example usage for org.objectweb.asm.signature SignatureReader accept.

Prototype

public void accept(final SignatureVisitor signatureVistor) 

Source Link

Document

Makes the given visitor visit the signature of this SignatureReader .

Usage

From source file:com.android.build.gradle.shrinker.DependencyFinderVisitor.java

License:Apache License

@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {//from www .j  a  va  2s  . c  om
    if (interfaces == null) {
        interfaces = new String[0];
    }

    mKlass = mGraph.getClassReference(name);
    if (superName != null && !isSdkPackage(superName)) {
        // Don't create graph edges for obvious things.
        handleDependency(mKlass, mGraph.getClassReference(superName), DependencyType.REQUIRED_CLASS_STRUCTURE);
    }

    if (interfaces.length > 0) {
        handleInterfaceInheritance(mKlass);

        if (!Objects.equal(superName, "java/lang/Object")) {
            // It's possible the superclass is implementing a method from the interface, we may
            // need to add more edges to the graph to represent this.
            handleMultipleInheritance(mKlass);
        }
    }

    mClassName = name;
    mIsAnnotation = (access & Opcodes.ACC_ANNOTATION) != 0;

    if (signature != null) {
        SignatureReader reader = new SignatureReader(signature);
        SignatureVisitor visitor = new DependencyFinderSignatureVisitor();
        reader.accept(visitor);
    }

    super.visit(version, access, name, signature, superName, interfaces);
}

From source file:com.android.build.gradle.shrinker.DependencyFinderVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    T method = mGraph.getMemberReference(mClassName, name, desc);

    if ((access & Opcodes.ACC_STATIC) == 0 && !name.equals(ByteCodeUtils.CONSTRUCTOR)) {
        handleVirtualMethod(method);//from  w  w w . jav a2 s  .  c o m
    }

    // Keep class initializers, but also keep all default constructors. This is the ProGuard
    // behavior,
    if (name.equals(ByteCodeUtils.CLASS_INITIALIZER)
            || (name.equals(ByteCodeUtils.CONSTRUCTOR) && desc.equals("()V"))) {
        handleDependency(mKlass, method, DependencyType.REQUIRED_CLASS_STRUCTURE);
    }

    if (mIsAnnotation) {
        // TODO: Strip unused annotation classes members.
        handleDependency(mKlass, method, DependencyType.REQUIRED_CLASS_STRUCTURE);
    }

    if (signature != null) {
        SignatureReader reader = new SignatureReader(signature);
        SignatureVisitor visitor = new DependencyFinderSignatureVisitor();
        reader.accept(visitor);
    }

    return new DependencyFinderMethodVisitor(method, desc,
            super.visitMethod(access, name, desc, signature, exceptions));
}

From source file:com.android.builder.shrinker.DependencyFinderVisitor.java

License:Apache License

private void handleClassSignature(T source, String signature) {
    SignatureReader reader = new SignatureReader(signature);
    SignatureVisitor visitor = new DependencyFinderSignatureVisitor(source);
    reader.accept(visitor);
}

From source file:com.android.mkstubs.sourcer.ClassSourcer.java

License:Apache License

@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {// www  . java2s.c o m

    String pkg = name.substring(0, name.lastIndexOf('/')).replace('/', '.');
    mClassName = name.substring(name.lastIndexOf('/') + 1);

    mOutput.write("package %s;\n", pkg);

    // dump access keywords. Note: do not dump "super" here
    mAccessSourcer.write(access & ~Opcodes.ACC_SUPER, AccessSourcer.IS_CLASS);

    // write class name
    mOutput.write(" class %s", mClassName);

    if (signature != null) {
        // write template formal definition and super type
        SignatureReader sigReader = new SignatureReader(signature);
        SignatureSourcer sigSourcer = new SignatureSourcer();
        sigReader.accept(sigSourcer);

        if (sigSourcer.hasFormalsContent()) {
            mOutput.write(sigSourcer.formalsToString());
        }

        mOutput.write(" extends %s", sigSourcer.getSuperClass().toString());

    } else {
        // write non-generic super type
        mOutput.write(" extends %s", superName.replace('/', '.'));
    }

    // write interfaces defined, if any
    if (interfaces != null && interfaces.length > 0) {
        mOutput.write(" implements ");
        boolean need_sep = false;
        for (String i : interfaces) {
            if (need_sep) {
                mOutput.write(", ");
            }
            mOutput.write(i.replace('/', '.'));
            need_sep = true;
        }
    }

    // open class body
    mOutput.write(" {\n");
}

From source file:com.android.mkstubs.sourcer.MethodSourcer.java

License:Apache License

private void writeHeader() {
    if (!mNeedDeclaration) {
        return;//  w  w  w .j  a v a 2 s.  com
    }

    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;
}

From source file:com.android.mkstubs.sourcer.SignatureSourcerTest.java

License:Apache License

@Test
public void testReturnMapNoArgs() {
    SignatureReader reader = new SignatureReader(
            "()Ljava/util/Map<Ljava/util/ArrayList<TT;>;Ljava/util/Map<Ljava/lang/String;Ljava/util/ArrayList<TU;>;>;>;");
    reader.accept(mSourcer);
    String result = mSourcer.getReturnType().toString();

    Assert.assertEquals(/*from  w  w w . j a v  a 2  s. c o m*/
            "java.util.Map<java.util.ArrayList<T>, java.util.Map<java.lang.String, java.util.ArrayList<U>>>",
            result);
}

From source file:com.android.mkstubs.sourcer.SignatureSourcerTest.java

License:Apache License

@Test
public void testReturnVoid() {
    SignatureReader reader = new SignatureReader("(Ljava/util/List<+Lorg/w3c/dom/css/Rect;>;)V");
    reader.accept(mSourcer);
    String result = mSourcer.getReturnType().toString();

    Assert.assertEquals("void", result);
}

From source file:com.android.mkstubs.sourcer.SignatureSourcerTest.java

License:Apache License

@Test
public void testSimpleArg() {
    SignatureReader reader = new SignatureReader("(Ljava/util/List<+Lorg/w3c/dom/css/Rect;>;)V");
    reader.accept(mSourcer);

    ArrayList<SignatureSourcer> params = mSourcer.getParameters();
    Assert.assertNotNull(params);// w w w  .  j a  v a2s .c  o  m

    String[] array = toStringArray(params);

    Assert.assertArrayEquals(new String[] { "java.util.List<? extends org.w3c.dom.css.Rect>" }, array);
}

From source file:com.android.mkstubs.sourcer.SignatureSourcerTest.java

License:Apache License

@Test
public void testFormalParameters1() {
    SignatureReader reader = new SignatureReader("<X:TT;Y:Ljava/lang/Object;>()V");
    reader.accept(mSourcer);

    Assert.assertTrue(mSourcer.hasFormalsContent());

    String result = mSourcer.formalsToString();
    Assert.assertEquals("<X extends T, Y extends java.lang.Object>", result);
}

From source file:com.android.mkstubs.sourcer.SignatureSourcerTest.java

License:Apache License

@Test
public void testFormalParameters2() {
    SignatureReader reader = new SignatureReader("<T::Ljava/lang/Comparable<-TT;>;>(Ljava/util/List<TT;>;)V");
    reader.accept(mSourcer);

    Assert.assertTrue(mSourcer.hasFormalsContent());

    String result = mSourcer.formalsToString();
    Assert.assertEquals("<T extends java.lang.Comparable<? super T>>", result);
}