In my this tutorial, I am going to demonstrate how to extract method details with Annotation details with Reflection mechanism. Have a look on complete details and let me know if I you need some more details.
Below interface is @Retention.RUNTIME Annotation enabled.
1 2 3 4 5 6 7 8 9 |
package www.technicaljungle.com; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface RefRetentionInterface { String value(); } |
In below method, I tried to showcase different types of Annotations in order to distinguish at Reflection class in RefTestReflection class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package www.technicaljungle.com; public class RefRetentionSample { @RefRetentionInterface ("Alice") public void aliceMethod() { System.out.println("This method is written by Alice"); } @RefRetentionInterface ("Popeye") public void buildHouse() { System.out.println("This method is written by Popeye"); } /* * This is just a dummy method. */ public void dummyMethod() { System.out.println("Dummy Method"); } } |
Below class showcase what all methods are from Annotated class and how many from without Annotated class. We are using reflection mechanism to find out methods details.
Reflection for Extracting Method Info
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package www.technicaljungle.com; import java.lang.annotation.Annotation; import java.lang.reflect.Method; public class RefTestReflection { public static void main(String args[]) throws SecurityException, ClassNotFoundException { for (Method method : Class.forName( "www.technicaljungle.com.RefRetentionSample").getMethods()) { // checks if there is annotation present of the given type Developer if (method .isAnnotationPresent(www.technicaljungle.com.RefRetentionInterface.class)) { try { // iterates all the annotations available in the method for (Annotation anno : method.getDeclaredAnnotations()) { System.out.println("Annotation in Method '" + method + "' : " + anno); RefRetentionInterface a = method.getAnnotation(RefRetentionInterface.class); if ("Popeye".equals(a.value())) { System.out.println("Popeye the sailor man! " + method); } } } catch (Throwable ex) { ex.printStackTrace(); } } else{ System.out.println("Dummy Method -- " + method); } } } } |
Have a look on below method listing. “Dummy Methods” will fall in “else” part and “@Annotated Methods” will fall under “if” block.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Annotation in Method 'public void www.technicaljungle.com.RefRetentionSample.aliceMethod()' : @www.technicaljungle.com.RefRetentionInterface(value=Alice) Annotation in Method 'public void www.technicaljungle.com.RefRetentionSample.buildHouse()' : @www.technicaljungle.com.RefRetentionInterface(value=Popeye) Popeye the sailor man! public void www.technicaljungle.com.RefRetentionSample.buildHouse() Dummy Method -- public void www.technicaljungle.com.RefRetentionSample.dummyMethod() Dummy Method -- public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException Dummy Method -- public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException Dummy Method -- public final void java.lang.Object.wait() throws java.lang.InterruptedException Dummy Method -- public boolean java.lang.Object.equals(java.lang.Object) Dummy Method -- public java.lang.String java.lang.Object.toString() Dummy Method -- public native int java.lang.Object.hashCode() Dummy Method -- public final native java.lang.Class java.lang.Object.getClass() Dummy Method -- public final native void java.lang.Object.notify() Dummy Method -- public final native void java.lang.Object.notifyAll() |
@Annotations