fork download
  1. import java.lang.annotation.ElementType;
  2. import java.lang.annotation.Retention;
  3. import java.lang.annotation.RetentionPolicy;
  4. import java.lang.annotation.Target;
  5. import java.lang.reflect.Field;
  6. import java.lang.reflect.Modifier;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9.  
  10. @Retention(RetentionPolicy.RUNTIME)
  11. @Target(ElementType.FIELD)
  12. @interface PatternHandler {
  13. String value();
  14. }
  15.  
  16. interface TownyChatReplacerCallable {
  17. String call(String match, String event);
  18. }
  19.  
  20. class AnnotationRegistry {
  21. private static final Map<String, TownyChatReplacerCallable> MAP = new HashMap<String, TownyChatReplacerCallable>();
  22. public static void register(String pattern, TownyChatReplacerCallable handler) {
  23. System.out.println("Registering pattern:" + pattern + " handler:" + handler);
  24. MAP.put(pattern, handler);
  25. }
  26. public static void handle(String pattern, String event) {
  27. TownyChatReplacerCallable handler = MAP.get(pattern);
  28. if (handler != null) {
  29. String result = handler.call(pattern, event);
  30. System.out.println("Handler returned:" + result);
  31. } else {
  32. System.out.println("No Handler for:" + pattern);
  33. }
  34. }
  35.  
  36. public static void register(Class<?> clazz) {
  37. Field[] fields = clazz.getDeclaredFields();
  38. for (Field field : fields) {
  39. PatternHandler annotation = field.getAnnotation(PatternHandler.class);
  40. if (annotation != null) {
  41. if (!Modifier.isStatic(field.getModifiers())) {
  42. System.out.println("Field must be static:" + field.getName());
  43. continue;
  44. }
  45. try {
  46. Object object = field.get(null);
  47. if (object instanceof TownyChatReplacerCallable) {
  48. register(annotation.value(), (TownyChatReplacerCallable) object);
  49. } else {
  50. System.out.println("Field must be instanceof TownyChatReplacerCallable:" + field.getName());
  51. }
  52. e.printStackTrace();
  53. } catch (IllegalAccessException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. }
  58. }
  59. }
  60.  
  61. class Callables {
  62.  
  63. @PatternHandler("foo")
  64. public static final TownyChatReplacerCallable FOO = new TownyChatReplacerCallable() {
  65. @Override
  66. public String call(String match, String event) {
  67. return "This is foo handler called with " + match + "," + event;
  68. }
  69. };
  70.  
  71. @PatternHandler("bar")
  72. public static final TownyChatReplacerCallable BAR = new TownyChatReplacerCallable() {
  73. @Override
  74. public String call(String match, String event) {
  75. return "This is foo handler called with " + match + "," + event;
  76. }
  77. };
  78.  
  79. @PatternHandler("lala")
  80. public static final String WRONG_CLASS = "";
  81.  
  82. @PatternHandler("lulu")
  83. public final TownyChatReplacerCallable notStatic = new TownyChatReplacerCallable() {
  84. @Override
  85. public String call(String match, String event) {
  86. return "This is foo handler called with " + match + "," + event;
  87. }
  88. };
  89. }
  90.  
  91. public class Main {
  92. public static void main(String[] args) {
  93. AnnotationRegistry.register(Callables.class);
  94.  
  95. System.out.println("\n-- Done with registration --\n");
  96.  
  97. AnnotationRegistry.handle("foo", "event1");
  98. AnnotationRegistry.handle("bar", "event2");
  99. AnnotationRegistry.handle("zoom", "event3");
  100. }
  101. }
  102.  
Success #stdin #stdout 0.09s 380352KB
stdin
Standard input is empty
stdout
Registering pattern:foo handler:Callables$1@157011e
Registering pattern:bar handler:Callables$2@10a8143
Field must be instanceof TownyChatReplacerCallable:WRONG_CLASS
Field must be static:notStatic

-- Done with registration --

Handler returned:This is foo handler called with foo,event1
Handler returned:This is foo handler called with bar,event2
No Handler for:zoom