Can annotation have complex return type, such as HashMap.
I am looking for something like:
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface column { public HashMap<String, String> table(); }
so I can have a constant annotated like(pseudo code):
@column({table=(dbName, tableName), table=(dbName, tableName2)}) public static final String USER_ID = "userid";
If Annotation doesn't allow you to have complex return type, then any good practice for this kind of case?
TYPE_USE , the annotation applies to the return type. If it uses ElementType. METHOD , it applies to the method declaration. If it uses both (or none), it applies to both the return type and method declaration.
What Object Types Can Be Returned from an Annotation Method Declaration? The return type must be a primitive, String, Class, Enum, or an array of one of the previous types. Otherwise, the compiler will throw an error.
Which of the following is not pre defined annotation in Java? Explanation: @Overriden is not a pre defined annotation in Java. @Depricated, @Override, @SuppressWarnings, @SafeVarags and @FunctionInterface are the pre defined annotations.
2. Re: try catch around annotated method. Right approach, but Seam doesn't support method level interceptors, only class level interceptors. So you can attach the interceptor at class level, and then mark the method with the same annotation, and only intercept those.
No, annotation elements can only be primitive types, Strings, enum
types, Class
, other annotations, or arrays of any of these. The typical way to represent these kinds of structures would be to declare another annotation type
public @interface TableMapping { public String dbName(); public String tableName(); }
then say
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface column { public TableMapping[] table(); }
And use the annotation as
@column(table={ @TableMapping(dbName="dbName", tableName="tableName"), @TableMapping(dbName="db2", tableName="table2") }) public String userId = "userid";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With