reset init inject code ,remove autoInject annotation

This commit is contained in:
yummylau
2019-10-22 14:07:55 +08:00
parent 6edabf05b0
commit 5a309b3b15
5 changed files with 39 additions and 69 deletions

View File

@@ -4,7 +4,6 @@ import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.plugin.component.anno.AutoInject;
import com.plugin.library.IProvideFromLibrary;
import com.plugin.library.ISdk;
import com.plugin.library.ISdk2;
@@ -48,7 +47,6 @@ public class MainActivity extends AppCompatActivity {
((TextView) findViewById(R.id.text)).setText(stringBuilder);
}
@AutoInject
public void initComponent(){
ComponentManager.init(getApplication());
}

View File

@@ -1,10 +0,0 @@
package com.plugin.component.anno
import kotlin.reflect.KClass
/**
* 自动注入组件实现
*/
@Target(AnnotationTarget.FUNCTION)
@kotlin.annotation.Retention(AnnotationRetention.BINARY)
annotation class AutoInject()

View File

@@ -1,15 +0,0 @@
package com.plugin.component.anno;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 自动注入sdk实现
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface AutoInject {
}

View File

@@ -1,6 +1,5 @@
package com.plugin.component.transform
import com.plugin.component.anno.AutoInject
import com.plugin.component.transform.info.ComponentSdkInfo
import com.plugin.component.transform.info.ScanRuntime
import org.objectweb.asm.AnnotationVisitor
@@ -33,53 +32,22 @@ class InjectCodeAdapter extends ClassVisitor {
super.visit(version, access, name, signature, superName, interfaces)
}
@Override
MethodVisitor visitMethod(int access, String name, String methodDescriptor, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, methodDescriptor, signature, exceptions)
mv = new AdviceAdapter(Opcodes.ASM7, mv, access, name, methodDescriptor) {
private boolean hasAutoInjectMethod = false
@Override
AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
hasAutoInjectMethod = Type.getDescriptor(AutoInject.class) == descriptor
return super.visitAnnotation(descriptor, visible)
}
@Override
protected void onMethodExit(int opcode) {
if (hasAutoInjectMethod) {
for (ComponentSdkInfo item : ScanRuntime.getComponentSdkInfoList()) {
if (item.isValid()) {
mv.visitLdcInsn(Type.getType(item.componentClassName))
mv.visitLdcInsn(Type.getType(item.sdk))
mv.visitLdcInsn(Type.getType(item.impl))
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/plugin/component/SdkManager", "register", "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Object;)V", false)
}
}
}
}
}
return mv
}
// @Override
// MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
// MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions)
// mv = new AdviceAdapter(Opcodes.ASM7, mv, access, name, descriptor) {
// MethodVisitor visitMethod(int access, String name, String methodDescriptor, String signature, String[] exceptions) {
// MethodVisitor mv = super.visitMethod(access, name, methodDescriptor, signature, exceptions)
// mv = new AdviceAdapter(Opcodes.ASM7, mv, access, name, methodDescriptor) {
//
// private boolean injectComponentAutoInitCode = false
// private boolean hasAutoInjectMethod = false
//
// @Override
// protected void onMethodEnter() {
// injectComponentAutoInitCode =
// className == sComponentManagerPath && name == "init" && descriptor == "(Landroid/app/Application;)V"
// AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
// hasAutoInjectMethod = Type.getDescriptor(AutoInject.class) == descriptor
// return super.visitAnnotation(descriptor, visible)
// }
//
// @Override
// protected void onMethodExit(int opcode) {
// if (injectComponentAutoInitCode) {
// if (hasAutoInjectMethod) {
// for (ComponentSdkInfo item : ScanRuntime.getComponentSdkInfoList()) {
// if (item.isValid()) {
// mv.visitLdcInsn(Type.getType(item.componentClassName))
@@ -93,4 +61,35 @@ class InjectCodeAdapter extends ClassVisitor {
// }
// return mv
// }
@Override
MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions)
mv = new AdviceAdapter(Opcodes.ASM7, mv, access, name, descriptor) {
private boolean injectComponentAutoInitCode = false
@Override
protected void onMethodEnter() {
injectComponentAutoInitCode =
className == sComponentManagerPath && name == "init" && descriptor == "(Landroid/app/Application;)V"
}
@Override
protected void onMethodExit(int opcode) {
if (injectComponentAutoInitCode) {
for (ComponentSdkInfo item : ScanRuntime.getComponentSdkInfoList()) {
if (item.isValid()) {
mv.visitLdcInsn(Type.getType(item.componentClassName))
mv.visitLdcInsn(Type.getType(item.sdk))
mv.visitLdcInsn(Type.getType(item.impl))
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/plugin/component/SdkManager", "register", "(Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/Object;)V", false)
}
}
}
}
}
return mv
}
}

View File

@@ -9,7 +9,6 @@ import androidx.appcompat.app.AppCompatActivity;
import com.component.debug.R;
import com.plugin.component.ComponentManager;
import com.plugin.component.anno.AutoInject;
public class DebugActivity extends AppCompatActivity {
@@ -21,7 +20,6 @@ public class DebugActivity extends AppCompatActivity {
initComponent(getApplication());
}
@AutoInject
public void initComponent(Application application){
ComponentManager.init(application);
}