/** * Extracts the {@link ConditionMethod}s from the given class by processing the * {@link Condition} annotation. * * @param clazz * The class to be analyzed. * @return List of ConditionMethods * @throws RuleDefinitionException * If {@link ConditionMethod} annotations are invalid. */ public static List<ConditionMethod> describeConditionMethods(Class<?> clazz) throws RuleDefinitionException { List<ConditionMethod> conditions = ReflectionUtils.visitMethodsAnnotatedWith(Condition.class, clazz, new Visitor<Condition, Method, ConditionMethod>() { @Override public ConditionMethod visit(Condition annotation, Method method) throws RuleDefinitionException { return new ConditionMethod(annotation.name(), annotation.hint(), method); } }); return conditions; } }
/** * Extracts the {@link Action} from the given class by processing the {@link Action} annotation. * * @param clazz * The class to be analyzed. * @return List of ActionMethod * @throws RuleDefinitionException * If {@link Action} annotation is invalid. */ public static ActionMethod describeActionMethod(Class<?> clazz) throws RuleDefinitionException { List<ActionMethod> actionMethods = ReflectionUtils.visitMethodsAnnotatedWith(Action.class, clazz, new Visitor<Action, Method, ActionMethod>() { @Override public ActionMethod visit(Action annotation, Method method) throws RuleDefinitionException { return new ActionMethod(method, annotation.resultTag(), annotation.resultQuantity()); } }); if ((null == actionMethods) || (actionMethods.size() != 1)) { throw new RuleDefinitionException("A rule must define exactly one method annotated with @Action. Otherwise the rule could never be exectued."); } else { return actionMethods.get(0); } }
@Test(expectedExceptions = { NullPointerException.class }) public void visitorReturnsNull() throws RuleDefinitionException { ReflectionUtils.visitMethodsAnnotatedWith(Action.class, TestClass.class, new Visitor<Action, Method, String>() { @Override public String visit(Action annotation, Method method) { return method.getName().endsWith("C") ? null : "ok"; } }); }
@Test(expectedExceptions = { RuleDefinitionException.class }) public void explicitVisitorException() throws RuleDefinitionException { ReflectionUtils.visitMethodsAnnotatedWith(Action.class, TestClass.class, new Visitor<Action, Method, String>() { @Override public String visit(Action annotation, Method method) throws RuleDefinitionException { if (method.getName().endsWith("C")) { throw new RuleDefinitionException("test exception"); } else { return "ok"; } } }); } }
@Test public void collectAllMethods() throws RuleDefinitionException { List<String> methodNames = ReflectionUtils.visitMethodsAnnotatedWith(Action.class, TestClass.class, new Visitor<Action, Method, String>() { @Override public String visit(Action annotation, Method method) { return method.getName(); } }); assertThat(methodNames, containsInAnyOrder("actionA", "actionB", "actionC", "actionD")); }
results.addAll(visitMethodsAnnotatedWith(annotation, clazz.getSuperclass(), visitor));