lang.reflect->java.lang.reflect.Field

java 反射功能强大,也是很多基础框架的核心技术。结合jdk1.5 的注解功能java反射技术再很多项目中成了必备的技术。

本例子展现了java.lang.reflect.Field类的使用方法以及方法的含义。

从功能上划分Field反射功能包含:

1、与注解相关方法。声明在 AnnotatedElement 接口中:

public interface AnnotatedElement {
/**
判断当前Element 是否有annotationClass注解信息
*/
boolean isAnnotationPresent(Class<? extends Annotation> annotationClass);

/**
返回当前element 对象的annotationClass 注解信息 没有annotationClass 注解信息会抛出异常
*/
<T extends Annotation> T getAnnotation(Class<T> annotationClass);

/**
返回当前Element 对象的所有注解信息
*/
Annotation[] getAnnotations();

/**
返回直接声明在当前Element对象的所有注解信息(一般在Method中使用)
*/
Annotation[] getDeclaredAnnotations();
}


2、Field 值的设置和获取信息。

public Object get(Object obj){...}
public boolean getBoolean(Object obj){...}
public void set(Object obj, Object value){...}

详细请参考jdk代码。(略)

3、Field 元信息。(名称、类型、泛型参数等)

public String getName() //返回field声明的名字
public Class<?> getType() //返回field声明的类型
public Type getGenericType()// 包含泛型的field声明的类型
public int getModifiers() //返回public、final 、static 等修饰符



综合例子:

@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 note() default "";
}

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

@Note(note="CONSTRUCTOR->B")
public <T> B(@Note(note = "p->note") @Type(note = "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)
public boolean loginUser(@Note(note = "p->username") String username, @Note(note = "p->password")String password) {
return true;
}

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.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 字段反射
* @author xinchun.wang
*
*/
public class FieldReflect {
public static void main(String[] args) throws Exception {
//生成一个真实的B对象
B b = new B("testNote", "note2");
//获取B.class 的loginUser方法 注意参数:("loginUser", String.class,String.class)
Method loginMethod = B.class.getMethod("loginUser", String.class,String.class);
//获取声明loginMethod 所在Class对象 即:B.class
Class<?> type = (Class<?>) loginMethod.getDeclaringClass();
System.out.println("----------------begin getDeclaringClass------------------------");
System.out.println(type);
System.out.println(B.class);
System.out.println(type.equals(B.class)); //true
System.out.println(type == B.class); //true
System.out.println("----------------end getDeclaringClass------------------------");



System.out.println("----------------begin getField------------------------");
//获取一个成员变量 (field 必须为public)
Field field = B.class.getField("note");
System.out.println(field.getName()); //获取声明的note变量
System.out.println(field.isAccessible()); // false :默认 不优化处理。返回是否可以优化处理 (注意含义请阅读api)
field.setAccessible(true);
System.out.println("----------------end getField------------------------");

//获取b对象的常量
@SuppressWarnings("unchecked")
List<String> objField = (List<String>)field.get(b);
System.out.println(objField);
List<String> list = new ArrayList<String>();
list.add("goodBoy");
field.set(b, list);
objField = (List<String>)field.get(b);
System.out.println(objField);

//获取字段的所有注解信息
Annotation[] fieldAnno = field.getAnnotations();
for (Annotation anno : fieldAnno) {
if (anno instanceof Note) {
System.out.println(((Note) anno).note());
}
}

//获取某一个类型的注解
Note note = field.getAnnotation(Note.class);
System.out.println(note.note());

//获取包含泛型信息的字段类型信息
java.lang.reflect.Type refType = field.getGenericType();
System.out.println(refType);
//获取字段的参数类型
System.out.println("***************");
if(refType instanceof ParameterizedType){
ParameterizedType pType = (ParameterizedType)refType;
java.lang.reflect.Type[] types = pType.getActualTypeArguments();
System.out.println(Arrays.toString(types));
}
System.out.println("***************");
//获取字段的类型信息
System.out.println(field.getType());


//获取直接在字段上声明的注解信息
Annotation[] anno = field.getDeclaredAnnotations();
for(Annotation item :anno){
System.out.println( item);
}

}
}



运行结果:


----------------begin getDeclaringClass------------------------
class array.B
class array.B
true
true
----------------end getDeclaringClass------------------------
----------------begin getField------------------------
note
false
----------------end getField------------------------
[defaultValue]
[goodBoy]
field->note
field->note
java.util.List<java.lang.String>
[color=red]
***************
[class java.lang.String]
***************
[/color]
interface java.util.List
@array.Note(require=true, note=field->note)
@array.Type(require=true, type=field->type)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值