org.gradle.internal.rules.DefaultRuleActionValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.internal.rules;
import org.gradle.model.internal.type.ModelType;
public class DefaultRuleActionValidator implements RuleActionValidator {
private static final String VALID_NO_TYPES = "Rule may not have an input parameter of type: %s.";
private static final String VALID_SINGLE_TYPES = "Rule may not have an input parameter of type: %s. Second parameter must be of type: %s.";
private final Class> validInputType;
public DefaultRuleActionValidator() {
this.validInputType = null;
}
public DefaultRuleActionValidator(Class> validInputType) {
this.validInputType = validInputType;
}
@Override
public RuleAction super T> validate(RuleAction super T> ruleAction) {
validateInputTypes(ruleAction);
return ruleAction;
}
private void validateInputTypes(RuleAction> ruleAction) {
for (Class> inputType : ruleAction.getInputTypes()) {
if (validInputType == null) {
throw new RuleActionValidationException(invalidParameterMessage(inputType));
} else if (!validInputType.equals(inputType)) {
throw new RuleActionValidationException(invalidParameterMessage(inputType));
}
}
}
private String invalidParameterMessage(Class> inputType) {
if (validInputType == null) {
return String.format(VALID_NO_TYPES, inputType.getName());
} else {
return String.format(VALID_SINGLE_TYPES, inputType.getName(), className(validInputType));
}
}
private static String className(Class> aClass) {
return ModelType.of(aClass).toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy