Created
April 23, 2014 04:55
-
-
Save 0xdevalias/11203141 to your computer and use it in GitHub Desktop.
Example code for accessing methods on a Java annotation using reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TODO: Example code for accessing variables/etc in annotations. | |
// TODO: Could this be done nicely with a 'map' type functional extraction? | |
Method m; | |
try | |
{ | |
m = Class.forName("MyClass").getMethod("someMethod"); // Class/method that are annotated | |
MyAnnotation a = m.getAnnotation(MyAnnotation.class); // Annotation class | |
String someString = a.aStringMethod(); // Thing we want to access | |
} | |
catch (SecurityException e) | |
{ | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
catch (NoSuchMethodException e) | |
{ | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
catch (ClassNotFoundException e) | |
{ | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment