Classインスタンスをパラメータとする関数を記述しています。クラスに定義された特定の注釈の価値を取得したい。 クラス:注釈値を取得したいJavaを使用してクラスレベルの注釈値を取得するにはどうすればいいですか?
@AllArgConstructor
@MyAnnotation(tableName = "MyTable")
public class MyClass {
String field1;
}
機能。
public class AnnotationValueGetter{
public String getTableName-1(Class reflectClass){
if(reflectClass.getAnnotation(MyAnnotation.class)!=null){
return reflectClass.getAnnotation(MyAnnotation.class).tableName();
//This does not work. I am not allowed to do .tableName(). Java compilation error
}
}
public String getTableName-2{
Class reflectClass = MyClass.class;
return reflectClass.getAnnotation(MyAnnotation.class).tableName()
//This works fine.`enter code here`
}
}
MyAnnotation:getTableName-2だけで正常に動作し、一方、
@DynamoDB
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface MyAnnotation {
/**
* The name of the table to use for this class.
*/
String tableName();
}
機能getTableName-1は私にコンパイルエラーを示しています。私はここで間違って何をしていますか? getTableName-1と同様の機能を実装したいと思います。ジェネリック型引数を持つ
てください、あなたが得る正確なコンパイラエラーメッセージは何@MyAnnotation –
のコードを追加しますか? – SpaceTrucker
@SpaceTrucker Intellijは.tableName()を実行できません。メソッドtableName()を解決できません –