您好,登錄后才能下訂單哦!
大家知道ASM可以來分析 修改類 從前學習spring的時候里面有個叫IOC的技術,不知道他的底層實現感覺很神秘,
但是最近在看ASM的東西,感覺結合反射用它就可以實現自動注入的功能。例子如下
那spring里面是如何實現的呢?
// 注解類 package hgs.asm; public @interface AutoWare { } //AnoDesc 里面的一個屬性 package hgs.asm; public class A { } package hgs.asm; //用于操作的類 public class AnoDesc { String name = "hgs"; int age = 100; @AutoWare A a; public void saySomething(String desc) { System.out.println("say:" + desc); } } //測試 package hgs.asm; import java.io.IOException; import java.lang.reflect.Field; import java.util.List; import org.objectweb.asm.ClassReader; import org.objectweb.asm.Type; import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.FieldNode; public class Test { public static void test1() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException { String clazz = "hgs.asm.AnoDesc"; Class<?> forName = Class.forName(clazz); AnoDesc newInstance = (AnoDesc)forName.newInstance(); //通過asm讀取類 通過其屬性api 來訪問類的屬性 方法 注解 ClassNode node = new ClassNode(); //node.superName ="org.objectweb.asm.ClassVisitor"; ClassReader reader = new ClassReader("hgs.asm.AnoDesc"); //ClassWriter writer = new ClassWriter(0); reader.accept(node,0); //node.accept(writer); //得到所有的屬性 List<FieldNode> fields = node.fields; for(FieldNode fnd: fields) { String name = fnd.name; String desc = fnd.desc; Object value = fnd.value; Type tp = Type.getObjectType(desc); System.out.println("name:"+name); System.out.println("desc:"+desc); System.out.println("value:"+value); System.out.println("type:"+tp.getInternalName()); System.out.println(); //判斷屬性是否存在 AutoWare注解 List<AnnotationNode> invisibleAnnotations = fnd.invisibleAnnotations; if(invisibleAnnotations!=null ) { for(AnnotationNode and : invisibleAnnotations) { System.out.println(" anotation:"+and.desc); System.out.println("equals:"+"Lhgs/asm/AutoWare;".equals(and.desc)); //存在的話 就把該屬性初始化 if("Lhgs/asm/AutoWare;".equals(and.desc)) { String qiliName = tp.getInternalName().replaceFirst("L", "").replaceAll("/", "\\.").replace(";", ""); System.out.println("qulity name :"+ qiliName); Class<?> fi = Class.forName(qiliName); Field declaredField = forName.getDeclaredField(name); declaredField.set(newInstance,fi.newInstance() ); } } } } System.out.println(newInstance.a); } public static void main(String[] args) throws Exception{ test1(); } } 結果
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。