【问题标题】:IntelliJ/JDK 11: Cannot find symbol name() interface java.lang.annotation.AnnotationIntelliJ/JDK 11:找不到符号名称()接口 java.lang.annotation.Annotation
【发布时间】:2020-06-15 21:59:45
【问题描述】:

我有这个简单的代码:

String tableName = MyEntity.class.getAnnotation( javax.persistence.Table.class ).name();

但 IntelliJ 强调了 name() 方法不存在 Cannot resolve method "name()" 的事实。

我在编译过程中也遇到了这个错误:

Error:(29, 68) java: cannot find symbol
  symbol:   method name()
  location: interface java.lang.annotation.Annotation

我看到很多使用这种方法的示例代码,这里有一个示例:https://stackoverflow.com/a/1320890/6643803 我错过了什么?

谢谢

【问题讨论】:

    标签: java maven reflection annotations


    【解决方案1】:

    如您所见,hereAnnotation 接口中没有name() 函数

    你可能需要投射它:

    Table table = MyEntity.class.getAnnotation(javax.persistence.Table.class);
    String tableName = table.name();
    

    【讨论】:

    • 糟糕,我忘记了 Java 的冗长。谢谢
    • @akuma8 如果你想要更简洁的方式,你可以使用((Table)...).name()
    猜你喜欢
    • 2019-03-01
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2019-04-26
    • 2017-02-19
    • 1970-01-01
    • 2018-05-15
    • 2015-01-11
    相关资源
    最近更新 更多