From f4329678b5e6f220bca3a16ebfa15b1f2a35d122 Mon Sep 17 00:00:00 2001 From: yangchengdong Date: Tue, 8 Jan 2019 10:36:09 +0800 Subject: [PATCH] upgrade asm api from ASM4 to ASM5. --- .../autoinject/AutoInjectPlugin.groovy | 2 +- .../autoinject/AutoInjectTransform.groovy | 2 +- .../plugins/autoinject/AutoInjector.groovy | 26 ++++++++++--------- .../adapter/AnnotationAdapter.groovy | 2 +- .../adapter/BowArrowClassAdapter.groovy | 12 ++++----- .../adapter/TargetClassAdapter.groovy | 2 +- .../adapter/TargetClassMethodAdapter.groovy | 8 +++--- 7 files changed, 28 insertions(+), 26 deletions(-) diff --git a/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/AutoInjectPlugin.groovy b/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/AutoInjectPlugin.groovy index 557cbb3..5b13177 100644 --- a/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/AutoInjectPlugin.groovy +++ b/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/AutoInjectPlugin.groovy @@ -11,7 +11,7 @@ class AutoInjectPlugin implements Plugin { 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)) diff --git a/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/AutoInjectTransform.groovy b/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/AutoInjectTransform.groovy index 7fa6e46..dd77673 100644 --- a/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/AutoInjectTransform.groovy +++ b/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/AutoInjectTransform.groovy @@ -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 diff --git a/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/AutoInjector.groovy b/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/AutoInjector.groovy index 6fb7a4b..e8b83da 100644 --- a/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/AutoInjector.groovy +++ b/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/AutoInjector.groovy @@ -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) @@ -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() } } @@ -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 { @@ -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 @@ -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 + "]." diff --git a/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/AnnotationAdapter.groovy b/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/AnnotationAdapter.groovy index 7d1c872..2792b95 100644 --- a/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/AnnotationAdapter.groovy +++ b/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/AnnotationAdapter.groovy @@ -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 } diff --git a/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/BowArrowClassAdapter.groovy b/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/BowArrowClassAdapter.groovy index df571dd..5690fec 100644 --- a/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/BowArrowClassAdapter.groovy +++ b/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/BowArrowClassAdapter.groovy @@ -25,7 +25,7 @@ class BowArrowClassAdapter extends ClassVisitor { private List getMethodDescList BowArrowClassAdapter() { - super(Opcodes.ASM4) + super(Opcodes.ASM5) } @Override @@ -78,7 +78,7 @@ class BowArrowClassAdapter extends ClassVisitor { if (autoClassInfo != null) { if (name == '') { if (autoClassInfo.initDesc != null) { - throw new GradleScriptException("class " + classname + " has multi-.", null); + throw new GradleScriptException("class " + classname + " has multi-.", 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') { @@ -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) { @@ -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 diff --git a/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/TargetClassAdapter.groovy b/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/TargetClassAdapter.groovy index 52cc5f3..52de586 100644 --- a/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/TargetClassAdapter.groovy +++ b/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/TargetClassAdapter.groovy @@ -11,7 +11,7 @@ class TargetClassAdapter extends ClassVisitor { String className TargetClassAdapter() { - super(Opcodes.ASM4) + super(Opcodes.ASM5) } void set(ClassVisitor classWriter, OnMethodInjectListener onMethodInjectListener) { diff --git a/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/TargetClassMethodAdapter.groovy b/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/TargetClassMethodAdapter.groovy index 29d47b0..dfa5625 100644 --- a/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/TargetClassMethodAdapter.groovy +++ b/auto-inject-plugin/src/main/groovy/com/eastwood/tools/plugins/autoinject/adapter/TargetClassMethodAdapter.groovy @@ -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) { @@ -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, '', autoClassInfo.initDesc, false) mv.visitVarInsn(Opcodes.ASTORE, offset + i + 1) @@ -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, '', autoClassInfo.initDesc, false) mv.visitVarInsn(Opcodes.ASTORE, offset + i + 1) @@ -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) } } }