Skip to content

Commit

Permalink
upgrade asm api from ASM4 to ASM5.
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchengdong committed Jan 8, 2019
1 parent b60d88e commit f432967
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AutoInjectPlugin implements Plugin<Project> {
def isApp = project.plugins.hasPlugin(AppPlugin)
if (!isApp) return

project.extensions.create("autoInject", AutoInjectExtension.class);
project.extensions.create("autoInject", AutoInjectExtension.class)

def android = project.extensions.getByType(AppExtension)
android.registerTransform(new AutoInjectTransform(project))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.gradle.api.Project

class AutoInjectTransform extends Transform {

private Project project;
private Project project

public AutoInjectTransform(Project project) {
this.project = project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class AutoInjector {
source.eachFileRecurse { File file ->
String filename = file.getName()
if (filterClass(filename)) return
ClassReader classReader = new ClassReader(file.readBytes());
classReader.accept(bowArrowClassAdapter, 0);
ClassReader classReader = new ClassReader(file.readBytes())
classReader.accept(bowArrowClassAdapter, 0)
}
} else {
JarFile jarFile = new JarFile(source)
Expand All @@ -40,11 +40,12 @@ class AutoInjector {

InputStream stream = jarFile.getInputStream(entry)
if (stream != null) {
ClassReader classReader = new ClassReader(stream.bytes);
classReader.accept(bowArrowClassAdapter, 0);
ClassReader classReader = new ClassReader(stream.bytes)
classReader.accept(bowArrowClassAdapter, 0)
stream.close()
}
}
jarFile.close()
}
}

Expand All @@ -60,9 +61,9 @@ class AutoInjector {
byte[] bytes = findTargetAndInject(source, file.readBytes())
if (bytes != null) {
Logger.i('-- replace class [' + file.absolutePath + ']')
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(bytes);
outputStream.close();
FileOutputStream outputStream = new FileOutputStream(file)
outputStream.write(bytes)
outputStream.close()
}
}
} else {
Expand Down Expand Up @@ -115,17 +116,18 @@ class AutoInjector {
jarOutputStream.close()

Logger.i('-- replace jar [' + source.absolutePath + ']')
FileOutputStream outputStream = new FileOutputStream(source);
outputStream.write(tempJar.bytes);
outputStream.close();
FileOutputStream outputStream = new FileOutputStream(source)
outputStream.write(tempJar.bytes)
outputStream.close()
tempJar.delete()
}
jarFile.close()
}
}

protected static byte[] findTargetAndInject(File source, byte[] bytes) {
ClassWriter classWriter = new ClassWriter(0)
// TraceClassVisitor traceClassVisitor = new TraceClassVisitor(classWriter, new PrintWriter(System.out));
// TraceClassVisitor traceClassVisitor = new TraceClassVisitor(classWriter, new PrintWriter(System.out))
boolean methodInject
OnMethodInjectListener onMethodInjectListener = new OnMethodInjectListener() {
@Override
Expand All @@ -135,7 +137,7 @@ class AutoInjector {
}
targetClassAdapter.set(classWriter, onMethodInjectListener)
try {
ClassReader classReader = new ClassReader(bytes);
ClassReader classReader = new ClassReader(bytes)
classReader.accept(targetClassAdapter, 0)
} catch (Exception e) {
String tip = "\nRead class failed when find target in source: " + source.name + "[" + source.absolutePath + "]."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AnnotationAdapter extends AnnotationVisitor {
OnAnnotationValueListener valueListener

AnnotationAdapter(AnnotationVisitor annotationVisitor, OnAnnotationValueListener listener) {
super(Opcodes.ASM4, annotationVisitor)
super(Opcodes.ASM5, annotationVisitor)
this.valueListener = listener
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BowArrowClassAdapter extends ClassVisitor {
private List<String> getMethodDescList

BowArrowClassAdapter() {
super(Opcodes.ASM4)
super(Opcodes.ASM5)
}

@Override
Expand Down Expand Up @@ -78,7 +78,7 @@ class BowArrowClassAdapter extends ClassVisitor {
if (autoClassInfo != null) {
if (name == '<init>') {
if (autoClassInfo.initDesc != null) {
throw new GradleScriptException("class " + classname + " has multi-<init>.", null);
throw new GradleScriptException("class " + classname + " has multi-<init>.", null)
} else if (context && desc == '()V') {
throw new GradleScriptException("class " + classname + " need context, but has a non-parameter constructor.", null)
} else if (!context && desc != '()V') {
Expand All @@ -101,7 +101,7 @@ class BowArrowClassAdapter extends ClassVisitor {
AutoType autoType = autoClassInfo.getAutoType()
if (autoType == AutoType.ARROW) {
if (!interfaces.contains(AUTO_ARROW_INTERFACE_BYTECODE)) {
throw new GradleScriptException("class " + classname + " should implements " + AUTO_ARROW_INTERFACE_BYTECODE + " directly.", null);
throw new GradleScriptException("class " + classname + " should implements " + AUTO_ARROW_INTERFACE_BYTECODE + " directly.", null)
}

if (this.signature == null) {
Expand All @@ -110,16 +110,16 @@ class BowArrowClassAdapter extends ClassVisitor {

String returnDesc = getReturnDesc()
if (returnDesc == null) {
throw new GradleScriptException("can't find returnDesc of class " + classname + ".get(...).", null);
throw new GradleScriptException("can't find returnDesc of class " + classname + ".get(...).", null)
}
autoClassInfo.returnDesc = returnDesc
} else if (autoType == AutoType.BOW) {
if (!interfaces.contains(AUTO_BOW_INTERFACE_BYTECODE)) {
throw new GradleScriptException("class " + classname + " should implements " + AUTO_BOW_INTERFACE_BYTECODE + " directly.", null);
throw new GradleScriptException("class " + classname + " should implements " + AUTO_BOW_INTERFACE_BYTECODE + " directly.", null)
}
} else if (autoType == AutoType.BOW_ARROW) {
if (!interfaces.contains(AUTO_BOW_ARROW_INTERFACE_BYTECODE)) {
throw new GradleScriptException("class " + classname + " should implements " + AUTO_BOW_ARROW_INTERFACE_BYTECODE + " directly.", null);
throw new GradleScriptException("class " + classname + " should implements " + AUTO_BOW_ARROW_INTERFACE_BYTECODE + " directly.", null)
}
}
autoClassInfo.className = classname
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TargetClassAdapter extends ClassVisitor {
String className

TargetClassAdapter() {
super(Opcodes.ASM4)
super(Opcodes.ASM5)
}

void set(ClassVisitor classWriter, OnMethodInjectListener onMethodInjectListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TargetClassMethodAdapter extends MethodVisitor {
private boolean contextStack

TargetClassMethodAdapter() {
super(Opcodes.ASM4)
super(Opcodes.ASM5)
}

void set(MethodVisitor methodVisitor, String className, String methodName, String desc) {
Expand Down Expand Up @@ -101,7 +101,7 @@ class TargetClassMethodAdapter extends MethodVisitor {
mv.visitInsn(Opcodes.DUP)
if (autoClassInfo.initDesc != "()V") {
contextStack = true
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 0)
}
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, autoClassInfo.className, '<init>', autoClassInfo.initDesc, false)
mv.visitVarInsn(Opcodes.ASTORE, offset + i + 1)
Expand All @@ -119,7 +119,7 @@ class TargetClassMethodAdapter extends MethodVisitor {
mv.visitInsn(Opcodes.DUP)
if (autoClassInfo.initDesc != "()V") {
contextStack = true
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 0)
}
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, autoClassInfo.className, '<init>', autoClassInfo.initDesc, false)
mv.visitVarInsn(Opcodes.ASTORE, offset + i + 1)
Expand All @@ -131,7 +131,7 @@ class TargetClassMethodAdapter extends MethodVisitor {

Logger.i("-- arrow , model: " + autoClassInfo.model + ', className: ' + autoClassInfo.className + ', returnDesc: ' + autoClassInfo.returnDesc)
} else {
throw new GradleScriptException("Unknown AutoType: " + autoClassInfo.toString(), null);
throw new GradleScriptException("Unknown AutoType: " + autoClassInfo.toString(), null)
}
}
}
Expand Down

0 comments on commit f432967

Please sign in to comment.