javafield的⽅法_JavaField详解
⼀.主要⽅法
getType(): 获取属性声明时类型对象(返回class对象)
getGenericType() : 返回属性的类型
getName() : 获取属性声明时名字
getAnnotations() : 获得这个属性上所有的注释
getModifiers() : 获取属性的修饰
isEnumConstant() : 判断这个属性是否是类
isSynthetic() : 判断这个属性是否是 复合类
get(Object obj) : 取得obj对象这个Field上的值
t(Object obj, Object value) : 向obj对象的这个Field设置新值value
Field类中最常⽤的是get(Object obj)和t(Object obj, Object value)这两个⽅法,所以这两个⽅法是最重要的。
getType() 和getGenericType()的区别 :
1.⾸先是返回的类型不⼀样,⼀个是Class对象⼀个是Type接⼝
2.如果属性是⼀个泛型,从getType()只能得到这个属性的接⼝类型。但从getGenericType()能得到这个属性接⼝的泛型类型。isEnumConstant()和isSynthetic():
对象中如果有属性是枚举类或复合类,⽤这两个⽅法返回的值并不是我们想象的true⽽是fal。其实这两个⽅法是对编译⽣成的才有效。⼆.试验代码
package test.fortest;
import static java.lang.System.out;
import flect.Field;
import javassist.Modifier;
always in my heart
del.Role;
public class FieldTest {undefined
Color {undefined
数不胜数
Blue,
Red
}
class Inner {undefined
}
public static void main(String args[]) {undefined
Role role = new Role();
role.tId("role1");
role.tRoleName("Role 1");
Field idField = Class(), "id");
Field childrenField = Class(), "children");
computer是什么意思Field roleTypeField = Class(), "roleType");
Field urField = Class(), "ur");
//获取属性声明时类型对象(返回class对象)
System.out.Type());
//返回属性声的Type类型
System.out.GenericType());
//如果属性是⼀个泛型,从getType只能得到这个属性的接⼝类型
篮球兴趣班System.out.Type());
/
/如果属性是⼀个参数化类型,从getGenericType还能得到这个泛型的参数类型System.out.GenericType());
//获取属性声明时名字
System.out.Name());
//获得这个属性上所有的注释
System.out.Annotations().length);
//获取属性的修饰
System.out.Modifiers()));
//判断这个属性是否是枚举类
System.out.println(roleTypeField.isEnumConstant());
//判断这个属性是否是 复合类
System.out.println(urField.isSynthetic());
//FieldTest$Color是Color枚举类编译后的名字。isSyntheticOrEnumConstant("test.fortest.FieldTest$Color");
theory//FieldTest$Inner是Inner类编译后的名字。
isSyntheticOrEnumConstant("test.fortest.FieldTest$Inner");
英文流行歌曲try {undefined
//取得对象这个Field上的值automacro
初2英语作文
System.out.(role));
//向对象的这个Field重新设置值
idField.t(role, "role2");
System.out.(role));
e.printStackTrace();
} catch (IllegalAccessException e) {undefined
e.printStackTrace();
}
}
public static Field getDeclaredField(final Class cla, final String fieldName) {undefined
for (Class superClass = cla; superClass != null; superClass = Superclass()) {undefined try {undefined
DeclaredField(fieldName);
} catch (NoSuchFieldException e) {undefined
//e.printStackTrace();
}
}
return null;
}
public static void isSyntheticOrEnumConstant (String completePackageName) {undefined
try {undefined
Class> c = Class.forName(completePackageName);
Field[] flds = c.getDeclaredFields();
for (Field f : flds) {undefined
out.format("%-8s [ synthetic=%-5b enum_constant=%-5b ]%n",
}
智慧消防工程师} catch (ClassNotFoundException e) {undefined
e.printStackTrace();
}
}
}
结果:
class java.lang.String
class java.lang.String
interface java.util.List
java.util.List
5
public
fal
fal
test.fortest.FieldTest$Color:Blue [ synthetic=fal enum_constant=true ]
test.fortest.FieldTest$Color:Red [ synthetic=fal enum_constant=true ]
test.fortest.FieldTest$Color:ENUM$VALUES [ synthetic=true enum_constant=fal ] test.fortest.FieldTest$Inner:this$0 [ synthetic=true enum_constant=fal ]
role1
role2
三.⽤到的类
del;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Table;
u.RoleType;
@Entity
@Table(schema = "public")
public class Role extends AbsEntity implements IEntity{undefined
public String roleName;
public String urId;
public List children;
private RoleType roleType = RoleType.Manager;
private Ur ur;
public Role() {}
private Role(String roleName) {undefined
}
public String getRoleName() {undefined
return roleName;
}
public void tRoleName(String roleName) {undefined
public String getUrId() {undefined
return urId;
}
public void tUrId(String urId) {undefined
this.urId = urId;
}
public String getDisplayString() {undefined
System.out.println("I am a Role");
return "Role";
}
}
u;
public enum RoleType{undefined
Manager,Employee;
}
del;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.AccessType;
import org.hibernate.annotations.GenericGenerator;英语学习网站
@Entity
@Table(name = "urs", schema = "public")
public class Ur {undefined
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid.hex") @Column(length = 40)
@AccessType("property")
private String id;