use of org.gradle.model.internal.inspect.RuleSourceValidationProblemCollector in project gradle by gradle.
the class RuleSourceBackedRuleAction method create.
public static <R, T> RuleSourceBackedRuleAction<R, T> create(ModelType<T> subjectType, R ruleSourceInstance) {
ModelType<R> ruleSourceType = ModelType.typeOf(ruleSourceInstance);
List<Method> mutateMethods = JavaReflectionUtil.findAllMethods(ruleSourceType.getConcreteClass(), new Spec<Method>() {
public boolean isSatisfiedBy(Method element) {
return element.isAnnotationPresent(Mutate.class);
}
});
FormattingValidationProblemCollector problemsFormatter = new FormattingValidationProblemCollector("rule source", ruleSourceType);
RuleSourceValidationProblemCollector problems = new DefaultRuleSourceValidationProblemCollector(problemsFormatter);
if (mutateMethods.size() == 0) {
problems.add("Must have at exactly one method annotated with @" + Mutate.class.getName());
} else {
if (mutateMethods.size() > 1) {
problems.add("More than one method is annotated with @" + Mutate.class.getName());
}
for (Method ruleMethod : mutateMethods) {
if (ruleMethod.getReturnType() != Void.TYPE) {
problems.add(ruleMethod, "A rule method must return void");
}
Type[] parameterTypes = ruleMethod.getGenericParameterTypes();
if (parameterTypes.length == 0 || !subjectType.isAssignableFrom(ModelType.of(parameterTypes[0]))) {
problems.add(ruleMethod, String.format("First parameter of a rule method must be of type %s", subjectType));
}
}
}
if (problemsFormatter.hasProblems()) {
throw new RuleActionValidationException(problemsFormatter.format());
}
return new RuleSourceBackedRuleAction<R, T>(ruleSourceInstance, new JavaMethod<R, T>(subjectType.getConcreteClass(), mutateMethods.get(0)));
}
Aggregations