Is there a way to pass a Java annotation as a parameter? -


Is there a way to pass an explanation directly in the form of parameters (overhead by all reflection)? For example, in the following code, I have an annotation number which holds an int value, I want to pass in the addImpl method as a parameter, how can I do this (except for reflection)?

Code Snippet:

@Retention (RetentionPolicy.RUNTIME) @Target ({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD}) Public @InterfaceFace number {int Value (); } Public Zero Addition (int x2) {addImpl (@ number (value = 10) worthy, x2); } Public Zero addImpl (number a, int b) {System.out.println (a.value () + b); } Public static zero main (string [] Args) {New Test Class (). Add (3); }

Yes, you can exceed such annotations (like they Common interface).

The only thing that you can not do to create an example of that interface on the runtime, you can only take existing annotations and pass them.

  import java.lang.annotation. *; Examples of public class {@titance (retention policy. RUNTIME) @Target (ElementType.mohdod) public stable @ interface number {int veril (); } @ Number (value = 42) public int adder (final integer B) throws security Exception, NoSuchMethodException {number number = getClass (). GetMethod ("Adder", int.class) .Get annotation (number.class); Return addImpl (number, b); } Public int addImpl (last number one, last int b) {return a.value () + b; } Public static zero main (last string [] args throws security Exception, NoSuchMethodException {System.out.println (New Example (.). Joiner (0)); }}  

Comments