java注解原理

        在基于spring开发的时候,一个简单的注解,就可以完成对象创建与注入,感觉非常的神秘,那么注解是怎么实现的呢?今天这里实现了一套类似junit相关注解XTest,XBefore,XAfter注解。

        注解基本实现思路:

1)扫描工程下面所有类文件并获取注解信息以及对应的类信息,这一步非常关键。spring其实也是有这种东西

2)通过反射,创建对象且执行用注解标注的方法

一、注解定义

        首先创建三个注解,注解内容是空,标注是运行时,且用于方法即可,具体如下:

package com.annotation.framework.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface XTest {
}
package com.annotation.framework.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface XBefore {
}
package com.annotation.framework.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface XAfter {
}

二、包扫描

        这一步是非常关键的,因为通过这一步才能收集到类的所有信息,即哪些类中的哪些方法被XTest,XBefore,XAfter标注。

2.1、入口方法

    //入口
    public static AnnotationFramework run(Class clazz, String[] args) throws Exception {
        AnnotationFramework app = new AnnotationFramework();
        //包扫描并且创建对象
        Set<File> files = app.scanRootPackage(clazz);
        app.createBean(files);
        //执行对应的方法
        app.runCases();
        return app;
    }

2.2、scanRootPackage

        实现思路

1)通过入口方法,传进来的Class对象,获取包路径,然后扫描该包路径下所有类信息(实现比较简单,只扫描该包),代码实现过程中使用ClassLoader提供getSystemResource方法获取资源对象(spring中用的就是这个方法,哈哈,抄袭)。

2)将.class文件存储到set集合中,为后面创建对象做准备。

    private Set<File> scanRootPackage(Class clazz) throws Exception {
        Set<File> files = new HashSet<>();
        Package pack = clazz.getPackage();//获取包对象
        String resourcePath = pack.getName().replace(".", "/");

        //根据包路径,获取资源对象,通过资源对象获取File对象,可能是文件也可能是目录
        Enumeration<URL> rootResources = ClassLoader.getSystemResources(resourcePath);//
        while(rootResources.hasMoreElements()) {
            URL node = rootResources.nextElement();
            File file = new File(node.getFile());
            scanPackage(file, files);
        }
        return files;
    }

    //注意递归调用
    private void scanPackage(File file, Set<File> files) throws Exception {
        for (File f : file.listFiles()) {
            if (f.isDirectory()) {
                scanPackage(f, files);
            } else if (f.isFile()){
                String fileName = f.getName();
                String suffix = fileName.substring(fileName.lastIndexOf(".")+1);
                if (suffix.equals("class")) {
                    files.add(f);
                }
            }
        }
    }

2.3、解析.class文件

        上一步,收集到所有的class文件,那么就要逐一对class文件解析,获取注解信息了。这一步中sping使用的asm字节码技术,这里为了方便采用的是classloader方式。

    private void createBean(Set<File> files) throws Exception {
        for (File file : files) {
            createBeanByClassloader(file);
            //这里可以使用 asm字节码技术 解析class文件
            //spring使用的就是asm字节码技术,asm字节码技术比通过classloader方式要快很多,但是由于asm字节码技术比较晦涩
            //有一定的学习门槛
        }
    }
    private void createBeanByClassloader(File file) throws Exception {
        ///Users/abc/IdeaProjects/untitled/target/classes/com/annotation/framework/annotation/XTest.class
        String classFile = file.getPath().substring(file.getPath().indexOf("classes") + 8);
        classFile = classFile.substring(0, classFile.lastIndexOf('.')).replace('/', '.');
        //System.out.println(classFile);
        Class clazz = ClassLoader.getSystemClassLoader().loadClass(classFile);
        if (clazz.getSimpleName().isEmpty()) {
            return;
        }
        genericBean(clazz); //获取到Class对象,反射创建对象
    }

三、通过反射创建对象

        上面获取到class对象后,我们就可以获取到方法(类)上的Annotation信息了,具体如下

    private void genericBean(Class clazz) {
        String name = Bean.genericBeanName(clazz.getSimpleName());
        for (Method method : clazz.getDeclaredMethods()) {
            for (Annotation annotation : method.getAnnotations()) {
                if (annotation.annotationType().getName().equals(XTest.class.getName())) {
                    StoreSingleBean(name, clazz, method, 0);
                } else if (annotation.annotationType().getName().equals(XBefore.class.getName())) {
                    StoreSingleBean(name, clazz, method, 1);
                } else if (annotation.annotationType().getName().equals(XAfter.class.getName())) {
                    StoreSingleBean(name, clazz, method, 2);
                }
            }
        }
    }

        外层遍历所有方法 ,内层遍历方法标注的所有注解,然后判断注解是否为XTest,XAfter,XBefore,当属于这三个注解时,就创建Bean对象。

    private void StoreSingleBean(String beanName, Class clazz, Method method, int type) {
        Bean bean = this.map.get(beanName);//Map<String, Bean> map = new ConcurrentHashMap<>();
        if (bean == null) {
            bean = new Bean(beanName, clazz);
            this.map.put(beanName, bean);
        }
        if (type == 0) {// XTest
            bean.PutTest(method);
        } else if (type == 1) {// XBefore
            bean.PutBefore(method);
        } else if (type == 2) {
            bean.PutAfter(method);
        }
    }

 创建Bean对象比较简单,具体如下:

public class Bean {
    private String beanName;
    private Class clazz; //class对象
    private Object instance; //实例对象
    private List<Method> before; //用XBefore标注的方法列表
    private List<Method> after; //用XAfter标注的方法列表
    private List<Method> test; //用XTest标准的方法列表

    public Bean(String beanName, Class clazz) {
        this.beanName = beanName;
        this.clazz = clazz;
        try {
            this.instance = clazz.newInstance(); //反射创建实例对象
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        this.before = new LinkedList<>();
        this.after = new LinkedList<>();
        this.test = new LinkedList<>();
    }

    public void PutBefore(Method m) {
        this.before.add(m);
    }
    public void PutAfter(Method m) {
        this.after.add(m);
    }
    public void PutTest(Method m) {
        this.test.add(m);
    }

    public static String genericBeanName(String original) {
        char[] data = original.toCharArray();
        data[0] = Character.toLowerCase(data[0]);
        return new String(data);
    }

    public void runBefore() {
        for (Method m : this.before) {
            try {
                m.invoke(instance);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }

    public void runTest() {
        for (Method m : this.test) {
            try {
                m.invoke(instance);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }

    public void runAfter() {
        for (Method m : this.after) {
            try {
                m.invoke(instance);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
}

四、应用

        至此,注解功能实现基本完成,下面是应用,那自然就很简单了,创建一个main方法类,然后在创建一个Test类。

public class AnnotationTest {

    public static void main(String[] args) throws Exception {
        AnnotationFramework.run(AnnotationTest.class, args);
    }
}
public class GenericTest {
    @XBefore
    public void test0() {
        System.out.println("test0 XBefore");
    }

    @XAfter
    public void test2() {
        System.out.println("test2 XAfter");
    }

    @XTest
    public void test1() {
        System.out.println("test1 XTest");
    }
}

执行结果如下:

五、总结

        以上是对于注解理解,期间参考了spring的源码实现,基本思路就是按照上面描述一样。只不过spring实现的更复杂一些,支撑功能更强大一些。希望能帮到对于注解感兴趣的小伙伴。之前写过一篇关于lombok gettet,setter的实现《lombok注解实现(annotation)》实现机制不太一样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值