Suppose we have a class with different users, as soon as defined in the package,
@ one square a {} @ two square b {} @ one square C {} < / Code>
Which class is using @one
annotation and which @wo
?
or
We are a class, in which the ways are annotated and I want to retrieve the annotations which are annotations
Like:
class one {@two public Method1 () {} @one public Method2 () {} @two public method3 () {}}
/ Pre>I am using core Java reflections and annotations.
Any other thoughts will be appreciated for doing so. Any help would be appreciated
This is my trail code which is not working. If there is any new way to guide me
Import java.lang.annotation.Annotation; Import java.lang.reflect.Method; Import java.util.Arrays; @GUI Public Category Demo {Public static zero example (Demo Ob = new demo); Try {class c = ob.getClass (); // Get an example of method method m = c.getMethod ("Example"); // Take Annotation Annotation [] Annotation = M. Gain Annotation (); Print the annotations for // (Int i = 0; i & lt; annotation.lamper; i ++) {System.out.println (annotation [i]); }} Hold (Noschmee exposure inquiry) {exc.printStackTrace (); }} Public static zero show () {java.lang.annotation.notation [] annotation = null; Try {Annotations = Class.forName ("Demo"). GetNnotations (); } Catch (Clasnostfundai Express E) {// To-O Auto-Generated Catch Block e.printStackTrace (); } // print [@ java.lang.Deprecated ()] System.out.println (Arrays.toString (annotation)); } Public stable zero myMeth () {Demo Ob = new demo (); {Annotation annos [] = Try ob.getClass (). GetAnnotations (); System.out.println ("All Annotations for Meta 2:"); (Annotation A: Anon) system OutprintLine (A); Method m = ob.getClass (). GetMethod ("myMeth"); Annos = mg annotation (); System.out.println ("All comments for my math:"); (Annotation A: Anon) system OutprintLine (A); } Grip (Noschmexception Ax) {System.out.println ("Method not found."); }} Public static zero principal (string agre []) {example (; Show (); MyMeth (); }}
Once you find the annotation on the class or method, Types of annotations can
annotation one = ..... if (a.annotationType (.) Equals (GUI.class)) {println ("found"); }
Edit: Full working class
@Deprecated public class demo {public static void example} {try {class c = Demo.class; // Receive annotation annotation [] Class annotation = c.getAnnation (); // print class annotation (I int = 0; I & lt; classAnnotations.length; i ++) for {if (classAnnotations [i] .annotationType () equals (Deprecated.class).) {Println ("class annotation : "+ Class Annotation [I] .Notation Type ()); }} // Get Method Method M = C Megthi ("Show"); Explanation [] Method Annotation = M. Gave Disapproved Annotation (); // print method annotation (i int = 0; i & lt; methodAnnotations.length; i ++) {if (methodAnnotations [i] .annotationType () is equal to (Deprecated.class).) {Println ("method annotation : "+ Method annotation [i] .notation type ()); }}} Hold (Noschmie Exception Ax) {exc.printStackTrace (); }} @Deprected Public Static Virus Show () {} Public Static Wide Men (string AGR []) {e.g. (; }}