Java 学习入门到掌握-四大元注解的高级使用_反射[2]

反射是框架设计的灵魂

一、反射的概述
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
二、反射加注解的基本使用


import java.lang.annotation.Annotation;
import java.lang.reflect.*;
public class MethodDemo {

    public static void main(String[] args) {
        //获得声明的方法,主要就是你定义过的方法。在这里指的是random、sin。
        Method[] declaredMethods = SampleClass.class.getDeclaredMethods();

        for (Method method : declaredMethods) {
            //判断该方法是否包含这个注解
            if (method.getAnnotation(FunctionAnnotations.NewFunctionAPI.class) == null) {
                System.out.println("NewFunctionAPI标注的方法:" + method.getName());
            }
            //判断该方法是否包含这个注解
            if (method.getAnnotation(FunctionAnnotations.Deterministic.class) == null) {
                System.out.println("Deterministic标注的方法:" + method.getName());
            }
            //判断该类是否包含这个注解
            if (method.getDeclaredAnnotation(FunctionAnnotations.MathFunction.class) == null) {
                System.out.println(method.getName() + "方法所在的类具有MathFunction标签");
            }
            //判断该是否为Public方法
            if (Modifier.isPublic(method.getModifiers())) {
                System.out.println(method.getName() + "方法为Public");
            }
            //判断该是否为Static方法
            if (Modifier.isStatic(method.getModifiers())) {
                System.out.println(method.getName() + "方法为Static");
            }
            //判断该方法是否为可变参数
            boolean varArgs = method.isVarArgs();

            System.out.println(method.getName() + "方法是否为可变参数" + varArgs);

            //返回方法的参数类型,注下面的类型推断的 方式主要用于传参类型转换。
            Class<?>[] javaTypes = method.getParameterTypes();

            for (int i = 0; i < javaTypes.length; i++) {
                Class<?> javaType = javaTypes[i];
                //如果为可变参数并且
                if (varArgs && i == javaTypes.length - 1) {
                    System.out.println("可变参数类型输出方式" + javaType.getComponentType());
                } else {
                    System.out.println("固定参数类型输出方式" + javaType);
                }
            }
            Class<?> returnType = method.getReturnType();
            boolean equals = returnType.equals(void.class);
            System.out.println(method.getName() + "方法是否有返回值" + equals);
            System.out.println(method.getName() + "方法返回值类型为" + returnType);

            Parameter[] parameters = method.getParameters();
            for (Parameter parameter:parameters){
                //===================================== Parameter信息获取 =====================================
                System.out.println("===================================== Parameter信息获取 =====================================");
                System.out.println("通过parameter.getModifiers()获取参数修饰符:" + Modifier.toString(parameter.getModifiers()));
                System.out.println("通过parameter.getName()获取参数名:" + parameter.getName());
                System.out.println("通过parameter.getParameterizedType()获取参数化类型(泛型):" + parameter.getParameterizedType());
                System.out.println("通过parameter.toString()获取参数的字符串描述:" + parameter.toString());
                System.out.println("通过parameter.isSynthetic()判断参数是否是合成的:" + parameter.isSynthetic());
                System.out.println("通过parameter.isImplicit()判断参数是否是隐式的:" + parameter.isImplicit());
                System.out.println("通过parameter.isNamePresent()判断参数是否以类文件名命名:" + parameter.isNamePresent());
                System.out.println("通过parameter.isVarArgs()判断参数是否是可变的:" + parameter.isVarArgs() + "\n");

                //===================================== Parameter注解信息 =====================================
                System.out.println("===================================== Parameter注解信息 =====================================");
                //通过parameter.getAnnotatedType()获取注解的类型(组合类型)
                AnnotatedType annotatedType = parameter.getAnnotatedType();
                System.out.println("通过parameter.getAnnotatedType()获取注解的类型(组合类型)--参数类型:" + annotatedType.getType() + "\n");

                //通过parameter.getAnnotation()和parameter.getDeclaredAnnotation()获取参数的一个注解
                System.out.println("通过parameter.getAnnotation()获取参数的一个注解:" + parameter.getAnnotation(FunctionAnnotations.Coerce.class));
                System.out.println("通过parameter.getDeclaredAnnotation()获取参数的一个注解:" + parameter.getDeclaredAnnotation(FunctionAnnotations.Coerce.class) + "\n");

                //通过parameter.getAnnotationsByType(annotation.class)获取一类注解
                Annotation[] typeAnnotations = parameter.getAnnotationsByType(FunctionAnnotations.Coerce.class);
                for (Annotation annotation : typeAnnotations) {
                    System.out.println("通过parameter.getAnnotationsByType(annotation.class)获取一类注解:" + annotation);
                }
                //通过parameter.getDeclaredAnnotationsByType(annotation.class)获取一类注解
                Annotation[] typeAnnotations1 = parameter.getDeclaredAnnotationsByType(FunctionAnnotations.Coerce.class);
                for (Annotation annotation : typeAnnotations1) {
                    System.out.println("通过parameter.getDeclaredAnnotationsByType(annotation.class)获取一类注解:" + annotation);
                }
                System.out.println("");

                //通过parameter.getAnnotations()获取全部注解
                Annotation[] annotations = parameter.getAnnotations();
                for (Annotation annotation : annotations) {
                    System.out.println("通过parameter.getAnnotations()获取全部注解:" + annotation);
                }
                //通过parameter.getDeclaredAnnotations()获取全部注解
                Annotation[] annotations1 = parameter.getDeclaredAnnotations();
                for (Annotation annotation : annotations1) {
                    System.out.println("通过parameter.getDeclaredAnnotations()获取全部注解:" + annotation);
                }
            }


            
        }

    }
}

@FunctionAnnotations.MathFunction
class SampleClass {
    @FunctionAnnotations.NewFunctionAPI
    public static double random() {
        return Math.random();
    }

    @FunctionAnnotations.Deterministic
    public static double sin(@FunctionAnnotations.Coerce double a) {
        return Math.sin(a);
    }

    @FunctionAnnotations.Deterministic
    public static double sum(double... doubles) {
        double sum = 0;
        for (double dd : doubles) {
            sum += dd;
        }
        return sum;
    }
}

本节主要介绍了反射加注解的基本操作方法,如果作者有时间将写一个利用四大注解的自动注入机制的小demo。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值