org.gradle.api.internal.tasks.properties.PropertyValidationAccess 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 2017 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.api.internal.tasks.properties;
import com.google.common.base.Equivalence;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.reflect.TypeToken;
import org.gradle.api.Named;
import org.gradle.api.NonNullApi;
import org.gradle.api.Task;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore;
import org.gradle.api.internal.tasks.properties.annotations.ClasspathPropertyAnnotationHandler;
import org.gradle.api.internal.tasks.properties.annotations.CompileClasspathPropertyAnnotationHandler;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.cache.internal.DefaultCrossBuildInMemoryCacheFactory;
import org.gradle.internal.Cast;
import org.gradle.internal.event.DefaultListenerManager;
import org.gradle.internal.reflect.PropertyMetadata;
import javax.annotation.Nullable;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayDeque;
import java.util.Map;
import java.util.Queue;
/**
* Class for easy access to task property validation from the validator task.
*/
@NonNullApi
public class PropertyValidationAccess {
private final static Map, PropertyValidator> PROPERTY_VALIDATORS = ImmutableMap.of(
Input.class, new InputOnFileTypeValidator(),
InputFiles.class, new MissingPathSensitivityValidator(),
InputFile.class, new MissingPathSensitivityValidator(),
InputDirectory.class, new MissingPathSensitivityValidator()
);
@SuppressWarnings("unused")
public static void collectTaskValidationProblems(Class> topLevelBean, Map problems, boolean enableStricterValidation) {
DefaultCrossBuildInMemoryCacheFactory cacheFactory = new DefaultCrossBuildInMemoryCacheFactory(new DefaultListenerManager());
DefaultTaskClassInfoStore taskClassInfoStore = new DefaultTaskClassInfoStore(cacheFactory);
TypeMetadataStore metadataStore = new DefaultTypeMetadataStore(ImmutableList.of(
new ClasspathPropertyAnnotationHandler(), new CompileClasspathPropertyAnnotationHandler()
), cacheFactory);
Queue> queue = new ArrayDeque>();
BeanTypeNodeFactory nodeFactory = new BeanTypeNodeFactory(metadataStore);
queue.add(nodeFactory.createRootNode(TypeToken.of(topLevelBean)));
boolean cacheable = taskClassInfoStore.getTaskClassInfo(Cast.>uncheckedCast(topLevelBean)).isCacheable();
boolean stricterValidation = enableStricterValidation || cacheable;
while (!queue.isEmpty()) {
BeanTypeNode> node = queue.remove();
node.visit(topLevelBean, stricterValidation, problems, queue, nodeFactory);
}
}
private static class BeanTypeNodeFactory {
private final TypeMetadataStore metadataStore;
public BeanTypeNodeFactory(TypeMetadataStore metadataStore) {
this.metadataStore = metadataStore;
}
public BeanTypeNode> createRootNode(TypeToken> beanType) {
return new NestedBeanTypeNode(null, null, beanType, metadataStore.getTypeMetadata(beanType.getRawType()));
}
public void createAndAddToQueue(BeanTypeNode> parentNode, String propertyName, TypeToken> beanType, Queue> queue) {
if (!parentNode.nodeCreatesCycle(beanType)) {
queue.add(createChild(parentNode, propertyName, beanType));
}
}
private BeanTypeNode> createChild(BeanTypeNode> parentNode, String propertyName, TypeToken> beanType) {
Class> rawType = beanType.getRawType();
TypeMetadata typeMetadata = metadataStore.getTypeMetadata(rawType);
if (!typeMetadata.hasAnnotatedProperties()) {
if (Map.class.isAssignableFrom(rawType)) {
return new MapBeanTypeNode(parentNode, propertyName, Cast.>>uncheckedCast(beanType), typeMetadata);
}
if (Iterable.class.isAssignableFrom(rawType)) {
return new IterableBeanTypeNode(parentNode, propertyName, Cast.>>uncheckedCast(beanType), typeMetadata);
}
}
return new NestedBeanTypeNode(parentNode, propertyName, beanType, typeMetadata);
}
}
private abstract static class BeanTypeNode extends AbstractPropertyNode> {
protected BeanTypeNode(@Nullable BeanTypeNode> parentNode, @Nullable String propertyName, TypeToken extends T> beanType, TypeMetadata typeMetadata) {
super(parentNode, propertyName, typeMetadata);
this.beanType = beanType;
}
public abstract void visit(Class> topLevelBean, boolean stricterValidation, Map problems, Queue> queue, BeanTypeNodeFactory nodeFactory);
public boolean nodeCreatesCycle(TypeToken> childType) {
return findNodeCreatingCycle(childType, Equivalence.equals()) != null;
}
private final TypeToken extends T> beanType;
protected TypeToken> extractNestedType(Class super T> parameterizedSuperClass, int typeParameterIndex) {
return PropertyValidationAccess.extractNestedType(beanType, parameterizedSuperClass, typeParameterIndex);
}
@Override
protected TypeToken> getNodeValue() {
return beanType;
}
}
private static class NestedBeanTypeNode extends BeanTypeNode
© 2015 - 2025 Weber Informatics LLC | Privacy Policy