Search in sources :

Example 1 with RuleSourceValidationProblemCollector

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)));
}
Also used : Mutate(org.gradle.model.Mutate) JavaMethod(org.gradle.internal.reflect.JavaMethod) Method(java.lang.reflect.Method) DefaultRuleSourceValidationProblemCollector(org.gradle.model.internal.inspect.DefaultRuleSourceValidationProblemCollector) RuleSourceValidationProblemCollector(org.gradle.model.internal.inspect.RuleSourceValidationProblemCollector) Type(java.lang.reflect.Type) ModelType(org.gradle.model.internal.type.ModelType) FormattingValidationProblemCollector(org.gradle.model.internal.inspect.FormattingValidationProblemCollector) DefaultRuleSourceValidationProblemCollector(org.gradle.model.internal.inspect.DefaultRuleSourceValidationProblemCollector)

Aggregations

Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 JavaMethod (org.gradle.internal.reflect.JavaMethod)1 Mutate (org.gradle.model.Mutate)1 DefaultRuleSourceValidationProblemCollector (org.gradle.model.internal.inspect.DefaultRuleSourceValidationProblemCollector)1 FormattingValidationProblemCollector (org.gradle.model.internal.inspect.FormattingValidationProblemCollector)1 RuleSourceValidationProblemCollector (org.gradle.model.internal.inspect.RuleSourceValidationProblemCollector)1 ModelType (org.gradle.model.internal.type.ModelType)1