newcoder1010 Posted March 23, 2022 Share Posted March 23, 2022 Hello I am doing annotation first time. package annotationsPractice; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.ElementType; @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD }) public @interface CustomAnnotation { public enum MYAPPS { app1, app2, noSet } public enum AUTHORS { john, mike, noSet } String ID() default "null"; AUTHORS AUTHOR() default AUTHORS.noSet; MYAPPS APP() default MYAPPS.noSet; } ClassA package annotationsPractice; import annotationsPractice.CustomAnnotation.AUTHORS; public class classA extends classB{ @CustomAnnotation(ID = "100", APP = CustomAnnotation.MYAPPS.app1, AUTHOR = AUTHORS.mike) public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("main"); getAnnotationAttributes(); } } classB package annotationsPractice; import java.lang.annotation.Annotation; import com.sun.jdi.Method; import annotationsPractice.CustomAnnotation.AUTHORS; public class classB { public static void getAnnotationAttributes() { java.lang.reflect.Method[] methods = classA.class.getMethods(); String id = methods.getClass().getAnnotation(CustomAnnotation.class).ID(); System.out.println("id " + id); } } When I run ClassA, I was expecting to see annotation attributes in ClassB. How do I get the attribute values (ID = "100") in ClassB when I run Class A? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now