背景
之前走读了Class类的函数,能让我们更灵活的获取一个类中的信息。然而仅有类信息还是不够的,想要达到灵活调用类中函数的功能还需要借助于reflect包的Method类,本博文将走读Method类的主要方法。
代码+实战结果
Main.java
public class Main {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
// 获取本类所有的方法
Method[] methods = Main.class.getDeclaredMethods();
for (Method method : methods) {
// public static void com.ztjy.Main.main(java.lang.String[]) throws java.lang.NoSuchMethodException
// public void com.ztjy.Main.demoTest(java.lang.Integer,java.lang.String) throws java.lang.Exception
System.out.println(method);
}
// 2
System.out.println(methods.length);
// 获取Method方法对象
Method method = Main.class.getMethod("demoTest", Integer.class, String.class);
// demoTest
System.out.println(method.getName());
// 获取方法注解(如果没有的话则返回null)
// @com.ztjy.DemoAnnotation(value=123)
System.out.println(method.getAnnotation(DemoAnnotation.class));
// void (包含了泛型)
System.out.println(method.getGenericReturnType());
// void
System.out.println(method.getReturnType());
// sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@1376c05c 还不太清楚这个东西
System.out.println(method.getAnnotatedReceiverType());
// sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@51521cc1 还不太清楚这个东西
System.out.println(method.getAnnotatedReturnType());
// void 还不太清楚这个东西
System.out.println(method.getAnnotatedReturnType().getType());
// @com.ztjy.DemoAnnotation(value=123)
for (Annotation annotation : method.getDeclaredAnnotations()) {
System.out.println(annotation);
}
// class com.ztjy.Main
System.out.println(method.getDeclaringClass());
// my-defaultValue,这可以获取注解中函数的默认值
System.out.println(DemoAnnotation.class.getMethod("value").getDefaultValue());
// class java.lang.Exception
for (Class<?> exceptionType : method.getExceptionTypes()) {
System.out.println(exceptionType);
}
// class java.lang.Exception
for (Type genericExceptionType : method.getGenericExceptionTypes()) {
System.out.println(genericExceptionType);
}
// class java.lang.Integer
// class java.lang.String
for (Type genericParameterType : method.getGenericParameterTypes()) {
System.out.println(genericParameterType);
}
// true
System.out.println(method.getModifiers() == Modifier.PUBLIC);
// 获取参数的注解
// @javax.validation.constraints.NotNull(message={javax.validation.constraints.NotNull.message}, groups=[], payload=[])
for (Annotation[] parameterAnnotation : method.getParameterAnnotations()) {
for (Annotation annotation : parameterAnnotation) {
System.out.println(annotation);
}
}
// 2
System.out.println(method.getParameterCount());
// class java.lang.Integer
// class java.lang.String
for (Class<?> parameterType : method.getParameterTypes()) {
System.out.println(parameterType);
}
// java.lang.Integer arg0
// java.lang.String arg1
for (Parameter parameter : method.getParameters()) {
System.out.println(parameter);
}
// 参考 https://www.zhihu.com/question/54895701
System.out.println(method.isBridge());
// 参考 https://blog.csdn.net/u010003835/article/details/76850242,default关键字只能在接口中使用,具体参考博文
System.out.println(method.isDefault());
// 没太明白。。。。参考:https://blog.csdn.net/feier7501/article/details/19088361
System.out.println(method.isSynthetic());
// false,可变参数
System.out.println(method.isVarArgs());
// 权限是否可访问,false
System.out.println(method.isAccessible());
// true
System.out.println(method.isAnnotationPresent(DemoAnnotation.class));
// 1123
method.invoke(Main.class.newInstance(), 1, "123");
}
@DemoAnnotation(value = "123")
public void demoTest(@NotNull Integer integer, String string) throws Exception {
System.out.println(integer + string);
}
}
DemoAnnotation.java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DemoAnnotation {
String value() default "my-defaultValue";
}
运行结果如下: