相关阅读
【小家java】java5新特性(简述十大新特性) 重要一跃
【小家java】java6新特性(简述十大新特性) 鸡肋升级
【小家java】java7新特性(简述八大新特性) 不温不火
【小家java】java8新特性(简述十大新特性) 饱受赞誉
【小家java】java9新特性(简述十大新特性) 褒贬不一
【小家java】java10新特性(简述十大新特性) 小步迭代
【小家java】java11新特性(简述八大新特性) 首个重磅LTS版本
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
那么本文就针对性说明一下,在我们使用反射时候经常用到也是经常会让我们迷糊的一些方法的区别。
getMethods()和getDeclaredMethods的区别
我们先来看看Class所有的类似的方法,然后咱们分配解释:
public static void main(String[] args) {
Class<Child> clazz = Child.class;
//get和getDeclared系列方法
clazz.getClasses();
clazz.getDeclaredClasses();
clazz.getDeclaringClass();
clazz.getFields();
clazz.getDeclaredFields();
clazz.getMethods();
clazz.getDeclaredMethods();
clazz.getConstructors();
clazz.getDeclaredConstructors();
//Enclosing系列方法
clazz.getEnclosingClass();
clazz.getEnclosingMethod();
clazz.getEnclosingConstructor();
//枚举类相关的方法 特殊讲解
clazz.getAnnotations();
clazz.getDeclaredAnnotations();
clazz.getEnumConstants();
}
针对于get和getDeclared系列,因为都是类似的,因此此处只讲解一例:
public static void main(String[] args) {
Class<Child> clazz = Child.class;
Method[] methods = clazz.getMethods();
Method[] declaredMethods = clazz.getDeclaredMethods();
System.out.println(methods.length);
System.out.println(declaredMethods.length);
System.out.println("-----------------------------");
}
输出:
11
3
附上两张图,就是为什么会输出11和3的结果截图:
通过结果,我们很容易得出下列结论:
- get系列:本类的public + 父类或接口的public(含静态方法)
- getDeclared系列:本类所有的访问权限的元素(含静态方法)
返回的数组为无序的,如果没有,返回长度为0的数组
因此,如果你想获取到比如该类所有的方法(包含父类并且包含本来的所有访问权限的方法,需要结合使用此两个方法)
但需要注意下面几点:
public static void main(String[] args) {
Class<Integer> clazz = int.class;
//Class<Void> clazz = void.class;
//Class<Void> clazz = Void.class;
Method[] methods = clazz.getMethods();
Method[] declaredMethods = clazz.getDeclaredMethods();
System.out.println(methods.length);
System.out.println(declaredMethods.length);
System.out.println("-----------------------------");
}
输出:
0
0
结论:简单类型、void、Void返回的都是长度为0的数组
以后在使用反射调用invoke方法时,在传递实际参数的时候,无论是基本数据类型,还是引用类型,或者是可变参数类型,把实际参数都包装在一维数组中。
关于getClasses().、getDeclaredClasses、getDeclaringClass的区别
public static void main(String[] args) {
Class<Child> clazz = Child.class;
Class<?>[] classes = clazz.getClasses();
Class<?>[] declaredClasses = clazz.getDeclaredClasses();
Class<?> declaringClass = clazz.getDeclaringClass();
System.out.println(classes.length);
System.out.println(declaredClasses.length);
System.out.println(declaringClass);
System.out.println("-----------------------------");
}
public static class Child {
public class InnerPrive {
}
}
getClasses() : 返回本类所有访问权限的内部类
getDeclaredClasses:返回public的内部类和父类的内部类
getDeclaringClass:返回表示声明由此Method对象表示的方法的类的Class对象。
这几个方法其实用得相对较少。但在精妙的高内聚的框架中,还是有可能被用到的
下面讲述一下通过Classs反射拿到注解的案例
随着springboot的兴起,基于注解驱动的编程模型越来越流行,所以下面这几个反射注解的方法,还是有必要去了解的
- T getAnnotation(Class annotationClass): 返回该元素上指定类型的注解,如果该类型注解不存在,则返回null。
- Annotation[] getAnnotations():返回该元素上存在的所有注解,包含父类、接口上的注解元素
- Annotation[] getDeclaredAnnotations():返回直接存在于此元素上的所有注释。该方法将忽略继承的注释
- boolean isAnnotationPresent(Class<?extends Annotation> annotationClass):判断该元素上是否包含指定类型的注解,存在则返回true,否则返回false.
public static void main(String[] args) {
Class<Child> clazz = Child.class;
Annotation[] annotations = clazz.getAnnotations();
Annotation[] declaredAnnotations = clazz.getDeclaredAnnotations();
System.out.println(annotations.length);
System.out.println(declaredAnnotations.length);
System.out.println("----------------------------------");
}
@Anno1
public static class Child extends Parent {
}
@Anno2
public static class Parent {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Anno1 {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Anno2 {
}
输出:
1
1
看结果,很多人会问:为啥有继承,输出却都是1呢?其实再看下面一段代码,这个疑问不攻自破
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Anno2 {
}
输出:
2
1
由此看出,只有这个注解被标识了能被继承,才会生效。
使用注解@Inherited可以让指定的注解在某个类上使用后,这个类的子类将自动被该注解标记。
我们知道,关键在于继承的问题上,getDeclaredAnnotations和getAnnotations是否相同,就在于父类的注解是否可继承,这可以用下面代码来判断某个注解是否能被继承
public static void main(String[] args) {
boolean inherited = AnnotationType.getInstance(Anno2.class).isInherited();
System.out.println(inherited); //返回true
}
上面就是关于我们反射注解的时候,经常用到一些方法和判断
关注A哥
Author | A哥(YourBatman) |
---|---|
个人站点 | www.yourbatman.cn |
yourbatman@qq.com | |
微 信 | fsx641385712 |
活跃平台 | |
公众号 | BAT的乌托邦(ID:BAT-utopia) |
知识星球 | BAT的乌托邦 |
每日文章推荐 | 每日文章推荐 |