美文网首页
反射 注解 应用

反射 注解 应用

作者: 勇者与王者 | 来源:发表于2020-10-20 22:30 被阅读0次

    今天才知道Java的反射这么强大,可以完全不知道方法名、参数、返回值就能进行方法调用,而且还能拿到方法的返回值,遍历返回值中的集合等。

    自定义注解:

    package Reflect;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * @ClassName TestMethod
     * @description:
     * @author: isquz
     * @time: 2020/10/20 22:08
     */
    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface TestMethod {
        String params();
    }
    
    

    方法使用注解:

    package Reflect;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @ClassName MethodAnnotation
     * @description:
     * @author: isquz
     * @time: 2020/10/20 22:11
     */
    public class MethodAnnotation {
    
        public List<String> returnParamList(){
            List<String> list = new ArrayList<>();
            list.add("String1");
            list.add("String2");
            list.add("String3");
            return list;
        }
    
        @TestMethod(params = "returnParamList")
        public void testMethodWithAnnotation(String s){
            System.out.println("获取注解传递的参数集合" + s);
        }
    }
    
    

    反射调用注解:

    package Reflect;
    
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.Iterator;
    
    /**
     * @ClassName TestMethodAnnotation
     * @description:
     * @author: isquz
     * @time: 2020/10/20 22:07
     */
    public class TestMethodAnnotation {
        public static void main(String[] args) {
            Class<?> cls = MethodAnnotation.class;
            Method[] methods = cls.getDeclaredMethods();
            try {
                for(Method method:methods){
                    TestMethod methodInfo = method.getAnnotation(TestMethod.class);
                    if(methodInfo != null){
                        System.out.println("添加注解的方法名:" + method.getName());
                        String param = methodInfo.params();
                        Class<?>[] methodParamType = method.getParameterTypes();
                        String variable = methodParamType[0].getSimpleName();
                        System.out.println("测试方法所需参数类型:" + variable);
                        System.out.println("注解中参数来源方法:" + param);
    
                        Method paramMethod = cls.getDeclaredMethod(param);
                        Method testMethod = cls.getDeclaredMethod(method.getName(), variable.getClass());
    
                        Iterable<?> paramList = (Iterable<?>) paramMethod.invoke(cls.newInstance());
                        Iterator<?> iterator = paramList.iterator();
                        while (iterator.hasNext()){
                            testMethod.invoke(cls.newInstance(), iterator.next());
                        }
                    }
                }
            }catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
    
        }
    }
    
    

    相关文章

      网友评论

          本文标题:反射 注解 应用

          本文链接:https://www.haomeiwen.com/subject/zqejmktx.html