是否可以在java中读取注释的值?

时间:2010-11-28 13:16:38

标签: java annotations getter-setter java-6

这是我的代码:

@Column(columnName="firstname")


private String firstName;

 @Column(columnName="lastname")
 private String lastName;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

是否可以在另一个类中读取我的注释@Column( columnName =“xyz123”)的值?

9 个答案:

答案 0 :(得分:105)

是的,如果您的Column注释具有运行时保留

@Retention(RetentionPolicy.RUNTIME)
@interface Column {
    ....
}

你可以做这样的事情

for (Field f: MyClass.class.getFields()) {
   Column column = f.getAnnotation(Column.class);
   if (column != null)
       System.out.println(column.columnName());
}

更新:要使用私人字段

Myclass.class.getDeclaredFields()

答案 1 :(得分:76)

当然是。这是一个示例注释:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {

    String testText();
}

示例注释方法:

class TestClass {

    @TestAnnotation(testText="zyx")
    public void doSomething() {}
}

另一个类中的示例方法打印testText的值:

Method[] methods = TestClass.class.getMethods();
for (Method m : methods) {
    if (m.isAnnotationPresent(TestAnnotation.class)) {
        TestAnnotation ta = m.getAnnotation(TestAnnotation.class);
        System.out.println(ta.testText());
    }
}

像你这样的字段注释没什么不同。

Cheerz!

答案 2 :(得分:19)

我从来没有这样做过,但看起来像Reflection提供了这个。 FieldAnnotatedElement,因此它有getAnnotationThis page有一个例子(复制如下);如果您知道注释的类,并且注释策略是否在运行时保留注释,则非常简单。当然,如果保留策略未在运行时保留注释,您将无法在运行时查询它。

自已删除后的答案(?)提供了一个有用的an annotations tutorial链接,您可能会发现它很有用;我在这里复制了链接,以便人们可以使用它。

来自this page的示例:

import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno {
  String str();

  int val();
}

class Meta {
  @MyAnno(str = "Two Parameters", val = 19)
  public static void myMeth(String str, int i) {
    Meta ob = new Meta();

    try {
      Class c = ob.getClass();

      Method m = c.getMethod("myMeth", String.class, int.class);

      MyAnno anno = m.getAnnotation(MyAnno.class);

      System.out.println(anno.str() + " " + anno.val());
    } catch (NoSuchMethodException exc) {
      System.out.println("Method Not Found.");
    }
  }

  public static void main(String args[]) {
    myMeth("test", 10);
  }
}

答案 3 :(得分:4)

虽然到目前为止给出的所有答案都是完全有效的,但是还应该记住google reflections library以获得更通用且更简单的注释扫描方法,例如

 Reflections reflections = new Reflections("my.project.prefix");

 Set<Field> ids = reflections.getFieldsAnnotatedWith(javax.persistence.Id.class);

答案 4 :(得分:4)

阐述@Cephalopod的答案,如果你想要列表中的所有列名,你可以使用这个oneliner:

List<String> columns = 
        Arrays.asList(MyClass.class.getFields())
              .stream()
              .filter(f -> f.getAnnotation(Column.class)!=null)
              .map(f -> f.getAnnotation(Column.class).columnName())
              .collect(Collectors.toList());

答案 5 :(得分:3)

在我的情况下,您还可以使用泛型类型,在您执行以下操作之前考虑所有内容:

public class SomeTypeManager<T> {

    public SomeTypeManager(T someGeneric) {

        //That's how you can achieve all previously said, with generic types.
        Annotation[] an = someGeneric.getClass().getAnnotations();

    }

}

请记住,这与SomeClass.class.get(...)()的100%不相等;

但可以做到这一点......

答案 6 :(得分:3)

通常情况下,您对字段有私有访问权限,因此您无法在反射中使用 getFields 。而不应该使用 getDeclaredFields

因此,首先,您应该知道Column注释是否具有运行时保留:

@Retention(RetentionPolicy.RUNTIME)
@interface Column {
}

之后你可以这样做:

for (Field f: MyClass.class.getDeclaredFields()) {
   Column column = f.getAnnotation(Column.class);
       // ...
}

显然,你想用字段做一些事情 - 使用注释值设置新值:

Column annotation = f.getAnnotation(Column.class);
if (annotation != null) {
    new PropertyDescriptor(f.getName(), Column.class).getWriteMethod().invoke(
        object,
        myCoolProcessing(
            annotation.value()
        )
    );
}

因此,完整代码可以如下所示:

for (Field f : MyClass.class.getDeclaredFields()) {
    Column annotation = f.getAnnotation(Column.class);
    if (annotation != null)
        new PropertyDescriptor(f.getName(), Column.class).getWriteMethod().invoke(
                object,
                myCoolProcessing(
                        annotation.value()
                )
        );
}

答案 7 :(得分:1)

对于少数要求通用方法的人来说,这应该会对您有所帮助(5年后:p)。

对于下面的示例,我从具有RequestMapping批注的方法中提取RequestMapping URL值。 要对此进行调整,只需更改

for (Method method: clazz.getMethods())

for (Field field: clazz.getFields())

RequestMapping 的用法换成您要阅读的注释。 但是请确保注释具有 @Retention(RetentionPolicy.RUNTIME)

public static String getRequestMappingUrl(final Class<?> clazz, final String methodName)
{
    // Only continue if the method name is not empty.
    if ((methodName != null) && (methodName.trim().length() > 0))
    {
        RequestMapping tmpRequestMapping;
        String[] tmpValues;

        // Loop over all methods in the class.
        for (Method method: clazz.getMethods())
        {
            // If the current method name matches the expected method name, then keep going.
            if (methodName.equalsIgnoreCase(method.getName()))
            {
                // Try to extract the RequestMapping annotation from the current method.
                tmpRequestMapping = method.getAnnotation(RequestMapping.class);

                // Only continue if the current method has the RequestMapping annotation.
                if (tmpRequestMapping != null)
                {
                    // Extract the values from the RequestMapping annotation.
                    tmpValues = tmpRequestMapping.value();

                    // Only continue if there are values.
                    if ((tmpValues != null) && (tmpValues.length > 0))
                    {
                        // Return the 1st value.
                        return tmpValues[0];
                    }
                }
            }
        }
    }

    // Since no value was returned, log it and return an empty string.
    logger.error("Failed to find RequestMapping annotation value for method: " + methodName);

    return "";
}

答案 8 :(得分:0)

我使用它的一种方式:

protected List<Field> getFieldsWithJsonView(Class sourceClass, Class jsonViewName){
    List<Field> fields = new ArrayList<>();
    for (Field field : sourceClass.getDeclaredFields()) {
        JsonView jsonViewAnnotation = field.getDeclaredAnnotation(JsonView.class);
        if(jsonViewAnnotation!=null){
            boolean jsonViewPresent = false;
            Class[] viewNames = jsonViewAnnotation.value();
            if(jsonViewName!=null && Arrays.asList(viewNames).contains(jsonViewName) ){
                fields.add(field);
            }
        }
    }
    return fields;
}    
相关问题