lang.reflect->java.lang.reflect.Method

java中反射使用几率最多的就是方法的反射。
功能支持的类库:java.lang.reflect.Method

提供的功能包括:
1、获取方法上的注解信息。(getAnnotations、getDeclaredAnnotations)
2、获取方式上的某个注解信息Annotation。(getAnnotation(Type.class))
3、获取方法上的类型参数Type。(getTypeParameters)
4、获取方式声明的异常类型Class[]。(getExceptionTypes)
5、获取方式上的参数注解。(getParameterAnnotations)
6、获取方法返回的数据类型Class。(getReturnType)
7、获取方法方法的参数类型Class[](getParameterTypes)
8、执行某个方法(invoke(Object obj, Object... args))

以上功能是使用最频率最多的方法,请结合案例仔细体会之!

测试案例:


package array;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER,ElementType.TYPE,ElementType.CONSTRUCTOR })
@interface Note {
public boolean require() default true;
public String note() default "";
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER,ElementType.TYPE,ElementType.CONSTRUCTOR })
@interface Type {
public boolean require() default true;
public String type() default "";
}

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER,ElementType.TYPE,ElementType.CONSTRUCTOR })
@interface Face {
public boolean require() default true;
public String face() default "";
}

interface IB{
@Face(face="method->face")
public boolean loginUser(String username,String password) throws RuntimeException,SQLException;
}

@Note(note= "calss->B",require = false)
public class B implements IB{
@Note(note="field->note")
@Type(type="field->type")
public List<String> note = Arrays.asList("defaultValue");

@Note(note="CONSTRUCTOR->B")
public <T> B(@Note(note = "p->note") @Type(type = "p->type") String note,String note2){
}

@Note(note = "method ->login()",require = true)
public void login(@Note(note = "p->username") String username, @Note(note = "p->password")String password) {
}

@Note(note = "method ->login()",require = true)
@Type(type = "p->type->username",require = false)
public boolean loginUser(@Note(note = "p->note->username") @Type(type = "p->type->username") String username, @Type(type = "p->password")String password)
throws RuntimeException,SQLException{
return true;
}

public <N,P> N getUserInfo(){
return null;
}
public List<String> getNote() {
return note;
}

public void setNote(List<String> note) {
this.note = note;
}

}


package array;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;
/**
* 测试方法反射方法
* @author create by [url=xinchunwang@aliyun.com]新春.王[/url] <br>
*
*/
public class MethodReflect {
public static void main(String[] args) throws Exception {
// 获取B.class 的loginUser方法 注意参数:("loginUser", String.class,String.class)
Method loginMethod = B.class.getMethod("loginUser", String.class, String.class);

//NOTE:方法上声明的注解是无法继承的
//获取方法上的所有注解
Annotation[] methodAnno = loginMethod.getAnnotations();
for(Annotation item : methodAnno) {
if(item instanceof Note){
Note note = (Note)item;
System.out.println(note);
}else if(item instanceof Type){
Type type = (Type)item;
System.out.println(type);
}else{
System.out.println(item);
}
}
//获取直接在方法上的注解
Annotation[] declareMethodAnno =loginMethod.getDeclaredAnnotations();
for(Annotation item : declareMethodAnno) {
if(item instanceof Note) {
Note note = (Note)item;
System.out.println(note);
}else if(item instanceof Type){
Type type = (Type)item;
System.out.println(type);
}else {
System.out.println(item);
}
}
System.out.println("----------------------------------");

//获取方法上某个类型的注解
Type type = (Type)loginMethod.getAnnotation(Type.class);
System.out.println(type);
//如果方法上没有此注解,直接返回null
Face face = (Face)loginMethod.getAnnotation(Face.class);
System.out.println(face);
System.out.println(loginMethod.getDefaultValue());

System.out.println("-----------------------------------------");
//获取方法返回的类型参数列表
Method getUserInfo = B.class.getMethod("getUserInfo");
TypeVariable[] typeVar = getUserInfo.getTypeParameters();
for(TypeVariable item :typeVar){
System.out.println(item.getName());
java.lang.reflect.Type[] bounds = item.getBounds();//参数类型的 上下边界
System.out.println(bounds);
}
System.out.println(Arrays.toString(typeVar));

System.out.println("-----------------------------------------");
//获取方法返回的异常列表
Class[] exceptions = loginMethod.getExceptionTypes();
System.out.println(Arrays.toString(exceptions));

System.out.println("-----------------------------------------");
//获取参数的类型
Class[] parameterTypes = loginMethod.getParameterTypes();
System.out.println(Arrays.toString(parameterTypes));

System.out.println("-----------------------------------------");
//获取参数上的注解
Annotation[][] parameterAnno = loginMethod.getParameterAnnotations();
for(Annotation[] item : parameterAnno){
System.out.println(Arrays.toString(item));
}

System.out.println("-----------------------------------------");
//获取返回的类型
Class returnType = loginMethod.getReturnType();
System.out.println(returnType);

B b = new B("","");
//执行某个方法
boolean result =(Boolean)loginMethod.invoke(b, "","");
System.out.println(result);
}
}



测试输出:
@array.Note(require=true, note=method ->login())
@array.Type(require=false, type=p->type->username)
@array.Note(require=true, note=method ->login())
@array.Type(require=false, type=p->type->username)
----------------------------------
@array.Type(require=false, type=p->type->username)
null
null
-----------------------------------------
N
[Ljava.lang.reflect.Type;@5a8a0d5d
P
[Ljava.lang.reflect.Type;@1d73831b
[N, P]
-----------------------------------------
[class java.lang.RuntimeException, class java.sql.SQLException]
-----------------------------------------
[class java.lang.String, class java.lang.String]
-----------------------------------------
[@array.Note(require=true, note=p->note->username), @array.Type(require=true, type=p->type->username)]
[@array.Type(require=true, type=p->password)]
-----------------------------------------
boolean
true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值