Java反射メカニズムの例
6218 ワード
Java反射の使い方
主な反射APIは、現在のJAVA仮想マシンにおけるクラス、インタフェース、またはオブジェクトの情報を生成するために使用される.
1.Classクラス:反射するコアクラスで、クラスの属性、メソッドなどのコンテンツ情報を取得できます.
2.Fieldクラス:Java.lang.reflect.クラスの属性を表し、クラスの中属性値を取得および設定できます.
3.Methodクラス:Java.lang.reflectは、クラスのメソッドを表し、クラス内のメソッドの情報や実行メソッドを取得するために使用できます.
4.Construcor類:Java.lang.reflectは、クラスの構造方法を表します.
utilクラス:
主な反射APIは、現在のJAVA仮想マシンにおけるクラス、インタフェース、またはオブジェクトの情報を生成するために使用される.
1.Classクラス:反射するコアクラスで、クラスの属性、メソッドなどのコンテンツ情報を取得できます.
2.Fieldクラス:Java.lang.reflect.クラスの属性を表し、クラスの中属性値を取得および設定できます.
3.Methodクラス:Java.lang.reflectは、クラスのメソッドを表し、クラス内のメソッドの情報や実行メソッドを取得するために使用できます.
4.Construcor類:Java.lang.reflectは、クラスの構造方法を表します.
:
:
public class UserEntity {
private Integer userId;
private String userName;
private String userSex;
private Integer userAge;
public UserEntity() {}
public UserEntity(Integer userId, String userName, String userSex, Integer userAge) {
this.userId = userId;
this.userName = userName;
this.userSex = userSex;
this.userAge = userAge;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
public Integer getUserAge() {
return userAge;
}
public void setUserAge(Integer userAge) {
this.userAge = userAge;
}
@Override
public String toString() {
return "UserEntity{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", userSex='" + userSex + '\'' +
", userAge=" + userAge +
'}';
}
}
utilクラス:
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
public class ReflectUtil {
/**
* :
* : TODO
* ${tags}
*/
public static Object getObject(Class clazz, Map map) {
Object obj = null;
try {
// ; , clazz.getConstructor(Integer.class,String.class,String.class,Integer.class)
Constructor constructor = clazz.getConstructor();
// constructor.newInstance(110," ","man",99), new UserEntity(110," ","man",99)
obj = constructor.newInstance();
// ,
Method method = null;
// getFields public , private getDeclaredFields
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
// field true, false( true )
field.setAccessible(true);
// :Integer、String、Boolean
String fieldType = field.getType().getName();
//
String fieldName = field.getName();
if (fieldType.equals("java.lang.Boolean")) {
method = clazz.getDeclaredMethod("is" + getMethodName(fieldName), field.getType());
} else {
method = clazz.getDeclaredMethod("set" + getMethodName(fieldName), field.getType());
}
/*if (fieldType.equals("java.lang.Integer")) {
method = clazz.getDeclaredMethod("set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), Integer.class);
} else if (fieldType.equals("java.lang.String")) {
method = clazz.getDeclaredMethod("set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), String.class);
}*/
for (String key : map.keySet()) {
if (fieldName.equals(key)) {
// , set (xxx.set())
method.invoke(obj, map.get(key));
break;
}
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return obj;
}
public static void getMethod(Class clazz) {
try {
Method method = clazz.getDeclaredMethod("getUserId");
System.out.println(method.getName());
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
/**
* :
* : TODO
* ${tags}
*/
public static void getBeanValues(Object object) {
Class clazz = object.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
// field
field.setAccessible(true);
try {
// field.get()
System.out.println(field.get(object));;
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
/**
* : 、
* : TODO
* ${tags}
*/
private static String getMethodName(String fildeName) {
byte[] items = fildeName.getBytes();
items[0] = (byte) ((char) items[0] - 'a' + 'A');
return new String(items);
}
public static void main(String[] args) {
Map map = new HashMap<>();
map.put("userId", 110);
map.put("userName", " ");
map.put("userSex", "man");
map.put("userAge", 99);
UserEntity userEntity = (UserEntity) ReflectUtil.getObject(UserEntity.class, map);
ReflectUtil.getMethod(UserEntity.class);
ReflectUtil.getBeanValues(userEntity);
}