Merge "Upgrade layoutlib generation code to use ASM 4.0"

This commit is contained in:
Tor Norbye
2011-12-06 13:31:04 -08:00
committed by Android (Google) Code Review
9 changed files with 427 additions and 256 deletions

View File

@@ -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>

View File

@@ -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

View File

@@ -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;
@@ -45,7 +46,7 @@ import java.util.zip.ZipFile;
public class AsmAnalyzer {
// Note: a bunch of stuff has package-level access for unit tests. Consider it private.
/** Output logger. */
private final Log mLog;
/** The input source JAR to parse. */
@@ -59,11 +60,11 @@ public class AsmAnalyzer {
/**
* Creates a new analyzer.
*
*
* @param log The log output.
* @param osJarPath The input source JARs to parse.
* @param gen The generator to fill with the class list and dependency list.
* @param deriveFrom Keep all classes that derive from these one (these included).
* @param deriveFrom Keep all classes that derive from these one (these included).
* @param includeGlobs Glob patterns of classes to keep, e.g. "com.foo.*"
* ("*" does not matches dots whilst "**" does, "." and "$" are interpreted as-is)
*/
@@ -83,14 +84,14 @@ public class AsmAnalyzer {
public void analyze() throws IOException, LogAbortException {
AsmAnalyzer visitor = this;
Map<String, ClassReader> zipClasses = parseZip(mOsSourceJar);
mLog.info("Found %d classes in input JAR%s.", zipClasses.size(),
mOsSourceJar.size() > 1 ? "s" : "");
Map<String, ClassReader> found = findIncludes(zipClasses);
Map<String, ClassReader> deps = findDeps(zipClasses, found);
if (mGen != null) {
mGen.setKeep(found);
mGen.setDeps(deps);
@@ -117,10 +118,10 @@ public class AsmAnalyzer {
}
}
}
return classes;
}
/**
* Utility that returns the fully qualified binary class name for a ClassReader.
* E.g. it returns something like android.view.View.
@@ -132,7 +133,7 @@ public class AsmAnalyzer {
return classReader.getClassName().replace('/', '.');
}
}
/**
* Utility that returns the fully qualified binary class name from a path-like FQCN.
* E.g. it returns android.view.View from android/view/View.
@@ -144,7 +145,7 @@ public class AsmAnalyzer {
return className.replace('/', '.');
}
}
/**
* Process the "includes" arrays.
* <p/>
@@ -162,11 +163,11 @@ public class AsmAnalyzer {
for (String s : mDeriveFrom) {
findClassesDerivingFrom(s, zipClasses, found);
}
return found;
}
/**
* Uses ASM to find the class reader for the given FQCN class name.
* If found, insert it in the in_out_found map.
@@ -215,7 +216,7 @@ public class AsmAnalyzer {
globPattern += "$";
Pattern regexp = Pattern.compile(globPattern);
for (Entry<String, ClassReader> entry : zipClasses.entrySet()) {
String class_name = entry.getKey();
if (regexp.matcher(class_name).matches()) {
@@ -284,7 +285,7 @@ public class AsmAnalyzer {
for (ClassReader cr : inOutKeepClasses.values()) {
cr.accept(visitor, 0 /* flags */);
}
while (new_deps.size() > 0 || new_keep.size() > 0) {
deps.putAll(new_deps);
inOutKeepClasses.putAll(new_keep);
@@ -308,15 +309,14 @@ public class AsmAnalyzer {
return deps;
}
// ----------------------------------
/**
* Visitor to collect all the type dependencies from a class.
* 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;
@@ -333,7 +333,7 @@ public class AsmAnalyzer {
* Creates a new visitor that will find all the dependencies for the visited class.
* Types which are already in the zipClasses, keepClasses or inDeps are not marked.
* New dependencies are marked in outDeps.
*
*
* @param zipClasses All classes found in the source JAR.
* @param inKeep Classes from which dependencies are to be found.
* @param inDeps Dependencies already known.
@@ -344,13 +344,14 @@ public class AsmAnalyzer {
Map<String, ClassReader> outKeep,
Map<String,ClassReader> inDeps,
Map<String,ClassReader> outDeps) {
super(Opcodes.ASM4);
mZipClasses = zipClasses;
mInKeep = inKeep;
mOutKeep = outKeep;
mInDeps = inDeps;
mOutDeps = outDeps;
}
/**
* Considers the given class name as a dependency.
* If it does, add to the mOutDeps map.
@@ -361,7 +362,7 @@ public class AsmAnalyzer {
}
className = internalToBinaryClassName(className);
// exclude classes that have already been found
if (mInKeep.containsKey(className) ||
mOutKeep.containsKey(className) ||
@@ -384,7 +385,7 @@ public class AsmAnalyzer {
} catch (ClassNotFoundException e) {
// ignore
}
// accept this class:
// - android classes are added to dependencies
// - non-android classes are added to the list of classes to keep as-is (they don't need
@@ -395,7 +396,7 @@ public class AsmAnalyzer {
mOutKeep.put(className, cr);
}
}
/**
* Considers this array of names using considerName().
*/
@@ -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());
}
}
@@ -450,17 +451,18 @@ public class AsmAnalyzer {
}
}
// ---------------------------------------------------
// --- ClassVisitor, FieldVisitor
// ---------------------------------------------------
// 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
// one, and does not extend or implement generic classes or interfaces.
if (signature != null) {
considerSignature(signature);
}
@@ -468,27 +470,57 @@ public class AsmAnalyzer {
// superName is the internal of name of the super class (see getInternalName).
// For interfaces, the super class is Object. May be null but only for the Object class.
considerName(superName);
// interfaces is the internal names of the class's interfaces (see getInternalName).
// May be null.
considerNames(interfaces);
}
@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
}
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 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).
@@ -513,239 +547,299 @@ public class AsmAnalyzer {
// signature is the method's signature. May be null if the method parameters, return
// 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
}
// ---------------------------------------------------
// --- MethodVisitor
// ---------------------------------------------------
public AnnotationVisitor visitAnnotationDefault() {
return this; // returns this to visit the default value
}
private class MyMethodVisitor extends MethodVisitor {
public MyMethodVisitor() {
super(Opcodes.ASM4);
}
public void visitCode() {
// pass
}
@Override
public AnnotationVisitor visitAnnotationDefault() {
return new MyAnnotationVisitor();
}
// field instruction
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
// name is the field's name.
considerName(name);
// desc is the field's descriptor (see Type).
considerDesc(desc);
}
@Override
public void visitCode() {
// pass
}
public void visitFrame(int type, int local, Object[] local2, int stack, Object[] stack2) {
// pass
}
// field instruction
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
// name is the field's name.
considerName(name);
// desc is the field's descriptor (see Type).
considerDesc(desc);
}
public void visitIincInsn(int var, int increment) {
// pass -- an IINC instruction
}
@Override
public void visitFrame(int type, int local, Object[] local2, int stack, Object[] stack2) {
// pass
}
public void visitInsn(int opcode) {
// pass -- a zero operand instruction
}
@Override
public void visitIincInsn(int var, int increment) {
// pass -- an IINC instruction
}
public void visitIntInsn(int opcode, int operand) {
// pass -- a single int operand instruction
}
@Override
public void visitInsn(int opcode) {
// pass -- a zero operand instruction
}
public void visitJumpInsn(int opcode, Label label) {
// pass -- a jump instruction
}
@Override
public void visitIntInsn(int opcode, int operand) {
// pass -- a single int operand instruction
}
public void visitLabel(Label label) {
// pass -- a label target
}
@Override
public void visitJumpInsn(int opcode, Label label) {
// pass -- a jump instruction
}
// instruction to load a constant from the stack
public void visitLdcInsn(Object cst) {
if (cst instanceof Type) {
considerType((Type) cst);
@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.
considerDesc(desc);
// signature is the type signature of this local variable. May be null if the local
// variable type does not use generic types.
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
considerName(owner);
// desc is the method's descriptor (see Type).
considerDesc(desc);
}
// 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 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).
considerName(type);
}
// 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
}
}
public void visitLineNumber(int line, Label start) {
// pass
}
private class MySignatureVisitor extends SignatureVisitor {
public void visitLocalVariable(String name, String desc,
String signature, Label start, Label end, int index) {
// desc is the type descriptor of this local variable.
considerDesc(desc);
// signature is the type signature of this local variable. May be null if the local
// variable type does not use generic types.
considerSignature(signature);
}
public MySignatureVisitor() {
super(Opcodes.ASM4);
}
public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
// pass -- a lookup switch instruction
}
// ---------------------------------------------------
// --- SignatureVisitor
// ---------------------------------------------------
public void visitMaxs(int maxStack, int maxLocals) {
// pass
}
private String mCurrentSignatureClass = null;
// instruction that invokes a method
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
// owner is the internal name of the method's owner class
considerName(owner);
// desc is the method's descriptor (see Type).
considerDesc(desc);
}
// Starts the visit of a signature corresponding to a class or interface type
@Override
public void visitClassType(String name) {
mCurrentSignatureClass = name;
considerName(name);
}
// instruction multianewarray, whatever that is
public void visitMultiANewArrayInsn(String desc, int dims) {
// desc an array type descriptor.
considerDesc(desc);
}
// Visits an inner class
@Override
public void visitInnerClassType(String name) {
if (mCurrentSignatureClass != null) {
mCurrentSignatureClass += "$" + name;
considerName(mCurrentSignatureClass);
}
}
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
}
@Override
public SignatureVisitor visitArrayType() {
return new MySignatureVisitor();
}
public void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels) {
// pass -- table switch instruction
}
@Override
public void visitBaseType(char descriptor) {
// pass -- a primitive type, ignored
}
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).
considerName(type);
}
@Override
public SignatureVisitor visitClassBound() {
return new MySignatureVisitor();
}
// type instruction
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 SignatureVisitor visitExceptionType() {
return new MySignatureVisitor();
}
public void visitVarInsn(int opcode, int var) {
// pass -- local variable instruction
}
@Override
public void visitFormalTypeParameter(String name) {
// pass
}
// ---------------------------------------------------
// --- SignatureVisitor
// ---------------------------------------------------
@Override
public SignatureVisitor visitInterface() {
return new MySignatureVisitor();
}
private String mCurrentSignatureClass = null;
@Override
public SignatureVisitor visitInterfaceBound() {
return new MySignatureVisitor();
}
// Starts the visit of a signature corresponding to a class or interface type
public void visitClassType(String name) {
mCurrentSignatureClass = name;
considerName(name);
}
@Override
public SignatureVisitor visitParameterType() {
return new MySignatureVisitor();
}
// Visits an inner class
public void visitInnerClassType(String name) {
if (mCurrentSignatureClass != null) {
mCurrentSignatureClass += "$" + name;
considerName(mCurrentSignatureClass);
@Override
public SignatureVisitor visitReturnType() {
return new MySignatureVisitor();
}
@Override
public SignatureVisitor visitSuperclass() {
return new MySignatureVisitor();
}
@Override
public SignatureVisitor visitTypeArgument(char wildcard) {
return new MySignatureVisitor();
}
@Override
public void visitTypeVariable(String name) {
// pass
}
@Override
public void visitTypeArgument() {
// pass
}
}
public SignatureVisitor visitArrayType() {
return this; // returns this to visit the signature of the array element type
}
public void visitBaseType(char descriptor) {
// pass -- a primitive type, ignored
}
public SignatureVisitor visitClassBound() {
return this; // returns this to visit the signature of the class bound
}
public SignatureVisitor visitExceptionType() {
return this; // return this to visit the signature of the exception type.
}
public void visitFormalTypeParameter(String name) {
// pass
}
public SignatureVisitor visitInterface() {
return this; // returns this to visit the signature of the interface type
}
public SignatureVisitor visitInterfaceBound() {
return this; // returns this to visit the signature of the interface bound
}
public SignatureVisitor visitParameterType() {
return this; // returns this to visit the signature of the parameter type
}
public SignatureVisitor visitReturnType() {
return this; // returns this to visit the signature of the return type
}
public SignatureVisitor visitSuperclass() {
return this; // returns this to visit the signature of the super class type
}
public SignatureVisitor visitTypeArgument(char wildcard) {
return this; // returns this to visit the signature of the type argument
}
public void visitTypeVariable(String name) {
// pass
}
public void visitTypeArgument() {
// pass
}
// ---------------------------------------------------
// --- AnnotationVisitor
// ---------------------------------------------------
private class MyAnnotationVisitor extends AnnotationVisitor {
// Visits a primitive value of an annotation
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
if (value instanceof Type) {
considerType((Type) value);
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
if (value instanceof Type) {
considerType((Type) value);
}
}
@Override
public AnnotationVisitor visitAnnotation(String name, String desc) {
// desc is the class descriptor of the nested annotation class.
considerDesc(desc);
return new MyAnnotationVisitor();
}
@Override
public AnnotationVisitor visitArray(String name) {
return new MyAnnotationVisitor();
}
@Override
public void visitEnum(String name, String desc, String value) {
// desc is the class descriptor of the enumeration class.
considerDesc(desc);
}
}
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
}
public AnnotationVisitor visitArray(String name) {
return this; // returns this to visit the actual array value elements
}
public void visitEnum(String name, String desc, String value) {
// desc is the class descriptor of the enumeration class.
considerDesc(desc);
}
}
}

View File

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

View File

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

View File

@@ -71,7 +71,7 @@ import java.util.ArrayList;
* Instances of this class are not re-usable.
* The class adapter creates a new instance for each method.
*/
class DelegateMethodAdapter2 implements MethodVisitor {
class DelegateMethodAdapter2 extends MethodVisitor {
/** Suffix added to delegate classes. */
public static final String DELEGATE_SUFFIX = "_Delegate";
@@ -121,6 +121,7 @@ class DelegateMethodAdapter2 implements MethodVisitor {
String methodName,
String desc,
boolean isStatic) {
super(Opcodes.ASM4);
mLog = log;
mOrgWriter = mvOriginal;
mDelWriter = mvDelegate;
@@ -265,6 +266,7 @@ class DelegateMethodAdapter2 implements MethodVisitor {
}
/* Pass down to visitor writer. In this implementation, either do nothing. */
@Override
public void visitCode() {
if (mOrgWriter != null) {
mOrgWriter.visitCode();
@@ -274,6 +276,7 @@ class DelegateMethodAdapter2 implements MethodVisitor {
/*
* visitMaxs is called just before visitEnd if there was any code to rewrite.
*/
@Override
public void visitMaxs(int maxStack, int maxLocals) {
if (mOrgWriter != null) {
mOrgWriter.visitMaxs(maxStack, maxLocals);
@@ -281,6 +284,7 @@ class DelegateMethodAdapter2 implements MethodVisitor {
}
/** End of visiting. Generate the delegating code. */
@Override
public void visitEnd() {
if (mOrgWriter != null) {
mOrgWriter.visitEnd();
@@ -289,6 +293,7 @@ class DelegateMethodAdapter2 implements MethodVisitor {
}
/* Writes all annotation from the original method. */
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
if (mOrgWriter != null) {
return mOrgWriter.visitAnnotation(desc, visible);
@@ -298,6 +303,7 @@ class DelegateMethodAdapter2 implements MethodVisitor {
}
/* Writes all annotation default values from the original method. */
@Override
public AnnotationVisitor visitAnnotationDefault() {
if (mOrgWriter != null) {
return mOrgWriter.visitAnnotationDefault();
@@ -306,6 +312,7 @@ class DelegateMethodAdapter2 implements MethodVisitor {
}
}
@Override
public AnnotationVisitor visitParameterAnnotation(int parameter, String desc,
boolean visible) {
if (mOrgWriter != null) {
@@ -316,6 +323,7 @@ class DelegateMethodAdapter2 implements MethodVisitor {
}
/* Writes all attributes from the original method. */
@Override
public void visitAttribute(Attribute attr) {
if (mOrgWriter != null) {
mOrgWriter.visitAttribute(attr);
@@ -326,6 +334,7 @@ class DelegateMethodAdapter2 implements MethodVisitor {
* Only writes the first line number present in the original code so that source
* viewers can direct to the correct method, even if the content doesn't match.
*/
@Override
public void visitLineNumber(int line, Label start) {
// Capture the first line values for the new delegate method
if (mDelegateLineNumber == null) {
@@ -336,66 +345,77 @@ class DelegateMethodAdapter2 implements MethodVisitor {
}
}
@Override
public void visitInsn(int opcode) {
if (mOrgWriter != null) {
mOrgWriter.visitInsn(opcode);
}
}
@Override
public void visitLabel(Label label) {
if (mOrgWriter != null) {
mOrgWriter.visitLabel(label);
}
}
@Override
public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
if (mOrgWriter != null) {
mOrgWriter.visitTryCatchBlock(start, end, handler, type);
}
}
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
if (mOrgWriter != null) {
mOrgWriter.visitMethodInsn(opcode, owner, name, desc);
}
}
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
if (mOrgWriter != null) {
mOrgWriter.visitFieldInsn(opcode, owner, name, desc);
}
}
@Override
public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) {
if (mOrgWriter != null) {
mOrgWriter.visitFrame(type, nLocal, local, nStack, stack);
}
}
@Override
public void visitIincInsn(int var, int increment) {
if (mOrgWriter != null) {
mOrgWriter.visitIincInsn(var, increment);
}
}
@Override
public void visitIntInsn(int opcode, int operand) {
if (mOrgWriter != null) {
mOrgWriter.visitIntInsn(opcode, operand);
}
}
@Override
public void visitJumpInsn(int opcode, Label label) {
if (mOrgWriter != null) {
mOrgWriter.visitJumpInsn(opcode, label);
}
}
@Override
public void visitLdcInsn(Object cst) {
if (mOrgWriter != null) {
mOrgWriter.visitLdcInsn(cst);
}
}
@Override
public void visitLocalVariable(String name, String desc, String signature,
Label start, Label end, int index) {
if (mOrgWriter != null) {
@@ -403,30 +423,35 @@ class DelegateMethodAdapter2 implements MethodVisitor {
}
}
@Override
public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
if (mOrgWriter != null) {
mOrgWriter.visitLookupSwitchInsn(dflt, keys, labels);
}
}
@Override
public void visitMultiANewArrayInsn(String desc, int dims) {
if (mOrgWriter != null) {
mOrgWriter.visitMultiANewArrayInsn(desc, dims);
}
}
@Override
public void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels) {
if (mOrgWriter != null) {
mOrgWriter.visitTableSwitchInsn(min, max, dflt, labels);
}
}
@Override
public void visitTypeInsn(int opcode, String type) {
if (mOrgWriter != null) {
mOrgWriter.visitTypeInsn(opcode, type);
}
}
@Override
public void visitVarInsn(int opcode, int var) {
if (mOrgWriter != null) {
mOrgWriter.visitVarInsn(opcode, var);

View File

@@ -17,12 +17,12 @@
package com.android.tools.layoutlib.create;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodAdapter;
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,13 +32,13 @@ import org.objectweb.asm.signature.SignatureWriter;
* This class visitor renames a class from a given old name to a given new name.
* The class visitor will also rename all inner classes and references in the methods.
* <p/>
*
*
* For inner classes, this handles only the case where the outer class name changes.
* The inner class name should remain the same.
* The inner class name should remain the same.
*/
public class RenameClassAdapter extends ClassAdapter {
public class RenameClassAdapter extends ClassVisitor {
private final String mOldName;
private final String mNewName;
private String mOldBase;
@@ -50,10 +50,10 @@ public class RenameClassAdapter extends ClassAdapter {
* The names must be full qualified internal ASM names (e.g. com/blah/MyClass$InnerClass).
*/
public RenameClassAdapter(ClassWriter cv, String oldName, String newName) {
super(cv);
super(Opcodes.ASM4, cv);
mOldBase = mOldName = oldName;
mNewBase = mNewName = newName;
int pos = mOldName.indexOf('$');
if (pos > 0) {
mOldBase = mOldName.substring(0, pos);
@@ -62,7 +62,7 @@ public class RenameClassAdapter extends ClassAdapter {
if (pos > 0) {
mNewBase = mNewName.substring(0, pos);
}
assert (mOldBase == null && mNewBase == null) || (mOldBase != null && mNewBase != null);
}
@@ -78,7 +78,7 @@ public class RenameClassAdapter extends ClassAdapter {
return renameType(Type.getType(desc));
}
/**
* Renames an object type, e.g. "Lcom.package.MyClass;" or an array type that has an
* object element, e.g. "[Lcom.package.MyClass;"
@@ -150,7 +150,7 @@ public class RenameClassAdapter extends ClassAdapter {
if (mOldBase != mOldName && type.equals(mOldBase)) {
return mNewBase;
}
int pos = type.indexOf('$');
if (pos == mOldBase.length() && type.startsWith(mOldBase)) {
return mNewBase + type.substring(pos);
@@ -183,7 +183,7 @@ public class RenameClassAdapter extends ClassAdapter {
sb.append(name);
}
sb.append(')');
Type ret = Type.getReturnType(desc);
String name = renameType(ret);
sb.append(name);
@@ -191,9 +191,9 @@ public class RenameClassAdapter extends ClassAdapter {
return sb.toString();
}
/**
* Renames the ClassSignature handled by ClassVisitor.visit
* Renames the ClassSignature handled by ClassVisitor.visit
* or the MethodTypeSignature handled by ClassVisitor.visitMethod.
*/
String renameTypeSignature(String sig) {
@@ -207,7 +207,7 @@ public class RenameClassAdapter extends ClassAdapter {
return sig;
}
/**
* Renames the FieldTypeSignature handled by ClassVisitor.visitField
* or MethodVisitor.visitLocalVariable.
@@ -223,17 +223,17 @@ public class RenameClassAdapter extends ClassAdapter {
return sig;
}
//----------------------------------
// Methods from the ClassAdapter
@Override
public void visit(int version, int access, String name, String signature,
String superName, String[] interfaces) {
name = renameInternalType(name);
superName = renameInternalType(superName);
signature = renameTypeSignature(signature);
super.visit(version, access, name, signature, superName, interfaces);
}
@@ -259,7 +259,7 @@ public class RenameClassAdapter extends ClassAdapter {
desc = renameTypeDesc(desc);
return super.visitAnnotation(desc, visible);
}
@Override
public FieldVisitor visitField(int access, String name, String desc,
String signature, Object value) {
@@ -267,14 +267,14 @@ public class RenameClassAdapter extends ClassAdapter {
signature = renameFieldSignature(signature);
return super.visitField(access, name, desc, signature, value);
}
//----------------------------------
/**
* A method visitor that renames all references from an old class name to a new class name.
*/
public class RenameMethodAdapter extends MethodAdapter {
public class RenameMethodAdapter extends MethodVisitor {
/**
* Creates a method visitor that renames all references from a given old name to a given new
@@ -282,13 +282,13 @@ public class RenameClassAdapter extends ClassAdapter {
* The names must be full qualified internal ASM names (e.g. com/blah/MyClass$InnerClass).
*/
public RenameMethodAdapter(MethodVisitor mv) {
super(mv);
super(Opcodes.ASM4, mv);
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
desc = renameTypeDesc(desc);
return super.visitAnnotation(desc, visible);
}
@@ -302,7 +302,7 @@ public class RenameClassAdapter extends ClassAdapter {
@Override
public void visitTypeInsn(int opcode, String type) {
type = renameInternalType(type);
super.visitTypeInsn(opcode, type);
}
@@ -321,7 +321,7 @@ public class RenameClassAdapter extends ClassAdapter {
super.visitMethodInsn(opcode, owner, name, desc);
}
@Override
public void visitLdcInsn(Object cst) {
// If cst is a Type, this means the code is trying to pull the .class constant
@@ -335,14 +335,14 @@ public class RenameClassAdapter extends ClassAdapter {
@Override
public void visitMultiANewArrayInsn(String desc, int dims) {
desc = renameTypeDesc(desc);
super.visitMultiANewArrayInsn(desc, dims);
}
@Override
public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
type = renameInternalType(type);
super.visitTryCatchBlock(start, end, handler, type);
}
@@ -351,96 +351,113 @@ public class RenameClassAdapter extends ClassAdapter {
Label start, Label end, int index) {
desc = renameTypeDesc(desc);
signature = renameFieldSignature(signature);
super.visitLocalVariable(name, desc, signature, start, end, index);
}
}
//----------------------------------
public class RenameSignatureAdapter implements SignatureVisitor {
public class RenameSignatureAdapter extends SignatureVisitor {
private final SignatureVisitor mSv;
public RenameSignatureAdapter(SignatureVisitor sv) {
super(Opcodes.ASM4);
mSv = sv;
}
@Override
public void visitClassType(String name) {
name = renameInternalType(name);
mSv.visitClassType(name);
}
@Override
public void visitInnerClassType(String name) {
name = renameInternalType(name);
mSv.visitInnerClassType(name);
}
@Override
public SignatureVisitor visitArrayType() {
SignatureVisitor sv = mSv.visitArrayType();
return new RenameSignatureAdapter(sv);
}
@Override
public void visitBaseType(char descriptor) {
mSv.visitBaseType(descriptor);
}
@Override
public SignatureVisitor visitClassBound() {
SignatureVisitor sv = mSv.visitClassBound();
return new RenameSignatureAdapter(sv);
}
@Override
public void visitEnd() {
mSv.visitEnd();
}
@Override
public SignatureVisitor visitExceptionType() {
SignatureVisitor sv = mSv.visitExceptionType();
return new RenameSignatureAdapter(sv);
}
@Override
public void visitFormalTypeParameter(String name) {
mSv.visitFormalTypeParameter(name);
}
@Override
public SignatureVisitor visitInterface() {
SignatureVisitor sv = mSv.visitInterface();
return new RenameSignatureAdapter(sv);
}
@Override
public SignatureVisitor visitInterfaceBound() {
SignatureVisitor sv = mSv.visitInterfaceBound();
return new RenameSignatureAdapter(sv);
}
@Override
public SignatureVisitor visitParameterType() {
SignatureVisitor sv = mSv.visitParameterType();
return new RenameSignatureAdapter(sv);
}
@Override
public SignatureVisitor visitReturnType() {
SignatureVisitor sv = mSv.visitReturnType();
return new RenameSignatureAdapter(sv);
}
@Override
public SignatureVisitor visitSuperclass() {
SignatureVisitor sv = mSv.visitSuperclass();
return new RenameSignatureAdapter(sv);
}
@Override
public void visitTypeArgument() {
mSv.visitTypeArgument();
}
@Override
public SignatureVisitor visitTypeArgument(char wildcard) {
SignatureVisitor sv = mSv.visitTypeArgument(wildcard);
return new RenameSignatureAdapter(sv);
}
@Override
public void visitTypeVariable(String name) {
mSv.visitTypeVariable(name);
}
}
}

View File

@@ -27,7 +27,7 @@ import org.objectweb.asm.Type;
* This method adapter rewrites a method by discarding the original code and generating
* a stub depending on the return type. Original annotations are passed along unchanged.
*/
class StubMethodAdapter implements MethodVisitor {
class StubMethodAdapter extends MethodVisitor {
private static String CONSTRUCTOR = "<init>";
private static String CLASS_INIT = "<clinit>";
@@ -50,6 +50,7 @@ class StubMethodAdapter implements MethodVisitor {
public StubMethodAdapter(MethodVisitor mv, String methodName, Type returnType,
String invokeSignature, boolean isStatic, boolean isNative) {
super(Opcodes.ASM4);
mParentVisitor = mv;
mReturnType = returnType;
mInvokeSignature = invokeSignature;
@@ -172,6 +173,7 @@ class StubMethodAdapter implements MethodVisitor {
}
/* Pass down to visitor writer. In this implementation, either do nothing. */
@Override
public void visitCode() {
mParentVisitor.visitCode();
}
@@ -181,6 +183,7 @@ class StubMethodAdapter implements MethodVisitor {
* For non-constructor, generate the messaging code and the return statement
* if it hasn't been done before.
*/
@Override
public void visitMaxs(int maxStack, int maxLocals) {
if (!mIsInitMethod && !mMessageGenerated) {
generateInvoke();
@@ -194,6 +197,7 @@ class StubMethodAdapter implements MethodVisitor {
* For non-constructor, generate the messaging code and the return statement
* if it hasn't been done before.
*/
@Override
public void visitEnd() {
if (!mIsInitMethod && !mMessageGenerated) {
generateInvoke();
@@ -204,21 +208,25 @@ class StubMethodAdapter implements MethodVisitor {
}
/* Writes all annotation from the original method. */
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return mParentVisitor.visitAnnotation(desc, visible);
}
/* Writes all annotation default values from the original method. */
@Override
public AnnotationVisitor visitAnnotationDefault() {
return mParentVisitor.visitAnnotationDefault();
}
@Override
public AnnotationVisitor visitParameterAnnotation(int parameter, String desc,
boolean visible) {
return mParentVisitor.visitParameterAnnotation(parameter, desc, visible);
}
/* Writes all attributes from the original method. */
@Override
public void visitAttribute(Attribute attr) {
mParentVisitor.visitAttribute(attr);
}
@@ -227,6 +235,7 @@ class StubMethodAdapter implements MethodVisitor {
* Only writes the first line number present in the original code so that source
* viewers can direct to the correct method, even if the content doesn't match.
*/
@Override
public void visitLineNumber(int line, Label start) {
if (mIsInitMethod || mOutputFirstLineNumber) {
mParentVisitor.visitLineNumber(line, start);
@@ -237,6 +246,7 @@ class StubMethodAdapter implements MethodVisitor {
/**
* For non-constructor, rewrite existing "return" instructions to write the message.
*/
@Override
public void visitInsn(int opcode) {
if (mIsInitMethod) {
switch (opcode) {
@@ -257,60 +267,70 @@ class StubMethodAdapter implements MethodVisitor {
}
}
@Override
public void visitLabel(Label label) {
if (mIsInitMethod) {
mParentVisitor.visitLabel(label);
}
}
@Override
public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
if (mIsInitMethod) {
mParentVisitor.visitTryCatchBlock(start, end, handler, type);
}
}
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
if (mIsInitMethod) {
mParentVisitor.visitMethodInsn(opcode, owner, name, desc);
}
}
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
if (mIsInitMethod) {
mParentVisitor.visitFieldInsn(opcode, owner, name, desc);
}
}
@Override
public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) {
if (mIsInitMethod) {
mParentVisitor.visitFrame(type, nLocal, local, nStack, stack);
}
}
@Override
public void visitIincInsn(int var, int increment) {
if (mIsInitMethod) {
mParentVisitor.visitIincInsn(var, increment);
}
}
@Override
public void visitIntInsn(int opcode, int operand) {
if (mIsInitMethod) {
mParentVisitor.visitIntInsn(opcode, operand);
}
}
@Override
public void visitJumpInsn(int opcode, Label label) {
if (mIsInitMethod) {
mParentVisitor.visitJumpInsn(opcode, label);
}
}
@Override
public void visitLdcInsn(Object cst) {
if (mIsInitMethod) {
mParentVisitor.visitLdcInsn(cst);
}
}
@Override
public void visitLocalVariable(String name, String desc, String signature,
Label start, Label end, int index) {
if (mIsInitMethod) {
@@ -318,30 +338,35 @@ class StubMethodAdapter implements MethodVisitor {
}
}
@Override
public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
if (mIsInitMethod) {
mParentVisitor.visitLookupSwitchInsn(dflt, keys, labels);
}
}
@Override
public void visitMultiANewArrayInsn(String desc, int dims) {
if (mIsInitMethod) {
mParentVisitor.visitMultiANewArrayInsn(desc, dims);
}
}
@Override
public void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels) {
if (mIsInitMethod) {
mParentVisitor.visitTableSwitchInsn(min, max, dflt, labels);
}
}
@Override
public void visitTypeInsn(int opcode, String type) {
if (mIsInitMethod) {
mParentVisitor.visitTypeInsn(opcode, type);
}
}
@Override
public void visitVarInsn(int opcode, int var) {
if (mIsInitMethod) {
mParentVisitor.visitVarInsn(opcode, var);

View File

@@ -16,7 +16,6 @@
package com.android.tools.layoutlib.create;
import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
@@ -28,7 +27,7 @@ import java.util.Set;
/**
* Class adapter that can stub some or all of the methods of the class.
*/
class TransformClassAdapter extends ClassAdapter {
class TransformClassAdapter extends ClassVisitor {
/** True if all methods should be stubbed, false if only native ones must be stubbed. */
private final boolean mStubAll;
@@ -54,7 +53,7 @@ class TransformClassAdapter extends ClassAdapter {
public TransformClassAdapter(Log logger, Set<String> stubMethods,
Set<String> deleteReturns, String className, ClassVisitor cv,
boolean stubNativesOnly, boolean hasNative) {
super(cv);
super(Opcodes.ASM4, cv);
mLog = logger;
mStubMethods = stubMethods;
mClassName = className;