/** * 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); } }); }
@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"; } }); }
/** * 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.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 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")); }
results.addAll(visitFieldsAnnotatedWith(annotation, clazz.getSuperclass(), visitor));