T annotation = findAnnotation(interfaze, annotationClass); if (null != annotation) { return annotation; return findAnnotation(superClazz, annotationClass);
@Test(expectedExceptions = { RuntimeException.class }) public void noDefaultConstructor() { ReflectionUtils.tryInstantiate(NoDefaultConstructorClass.class); } }
/** * 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); } }); }
Rule annotation = ReflectionUtils.findAnnotation(clazz, Rule.class); if (annotation == null) { throw new RuleDefinitionException(clazz.getName() + " must be annotated with @Rule annotation."); if (!ReflectionUtils.hasNoArgsConstructor(clazz)) { throw new RuleDefinitionException(clazz.getName() + " must define an empty default constructor.");
/** * 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 noDefaultConstructor() { boolean constructor = ReflectionUtils.hasNoArgsConstructor(NoDefaultConstructorClass.class); assertThat(constructor, is(false)); }
/** * 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 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 privateConstructor() { boolean constructor = ReflectionUtils.hasNoArgsConstructor(PrivateConstructorClass.class); assertThat(constructor, is(false)); } }
/** * Executes the action. Throws: RuleExecutionException in case of any error * * @param context * The current executing {@link ExecutionContext} * @return A collection of {@link Tags}s * @throws RuleExecutionException * If rule execution fails with an exception. * @see ExecutionContext * @see Tag */ public Collection<Tag> execute(ExecutionContext context) throws RuleExecutionException { try { Object result = ReflectionUtils.invokeMethod(getMethod(), context.getInstance()); return transform(result, context); } catch (Exception e) { throw new RuleExecutionException("Failed to invoke action method (" + getMethod().getName() + ")", context, e); } }
@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(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 public void unavailableAnnotation() { Rule ruleAnnotation = ReflectionUtils.findAnnotation(NoDefaultConstructorClass.class, Rule.class); assertThat(ruleAnnotation, equalTo(null)); } }
@Test public void validClass() { TestClass instance = ReflectionUtils.tryInstantiate(TestClass.class); assertThat(instance.par1, is(1)); }
@Test public void defaultConstructor() { boolean constructor = ReflectionUtils.hasNoArgsConstructor(TestClass.class); assertThat(constructor, is(true)); }
/** * Executes this {@link ConditionMethod}. If the #method does not succeed a * {@link ConditionFailure} is returned. Otherwise null is returned. * * @param context * The current executing {@link ExecutionContext} * @return A {@link ConditionFailure} if condition fails, null otherwise * @throws RuleExecutionException * If execution of the condition method fails with an exception. * @see ExecutionContext * @see ConditionFailure */ public ConditionFailure execute(ExecutionContext context) throws RuleExecutionException { try { boolean valid = (boolean) ReflectionUtils.invokeMethod(getMethod(), context.getInstance()); if (!valid) { // Store information about the failed condition for later usage return new ConditionFailure(getName(), getHint()); } return null; } catch (Exception e) { throw new RuleExecutionException("Invocation of condition method failed.", context, e); } }
/** * Extracts the {@link TagInjection}s from the given class by processing the * {@link TagInjection} annotations. * * @param clazz * The class to be analyzed. * @return List of TagInjection * @throws RuleDefinitionException * If {@link TagInjection} annotations are invalid. Contract is that a class must * defined at least one field annotated with {@link TagValue}. Otherwise the rule * could never fire, because no input could be determined. */ public static List<TagInjection> describeTagInjection(Class<?> clazz) throws RuleDefinitionException { List<TagInjection> tagInjections = ReflectionUtils.visitFieldsAnnotatedWith(TagValue.class, clazz, new Visitor<TagValue, Field, TagInjection>() { @Override public TagInjection visit(TagValue annotation, Field field) { return new TagInjection(annotation.type(), field, annotation.injectionStrategy()); } }); if (CollectionUtils.isEmpty(tagInjections)) { throw new RuleDefinitionException(clazz.getName() + " must annotate at least one field with @Value. Otherwise the " + "rule will never fire and is useless."); } else { return tagInjections; } }
@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 availableAnnotation() { Rule ruleAnnotation = ReflectionUtils.findAnnotation(TestClass.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())); }