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 2019 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.annotations.VisibleForTesting;
import com.google.common.base.Equivalence;
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.artifacts.transform.CacheableTransform;
import org.gradle.api.artifacts.transform.TransformAction;
import org.gradle.api.internal.TaskInternal;
import org.gradle.api.internal.project.taskfactory.TaskClassInfoStore;
import org.gradle.api.provider.Provider;
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.cache.internal.DefaultCrossBuildInMemoryCacheFactory;
import org.gradle.internal.Cast;
import org.gradle.internal.event.DefaultListenerManager;
import org.gradle.internal.instantiation.DefaultInstantiatorFactory;
import org.gradle.internal.reflect.ParameterValidationContext;
import org.gradle.internal.reflect.PropertyMetadata;
import org.gradle.internal.service.DefaultServiceLocator;
import org.gradle.internal.service.ServiceRegistration;
import org.gradle.internal.service.ServiceRegistry;
import org.gradle.internal.service.ServiceRegistryBuilder;
import org.gradle.internal.service.scopes.PluginServiceRegistry;
import javax.annotation.Nullable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import static org.gradle.api.internal.tasks.properties.ModifierAnnotationCategory.NORMALIZATION;
/**
* Class for easy access to property validation from the validator task.
*/
@NonNullApi
public class PropertyValidationAccess {
private final static Map, ? extends PropertyValidator> PROPERTY_VALIDATORS = ImmutableMap.of(
InputFiles.class, new MissingNormalizationValidator(false),
InputFile.class, new MissingNormalizationValidator(false),
InputDirectory.class, new MissingNormalizationValidator(false)
);
private final static Map, ? extends PropertyValidator> STRICT_PROPERTY_VALIDATORS = ImmutableMap.of(
InputFiles.class, new MissingNormalizationValidator(true),
InputFile.class, new MissingNormalizationValidator(true),
InputDirectory.class, new MissingNormalizationValidator(true)
);
private static final PropertyValidationAccess INSTANCE = new PropertyValidationAccess();
private final TaskClassInfoStore taskClassInfoStore;
private final List typeSchemes;
@VisibleForTesting
PropertyValidationAccess() {
ServiceRegistryBuilder builder = ServiceRegistryBuilder.builder().displayName("Global services");
// Should reuse `GlobalScopeServices` here, however this requires a bunch of stuff in order to discover the plugin service registries
// For now, re-implement the discovery here
builder.provider(new Object() {
void configure(ServiceRegistration registration) {
registration.add(DefaultListenerManager.class, new DefaultListenerManager());
registration.add(DefaultCrossBuildInMemoryCacheFactory.class);
registration.add(DefaultInstantiatorFactory.class);
List pluginServiceFactories = new DefaultServiceLocator(false, getClass().getClassLoader()).getAll(PluginServiceRegistry.class);
for (PluginServiceRegistry pluginServiceFactory : pluginServiceFactories) {
pluginServiceFactory.registerGlobalServices(registration);
}
}
});
ServiceRegistry services = builder.build();
taskClassInfoStore = services.get(TaskClassInfoStore.class);
typeSchemes = services.getAll(TypeScheme.class);
}
@SuppressWarnings("unused")
public static void collectTaskValidationProblems(Class> topLevelBean, Map problems, boolean enableStricterValidation) {
INSTANCE.collectTypeValidationProblems(topLevelBean, problems, enableStricterValidation);
}
@SuppressWarnings("unused")
public static void collectValidationProblems(Class> topLevelBean, Map problems, boolean enableStricterValidation) {
INSTANCE.collectTypeValidationProblems(topLevelBean, problems, enableStricterValidation);
}
private void collectTypeValidationProblems(Class> topLevelBean, Map problems, boolean enableStricterValidation) {
// Skip this for now
if (topLevelBean.equals(TaskInternal.class)) {
return;
}
TypeMetadataStore metadataStore = null;
for (TypeScheme typeScheme : typeSchemes) {
if (typeScheme.appliesTo(topLevelBean)) {
metadataStore = typeScheme.getMetadataStore();
break;
}
}
if (metadataStore == null) {
// Don't know about this type
return;
}
boolean cacheable;
boolean mapErrorsToWarnings;
if (Task.class.isAssignableFrom(topLevelBean)) {
cacheable = taskClassInfoStore.getTaskClassInfo(Cast.>uncheckedNonnullCast(topLevelBean)).isCacheable();
// Treat all errors as warnings, for backwards compatibility
mapErrorsToWarnings = true;
} else if (TransformAction.class.isAssignableFrom(topLevelBean)) {
cacheable = topLevelBean.isAnnotationPresent(CacheableTransform.class);
mapErrorsToWarnings = false;
} else {
cacheable = false;
mapErrorsToWarnings = false;
}
Queue> queue = new ArrayDeque>();
BeanTypeNodeFactory nodeFactory = new BeanTypeNodeFactory(metadataStore);
queue.add(nodeFactory.createRootNode(TypeToken.of(topLevelBean)));
boolean stricterValidation = enableStricterValidation || cacheable;
while (!queue.isEmpty()) {
BeanTypeNode> node = queue.remove();
node.visit(topLevelBean, stricterValidation, new ProblemCollector(problems, mapErrorsToWarnings), queue, nodeFactory);
}
}
private static class ProblemCollector {
final Map problems;
private final boolean mapErrorsToWarnings;
public ProblemCollector(Map problems, boolean mapErrorsToWarnings) {
this.problems = problems;
this.mapErrorsToWarnings = mapErrorsToWarnings;
}
void error(String message, boolean strict) {
problems.put(message, strict || !mapErrorsToWarnings);
}
}
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.>>uncheckedNonnullCast(beanType), typeMetadata);
}
if (Iterable.class.isAssignableFrom(rawType)) {
return new IterableBeanTypeNode(parentNode, propertyName, Cast.>>uncheckedNonnullCast(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, ProblemCollector 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