您的位置:首页 > 编程语言 > Java开发

java 反射获取方法并赋值,反射获取注解

2018-01-12 10:31 330 查看
/**
* 获取方法并赋值
* @param obj
*/
public void give(Object obj){
try {
Class clas = obj.getClass();
Field [] fields = clas.getDeclaredFields();
for (Field field:fields){
PropertyDescriptor pd = new PropertyDescriptor(field.getName(),clas);
Method method = pd.getWriteMethod();
String fieldType = field.getType().getSimpleName();
if(!method.getName().equals("setId")){
if ("String".equals(fieldType)) {
method.invoke(obj, "zxczxczxczx");
} else if ("Date".equals(fieldType)) {
method.invoke(obj, new Date());
} else if ("Integer".equals(fieldType)
|| "int".equals(fieldType)) {
Integer intval = Integer.parseInt("10");
method.invoke(obj, intval);
} else if ("Long".equalsIgnoreCase(fieldType)) {
Long temp = Long.parseLong("11");
method.invoke(obj, temp);
} else if ("Double".equalsIgnoreCase(fieldType)) {
Double temp = Double.parseDouble("12.12");
method.invoke(obj, temp);
} else if ("Boolean".equalsIgnoreCase(fieldType)) {
Boolean temp = Boolean.parseBoolean("true");
method.invoke(obj, temp);
} else if("BigDecimal".equalsIgnoreCase(fieldType)){
method.invoke(obj, new BigDecimal("22.00"));
} else {
System.out.println("not supper type" + fieldType);
}
}
}
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}


@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
@Target({ElementType.FIELD,ElementType.METHOD})//定义注解的作用目标**作用范围字段、枚举的常量/方法
@Documented//说明该注解将被包含在javadoc中
public @interface ExcelExport {
/**
* 字段名称
* @return
*/
String name() default "";
/**
* 排序字段
* @return
*/
int order() default 0;
}


public class User {
private Integer id;

private String userName;

private String password;

@ExcelExport(name="gg")
private String roleId;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

@ExcelExport(name="用户姓名",order=1)
public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

@ExcelExport(name="用户密码",order=2)
public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getRoleId() {
return roleId;
}

public void setRoleId(String roleId) {
this.roleId = roleId;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", roleId='" + roleId + '\'' +
'}';
}
}


/**
* 获取方法上的注解
* @param obj
*/
public void getAnnoMethod(Object obj){
Class clas = obj.getClass();
Method [] methods = clas.getDeclaredMethods();
for (Method metho:methods){
Annotation [] annotations = metho.getDeclaredAnnotations();
if(annotations.length<0){
return;
}
for (Annotation anno:annotations) {
ExcelExport excelExport = (ExcelExport)anno;
System.out.println(excelExport.name());
System.out.println(excelExport.order());
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐