Created
October 11, 2010 13:15
-
-
Save cocoatomo/620487 to your computer and use it in GitHub Desktop.
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
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"); | |
} | |
} |
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
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