1

I have this annotated class in one of my projects.
The annotation class is defined by me.

Question: can I instead of the String "TP_REPORT" use some property
whose value is not known at compile time but only at runtime?

@PhoenixTable(tableName="TP_REPORT")
public class TPData {
    ...
}

Just as a reference, here is the definition of my annotation type.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface PhoenixTable {

    public String tableName();

}
Impulse The Fox
  • 2,638
  • 2
  • 27
  • 52
peter.petrov
  • 38,363
  • 16
  • 94
  • 159

2 Answers2

4

No. It's not possible in Java. Values in an annotation should be constant at compile time.

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
0

You cannot give any object reference since you don't have any object when declaring an annotation. From docs by Oracle (click here):

Return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types.

But if you absolutely need something dynamic, you can give a class and use that class to read from some property file. Although this may not be the best solution for you, it may give you something to investigate.

public interface TableNameProp{
    public static String getTableName(){
        return "TP_REPORT"; //or read from some prop and use it
    }
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface PhoenixTable {

    public Class<TableNameProp> tableProps();

}

@PhoenixTable(tableProps=TableNameProp.class)
public class TPData {
    ...
}
Cem Özer
  • 1,263
  • 11
  • 19