/** * Extracts the {@link SessionVariableInjection}s from the given class by processing the * {@link SessionVariable} annotations. * * @param clazz * The class to be analyzed. * @return List of SessionVariableInjections * @throws RuleDefinitionException * If {@link SessionVariableInjection} annotations are invalid. */ public static List<SessionVariableInjection> describeSessionParameterInjections(Class<?> clazz) throws RuleDefinitionException { return ReflectionUtils.visitFieldsAnnotatedWith(SessionVariable.class, clazz, new Visitor<SessionVariable, Field, SessionVariableInjection>() { @Override public SessionVariableInjection visit(SessionVariable annotation, Field field) { return new SessionVariableInjection(annotation.name(), annotation.optional(), field); } }); }
/** * Invokes the given method on the passed target. * * @param method * Method to invoke per reflection. * @param target * Object on which to invoke the method. * @return The result object. * @throws InvocationTargetException * If invocation target is invalid. * @throws IllegalArgumentException * If arguments are invalid. * @throws IllegalAccessException * If method cannot be accessed. */ public static Object invokeMethod(Method method, Object target) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { return invokeMethod(method, target, new Object[0]); }
/** * 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; } }
@Test public void unavailableAnnotation() { Rule ruleAnnotation = ReflectionUtils.findAnnotation(NoDefaultConstructorClass.class, Rule.class); assertThat(ruleAnnotation, equalTo(null)); } }
@Test(expectedExceptions = { RuntimeException.class }) public void noDefaultConstructor() { ReflectionUtils.tryInstantiate(NoDefaultConstructorClass.class); } }
@Test public void noDefaultConstructor() { boolean constructor = ReflectionUtils.hasNoArgsConstructor(NoDefaultConstructorClass.class); assertThat(constructor, is(false)); }
@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 = { NullPointerException.class }) public void visitorReturnsNull() throws RuleDefinitionException { ReflectionUtils.visitFieldsAnnotatedWith(TagValue.class, TestClass.class, new Visitor<TagValue, Field, String>() { @Override public String visit(TagValue annotation, Field field) { return field.getName().endsWith("2") ? null : "ok"; } }); }
@Test public void availableAnnotation() { Rule ruleAnnotation = ReflectionUtils.findAnnotation(TestClass.class, Rule.class); assertThat(ruleAnnotation, not(equalTo(null))); }
@Test public void validClass() { TestClass instance = ReflectionUtils.tryInstantiate(TestClass.class); assertThat(instance.par1, is(1)); }
@Test public void privateConstructor() { boolean constructor = ReflectionUtils.hasNoArgsConstructor(PrivateConstructorClass.class); assertThat(constructor, is(false)); } }
@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(expectedExceptions = { RuleDefinitionException.class }) public void explicitVisitorException() throws RuleDefinitionException { ReflectionUtils.visitFieldsAnnotatedWith(TagValue.class, TestClass.class, new Visitor<TagValue, Field, String>() { @Override public String visit(TagValue annotation, Field field) throws RuleDefinitionException { if (field.getName().endsWith("2")) { throw new RuleDefinitionException("test exception"); } else { return "ok"; } } }); } }
@Test public void availableAnnotationThroughSuperclass() { Rule ruleAnnotation = ReflectionUtils.findAnnotation(TestClass2.class, Rule.class); assertThat(ruleAnnotation, not(equalTo(null))); }
@Override public Session<I, R> makeObject() throws Exception { return new Session<>(ruleDefinitions, configuration.getResultCollector(), ReflectionUtils.tryInstantiate(configuration.getStorageClass())); }
@Test public void defaultConstructor() { boolean constructor = ReflectionUtils.hasNoArgsConstructor(TestClass.class); assertThat(constructor, is(true)); }
/** * 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 public void collectAllFields() throws RuleDefinitionException { List<String> fieldNames = ReflectionUtils.visitFieldsAnnotatedWith(TagValue.class, TestClass.class, new Visitor<TagValue, Field, String>() { @Override public String visit(TagValue annotation, Field field) { return field.getName(); } }); assertThat(fieldNames, containsInAnyOrder("par1", "par2", "par3", "par4")); }
@Test public void availableAnnotationThroughInterface() { Rule ruleAnnotation = ReflectionUtils.findAnnotation(TestClass3.class, Rule.class); assertThat(ruleAnnotation, not(equalTo(null))); }
@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")); }