Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f7270ba9 authored by Tor Norbye's avatar Tor Norbye
Browse files

Upgrade layoutlib generation code to use ASM 4.0

Change-Id: I46b3854a05677dc7cadd217efb001dbb25631fbd
parent 58915ce9
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -4,6 +4,6 @@
	<classpathentry excluding="mock_android/" kind="src" path="tests"/>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
	<classpathentry kind="var" path="ANDROID_SRC/prebuilt/common/asm/asm-3.1.jar"/>
	<classpathentry kind="var" path="ANDROID_SRC/prebuilt/common/asm/asm-4.0.jar"/>
	<classpathentry kind="output" path="bin"/>
</classpath>
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ LOCAL_SRC_FILES := $(call all-java-files-under,src)

LOCAL_JAR_MANIFEST := manifest.txt
LOCAL_STATIC_JAVA_LIBRARIES := \
	asm-3.1
	asm-4.0

LOCAL_MODULE := layoutlib_create

+293 −199
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.signature.SignatureReader;
import org.objectweb.asm.signature.SignatureVisitor;
@@ -32,8 +33,8 @@ import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@@ -315,8 +316,7 @@ public class AsmAnalyzer {
    /**
     * Visitor to collect all the type dependencies from a class.
     */
    public class DependencyVisitor
        implements ClassVisitor, FieldVisitor, MethodVisitor, SignatureVisitor, AnnotationVisitor {
    public class DependencyVisitor extends ClassVisitor {

        /** All classes found in the source JAR. */
        private final Map<String, ClassReader> mZipClasses;
@@ -344,6 +344,7 @@ public class AsmAnalyzer {
                Map<String, ClassReader> outKeep,
                Map<String,ClassReader> inDeps,
                Map<String,ClassReader> outDeps) {
            super(Opcodes.ASM4);
            mZipClasses = zipClasses;
            mInKeep = inKeep;
            mOutKeep = outKeep;
@@ -416,7 +417,7 @@ public class AsmAnalyzer {
                SignatureReader sr = new SignatureReader(signature);
                // SignatureReader.accept will call accessType so we don't really have
                // to differentiate where the signature comes from.
                sr.accept(this);
                sr.accept(new MySignatureVisitor());
            }
        }

@@ -456,6 +457,7 @@ public class AsmAnalyzer {
        // ---------------------------------------------------

        // Visits a class header
        @Override
        public void visit(int version, int access, String name,
                String signature, String superName, String[] interfaces) {
            // signature is the signature of this class. May be null if the class is not a generic
@@ -474,21 +476,51 @@ public class AsmAnalyzer {
            considerNames(interfaces);
        }


        @Override
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            // desc is the class descriptor of the annotation class.
            considerDesc(desc);
            return new MyAnnotationVisitor();
        }

        @Override
        public void visitAttribute(Attribute attr) {
            // pass
        }

        // Visits the end of a class
        @Override
        public void visitEnd() {
            // pass
        }

        private class MyFieldVisitor extends FieldVisitor {

            public MyFieldVisitor() {
                super(Opcodes.ASM4);
            }

            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                // desc is the class descriptor of the annotation class.
                considerDesc(desc);
            return this; // return this to visit annotion values
                return new MyAnnotationVisitor();
            }

            @Override
            public void visitAttribute(Attribute attr) {
                // pass
            }

            // Visits the end of a class
            @Override
            public void visitEnd() {
                // pass
            }
        }

        @Override
        public FieldVisitor visitField(int access, String name, String desc,
                String signature, Object value) {
            // desc is the field's descriptor (see Type).
@@ -498,14 +530,16 @@ public class AsmAnalyzer {
            // generic types.
            considerSignature(signature);

            return this; // a visitor to visit field annotations and attributes
            return new MyFieldVisitor();
        }

        @Override
        public void visitInnerClass(String name, String outerName, String innerName, int access) {
            // name is the internal name of an inner class (see getInternalName).
            considerName(name);
        }

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc,
                String signature, String[] exceptions) {
            // desc is the method's descriptor (see Type).
@@ -514,13 +548,15 @@ public class AsmAnalyzer {
            // type and exceptions do not use generic types.
            considerSignature(signature);

            return this; // returns this to visit the method
            return new MyMethodVisitor();
        }

        @Override
        public void visitOuterClass(String owner, String name, String desc) {
            // pass
        }

        @Override
        public void visitSource(String source, String debug) {
            // pass
        }
@@ -530,16 +566,25 @@ public class AsmAnalyzer {
        // --- MethodVisitor
        // ---------------------------------------------------

        public AnnotationVisitor visitAnnotationDefault() {
            return this; // returns this to visit the default value
        private class MyMethodVisitor extends MethodVisitor {

            public MyMethodVisitor() {
                super(Opcodes.ASM4);
            }


            @Override
            public AnnotationVisitor visitAnnotationDefault() {
                return new MyAnnotationVisitor();
            }

            @Override
            public void visitCode() {
                // pass
            }

            // field instruction
            @Override
            public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                // name is the field's name.
                considerName(name);
@@ -547,41 +592,50 @@ public class AsmAnalyzer {
                considerDesc(desc);
            }

            @Override
            public void visitFrame(int type, int local, Object[] local2, int stack, Object[] stack2) {
                // pass
            }

            @Override
            public void visitIincInsn(int var, int increment) {
                // pass -- an IINC instruction
            }

            @Override
            public void visitInsn(int opcode) {
                // pass -- a zero operand instruction
            }

            @Override
            public void visitIntInsn(int opcode, int operand) {
                // pass -- a single int operand instruction
            }

            @Override
            public void visitJumpInsn(int opcode, Label label) {
                // pass -- a jump instruction
            }

            @Override
            public void visitLabel(Label label) {
                // pass -- a label target
            }

            // instruction to load a constant from the stack
            @Override
            public void visitLdcInsn(Object cst) {
                if (cst instanceof Type) {
                    considerType((Type) cst);
                }
            }

            @Override
            public void visitLineNumber(int line, Label start) {
                // pass
            }

            @Override
            public void visitLocalVariable(String name, String desc,
                    String signature, Label start, Label end, int index) {
                // desc is the type descriptor of this local variable.
@@ -591,15 +645,18 @@ public class AsmAnalyzer {
                considerSignature(signature);
            }

            @Override
            public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
                // pass -- a lookup switch instruction
            }

            @Override
            public void visitMaxs(int maxStack, int maxLocals) {
                // pass
            }

            // instruction that invokes a method
            @Override
            public void visitMethodInsn(int opcode, String owner, String name, String desc) {

                // owner is the internal name of the method's owner class
@@ -609,24 +666,28 @@ public class AsmAnalyzer {
            }

            // instruction multianewarray, whatever that is
            @Override
            public void visitMultiANewArrayInsn(String desc, int dims) {

                // desc an array type descriptor.
                considerDesc(desc);
            }

            @Override
            public AnnotationVisitor visitParameterAnnotation(int parameter, String desc,
                    boolean visible) {
                // desc is the class descriptor of the annotation class.
                considerDesc(desc);
            return this; // return this to visit annotation values
                return new MyAnnotationVisitor();
            }

            @Override
            public void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels) {
                // pass -- table switch instruction

            }

            @Override
            public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
                // type is the internal name of the type of exceptions handled by the handler,
                // or null to catch any exceptions (for "finally" blocks).
@@ -634,16 +695,24 @@ public class AsmAnalyzer {
            }

            // type instruction
            @Override
            public void visitTypeInsn(int opcode, String type) {
                // type is the operand of the instruction to be visited. This operand must be the
                // internal name of an object or array class.
                considerName(type);
            }

            @Override
            public void visitVarInsn(int opcode, int var) {
                // pass -- local variable instruction
            }
        }

        private class MySignatureVisitor extends SignatureVisitor {

            public MySignatureVisitor() {
                super(Opcodes.ASM4);
            }

            // ---------------------------------------------------
            // --- SignatureVisitor
@@ -652,12 +721,14 @@ public class AsmAnalyzer {
            private String mCurrentSignatureClass = null;

            // Starts the visit of a signature corresponding to a class or interface type
            @Override
            public void visitClassType(String name) {
                mCurrentSignatureClass = name;
                considerName(name);
            }

            // Visits an inner class
            @Override
            public void visitInnerClassType(String name) {
                if (mCurrentSignatureClass != null) {
                    mCurrentSignatureClass += "$" + name;
@@ -665,65 +736,85 @@ public class AsmAnalyzer {
                }
            }

            @Override
            public SignatureVisitor visitArrayType() {
            return this; // returns this to visit the signature of the array element type
                return new MySignatureVisitor();
            }

            @Override
            public void visitBaseType(char descriptor) {
                // pass -- a primitive type, ignored
            }

            @Override
            public SignatureVisitor visitClassBound() {
            return this; // returns this to visit the signature of the class bound
                return new MySignatureVisitor();
            }

            @Override
            public SignatureVisitor visitExceptionType() {
            return this; // return this to visit the signature of the exception type.
                return new MySignatureVisitor();
            }

            @Override
            public void visitFormalTypeParameter(String name) {
                // pass
            }

            @Override
            public SignatureVisitor visitInterface() {
            return this; // returns this to visit the signature of the interface type
                return new MySignatureVisitor();
            }

            @Override
            public SignatureVisitor visitInterfaceBound() {
            return this; // returns this to visit the signature of the interface bound
                return new MySignatureVisitor();
            }

            @Override
            public SignatureVisitor visitParameterType() {
            return this; // returns this to visit the signature of the parameter type
                return new MySignatureVisitor();
            }

            @Override
            public SignatureVisitor visitReturnType() {
            return this; // returns this to visit the signature of the return type
                return new MySignatureVisitor();
            }

            @Override
            public SignatureVisitor visitSuperclass() {
            return this; // returns this to visit the signature of the super class type
                return new MySignatureVisitor();
            }

            @Override
            public SignatureVisitor visitTypeArgument(char wildcard) {
            return this; // returns this to visit the signature of the type argument
                return new MySignatureVisitor();
            }

            @Override
            public void visitTypeVariable(String name) {
                // pass
            }

            @Override
            public void visitTypeArgument() {
                // pass
            }
        }


        // ---------------------------------------------------
        // --- AnnotationVisitor
        // ---------------------------------------------------

        private class MyAnnotationVisitor extends AnnotationVisitor {

            public MyAnnotationVisitor() {
                super(Opcodes.ASM4);
            }

            // Visits a primitive value of an annotation
            @Override
            public void visit(String name, Object value) {
                // value is the actual value, whose type must be Byte, Boolean, Character, Short,
                // Integer, Long, Float, Double, String or Type
@@ -732,20 +823,23 @@ public class AsmAnalyzer {
                }
            }

            @Override
            public AnnotationVisitor visitAnnotation(String name, String desc) {
                // desc is the class descriptor of the nested annotation class.
                considerDesc(desc);
            return this; // returns this to visit the actual nested annotation value
                return new MyAnnotationVisitor();
            }

            @Override
            public AnnotationVisitor visitArray(String name) {
            return this; // returns this to visit the actual array value elements
                return new MyAnnotationVisitor();
            }

            @Override
            public void visitEnum(String name, String desc, String value) {
                // desc is the class descriptor of the enumeration class.
                considerDesc(desc);
            }
        
        }
    }
}
+13 −1
Original line number Diff line number Diff line
@@ -29,7 +29,10 @@ import org.objectweb.asm.Opcodes;
/**
 * Indicates if a class contains any native methods.
 */
public class ClassHasNativeVisitor implements ClassVisitor {
public class ClassHasNativeVisitor extends ClassVisitor {
    public ClassHasNativeVisitor() {
        super(Opcodes.ASM4);
    }

    private boolean mHasNativeMethods = false;

@@ -42,35 +45,42 @@ public class ClassHasNativeVisitor implements ClassVisitor {
        mHasNativeMethods = hasNativeMethods;
    }

    @Override
    public void visit(int version, int access, String name, String signature,
            String superName, String[] interfaces) {
        // pass
    }

    @Override
    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
        // pass
        return null;
    }

    @Override
    public void visitAttribute(Attribute attr) {
        // pass
    }

    @Override
    public void visitEnd() {
        // pass
    }

    @Override
    public FieldVisitor visitField(int access, String name, String desc,
            String signature, Object value) {
        // pass
        return null;
    }

    @Override
    public void visitInnerClass(String name, String outerName,
            String innerName, int access) {
        // pass
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String desc,
            String signature, String[] exceptions) {
        if ((access & Opcodes.ACC_NATIVE) != 0) {
@@ -79,10 +89,12 @@ public class ClassHasNativeVisitor implements ClassVisitor {
        return null;
    }

    @Override
    public void visitOuterClass(String owner, String name, String desc) {
        // pass
    }

    @Override
    public void visitSource(String source, String debug) {
        // pass
    }
+2 −3
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.tools.layoutlib.create;

import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
@@ -29,7 +28,7 @@ import java.util.Set;
 * <p/>
 * This is used to override specific methods and or all native methods in classes.
 */
public class DelegateClassAdapter extends ClassAdapter {
public class DelegateClassAdapter extends ClassVisitor {

    /** Suffix added to original methods. */
    private static final String ORIGINAL_SUFFIX = "_Original";
@@ -59,7 +58,7 @@ public class DelegateClassAdapter extends ClassAdapter {
            ClassVisitor cv,
            String className,
            Set<String> delegateMethods) {
        super(cv);
        super(Opcodes.ASM4, cv);
        mLog = log;
        mClassName = className;
        mDelegateMethods = delegateMethods;
Loading