Java 如何获取成员变量的注释?
我想知道一个类的一些成员变量的注释,我使用Java 如何获取成员变量的注释?,java,reflection,annotations,beaninfo,Java,Reflection,Annotations,Beaninfo,我想知道一个类的一些成员变量的注释,我使用BeanInfo BeanInfo=Introspector.getBeanInfo(User.class)来内省一个类,使用BeanInfo.getPropertyDescriptors(),来查找特定的属性,并使用classtype=propertyDescriptor.getPropertyType()获取属性的类 但是我不知道如何将注释添加到成员变量中 我尝试了type.getAnnotations()和type.getDeclaredAnnot
BeanInfo BeanInfo=Introspector.getBeanInfo(User.class)
来内省一个类,使用BeanInfo.getPropertyDescriptors()
,来查找特定的属性,并使用classtype=propertyDescriptor.getPropertyType()
获取属性的类
但是我不知道如何将注释添加到成员变量中
我尝试了type.getAnnotations()
和type.getDeclaredAnnotations()
,但两者都返回了类的注释,而不是我想要的。例如:
class User
{
@Id
private Long id;
@Column(name="ADDRESS_ID")
private Address address;
// getters , setters
}
@Entity
@Table(name = "Address")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
class Address
{
...
}
我要获取地址的批注:@列,而不是类地址的批注(@Entity、@Table、@Cache)。如何实现?谢谢。您必须使用反射来获取
User
类的所有成员字段,遍历它们并找到它们的注释
大概是这样的:
public void getAnnotations(Class clazz){
for(Field field : clazz.getDeclaredFields()){
Class type = field.getType();
String name = field.getName();
field.getDeclaredAnnotations(); //do something to these
}
}
您可以在getter方法上获取注释:
propertyDescriptor.getReadMethod().getDeclaredAnnotations();
获取私有字段的注释似乎是个坏主意。。。如果属性甚至不受字段支持,或者受具有不同名称的字段支持,该怎么办?即使忽略这些案例,你也在通过查看私人资料来打破抽象
for(Field field : cls.getDeclaredFields()){
Class type = field.getType();
String name = field.getName();
Annotation[] annotations = field.getDeclaredAnnotations();
}
另见:
和一个测试类:
package be.fery.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class AnnotationIntrospector {
public AnnotationIntrospector() {
super();
}
public Annotation[] findClassAnnotation(Class<?> clazz) {
return clazz.getAnnotations();
}
public Annotation[] findMethodAnnotation(Class<?> clazz, String methodName) {
Annotation[] annotations = null;
try {
Class<?>[] params = null;
Method method = clazz.getDeclaredMethod(methodName, params);
if (method != null) {
annotations = method.getAnnotations();
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return annotations;
}
public Annotation[] findFieldAnnotation(Class<?> clazz, String fieldName) {
Annotation[] annotations = null;
try {
Field field = clazz.getDeclaredField(fieldName);
if (field != null) {
annotations = field.getAnnotations();
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return annotations;
}
/**
* @param args
*/
public static void main(String[] args) {
AnnotationIntrospector ai = new AnnotationIntrospector();
Annotation[] annotations;
Class<User> userClass = User.class;
String methodDoStuff = "doStuff";
String fieldId = "id";
String fieldAddress = "address";
// Find class annotations
annotations = ai.findClassAnnotation(be.fery.annotation.User.class);
System.out.println("Annotation on class '" + userClass.getName()
+ "' are:");
showAnnotations(annotations);
// Find method annotations
annotations = ai.findMethodAnnotation(User.class, methodDoStuff);
System.out.println("Annotation on method '" + methodDoStuff + "' are:");
showAnnotations(annotations);
// Find field annotations
annotations = ai.findFieldAnnotation(User.class, fieldId);
System.out.println("Annotation on field '" + fieldId + "' are:");
showAnnotations(annotations);
annotations = ai.findFieldAnnotation(User.class, fieldAddress);
System.out.println("Annotation on field '" + fieldAddress + "' are:");
showAnnotations(annotations);
}
public static void showAnnotations(Annotation[] ann) {
if (ann == null)
return;
for (Annotation a : ann) {
System.out.println(a.toString());
}
}
}
包be.fery.annotation;
导入java.lang.annotation.annotation;
导入java.lang.reflect.Field;
导入java.lang.reflect.Method;
公共类注释导入器{
公共注释导入器(){
超级();
}
公共批注[]findClassAnnotation(类clazz){
返回clazz.getAnnotations();
}
公共注释[]FindMethodAnotation(类clazz,字符串methodName){
注释[]注释=null;
试一试{
类[]参数=null;
方法Method=clazz.getDeclaredMethod(方法名,参数);
if(方法!=null){
annotations=method.getAnnotations();
}
}捕获(安全异常e){
e、 printStackTrace();
}捕获(无此方法例外){
e、 printStackTrace();
}
返回注释;
}
公共批注[]findFieldAnnotation(类clazz,字符串字段名){
注释[]注释=null;
试一试{
字段字段=clazz.getDeclaredField(字段名);
如果(字段!=null){
annotations=field.getAnnotations();
}
}捕获(安全异常e){
e、 printStackTrace();
}捕获(无此字段例外){
e、 printStackTrace();
}
返回注释;
}
/**
*@param args
*/
公共静态void main(字符串[]args){
AnnotationIntrospector ai=新的AnnotationIntrospector();
注释[]注释;
Class userClass=User.Class;
String methodDoStuff=“doStuff”;
字符串fieldId=“id”;
字符串fieldAddress=“地址”;
//查找类注释
annotations=ai.findClassAnnotation(be.fery.annotation.User.class);
System.out.println(“类“”上的注释+userClass.getName()
+“‘是’”;
显示注释(注释);
//查找方法注释
annotations=ai.findMethodAnotation(User.class,methodDoStuff);
println(“方法上的注释””+methodDoStuff+“”是:”;
显示注释(注释);
//查找字段注释
annotations=ai.findFieldAnnotation(User.class,fieldId);
System.out.println(“字段“”上的注释+fieldId+”是:”;
显示注释(注释);
annotations=ai.findFieldAnnotation(User.class,fieldAddress);
System.out.println(“字段“”上的注释+字段地址+”是:”;
显示注释(注释);
}
公共静态void注释(注释[]ann){
如果(ann==null)
返回;
用于(注释a:ann){
System.out.println(a.toString());
}
}
}
希望对你有帮助
;-) 每个人都描述了获取注释的问题,但问题在于注释的定义。您应该在注释定义中添加一个
@Retention(RetentionPolicy.RUNTIME)
:
或者你可以试试这个
try {
BeanInfo bi = Introspector.getBeanInfo(User.getClass());
PropertyDescriptor[] properties = bi.getPropertyDescriptors();
for(PropertyDescriptor property : properties) {
//One way
for(Annotation annotation : property.getAnnotations()){
if(annotation instanceof Column) {
String string = annotation.name();
}
}
//Other way
Annotation annotation = property.getAnnotation(Column.class);
String string = annotation.name();
}
}catch (IntrospectonException ie) {
ie.printStackTrace();
}
希望这会有所帮助。如果您需要知道是否存在特定的注释。你可以这样做:
Field[] fieldList = obj.getClass().getDeclaredFields();
boolean isAnnotationNotNull, isAnnotationSize, isAnnotationNotEmpty;
for (Field field : fieldList) {
//Return the boolean value
isAnnotationNotNull = field.isAnnotationPresent(NotNull.class);
isAnnotationSize = field.isAnnotationPresent(Size.class);
isAnnotationNotEmpty = field.isAnnotationPresent(NotEmpty.class);
}
其他注释也是如此
我希望能帮助别人。以我的方式
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
public class ReadAnnotation {
private static final Logger LOGGER = LoggerFactory.getLogger(ReadAnnotation.class);
public static boolean hasIgnoreAnnotation(String fieldName, Class entity) throws NoSuchFieldException {
return entity.getDeclaredField(fieldName).isAnnotationPresent(IgnoreAnnotation.class);
}
public static boolean isSkip(PropertyDescriptor propertyDescriptor, Class entity) {
boolean isIgnoreField;
try {
isIgnoreField = hasIgnoreAnnotation(propertyDescriptor.getName(), entity);
} catch (NoSuchFieldException e) {
LOGGER.error("Can not check IgnoreAnnotation", e);
isIgnoreField = true;
}
return isIgnoreField;
}
public void testIsSkip() throws Exception {
Class<TestClass> entity = TestClass.class;
BeanInfo beanInfo = Introspector.getBeanInfo(entity);
for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
System.out.printf("Field %s, has annotation %b", propertyDescriptor.getName(), isSkip(propertyDescriptor, entity));
}
}
}
import org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入java.beans.BeanInfo;
导入java.beans.Introspector;
导入java.beans.PropertyDescriptor;
公共类ReadAnnotation{
私有静态最终记录器Logger=LoggerFactory.getLogger(ReadAnnotation.class);
公共静态布尔hasIgnoreAnnotation(字符串字段名,类实体)抛出NoSuchFieldException{
返回实体.getDeclaredField(fieldName).isAnnotationPresent(IgnoreAnnotation.class);
}
公共静态布尔isSkip(PropertyDescriptor PropertyDescriptor,类实体){
布尔isIgnoreField;
试一试{
isIgnoreField=hasIgnoreAnnotation(propertyDescriptor.getName(),entity);
}捕获(无此字段例外){
LOGGER.错误(“无法检查IgnoreAnnotation”,e);
isIgnoreField=true;
}
返回isIgnoreField;
}
public void testIsSkip()引发异常{
类实体=TestClass.Class;
BeanInfo BeanInfo=内省者.getBeanInfo(实体);
对于(PropertyDescriptor PropertyDescriptor:beanInfo.getPropertyDescriptors()){
System.out.printf(“字段%s,有注释%b”,propertyDescriptor.getName(),isSkip(propertyDescriptor,entity));
}
}
}
谢谢,但我的clazz没有公共无参数构造函数。。。(对不起,我没提到)。因此,clazz.newInstance()不会
Field[] fieldList = obj.getClass().getDeclaredFields();
boolean isAnnotationNotNull, isAnnotationSize, isAnnotationNotEmpty;
for (Field field : fieldList) {
//Return the boolean value
isAnnotationNotNull = field.isAnnotationPresent(NotNull.class);
isAnnotationSize = field.isAnnotationPresent(Size.class);
isAnnotationNotEmpty = field.isAnnotationPresent(NotEmpty.class);
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
public class ReadAnnotation {
private static final Logger LOGGER = LoggerFactory.getLogger(ReadAnnotation.class);
public static boolean hasIgnoreAnnotation(String fieldName, Class entity) throws NoSuchFieldException {
return entity.getDeclaredField(fieldName).isAnnotationPresent(IgnoreAnnotation.class);
}
public static boolean isSkip(PropertyDescriptor propertyDescriptor, Class entity) {
boolean isIgnoreField;
try {
isIgnoreField = hasIgnoreAnnotation(propertyDescriptor.getName(), entity);
} catch (NoSuchFieldException e) {
LOGGER.error("Can not check IgnoreAnnotation", e);
isIgnoreField = true;
}
return isIgnoreField;
}
public void testIsSkip() throws Exception {
Class<TestClass> entity = TestClass.class;
BeanInfo beanInfo = Introspector.getBeanInfo(entity);
for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
System.out.printf("Field %s, has annotation %b", propertyDescriptor.getName(), isSkip(propertyDescriptor, entity));
}
}
}