Skip to content

Instantly share code, notes, and snippets.

@cocoatomo
Created October 11, 2010 13:15
Show Gist options
  • Save cocoatomo/620487 to your computer and use it in GitHub Desktop.
Save cocoatomo/620487 to your computer and use it in GitHub Desktop.
package annotations;
import java.lang.reflect.InvocationTargetException;
@TestAnnotation(hoge="fuga")
public class Annotated {
/**
* @param args
*/
public static void main(String[] args) {
Class<Annotated> thisClass = Annotated.class;
TestAnnotation a = thisClass.getAnnotation(TestAnnotation.class);
Class<TestAnnotation.Fuga> fugaClass = a.logic();
try {
TestAnnotation.Fuga fuga = fugaClass.newInstance();
fugaClass.getMethod("greet").invoke(fuga);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println("hoge");
}
}
package annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
String hoge() default "hoge";
Class<TestAnnotation.Fuga> logic() default Fuga.class;
public class Fuga {
public void greet() {
System.out.println("Hello World!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment