Skip to content

Instantly share code, notes, and snippets.

@0xdevalias
Created April 23, 2014 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xdevalias/11203141 to your computer and use it in GitHub Desktop.
Save 0xdevalias/11203141 to your computer and use it in GitHub Desktop.
Example code for accessing methods on a Java annotation using reflection
// 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