Search in sources :

Example 1 with Severity

use of org.gradle.internal.reflect.validation.Severity in project gradle by gradle.

the class DefaultTransformationRegistrationFactory method create.

@Override
public ArtifactTransformRegistration create(ImmutableAttributes from, ImmutableAttributes to, Class<? extends TransformAction<?>> implementation, @Nullable TransformParameters parameterObject) {
    TypeMetadata actionMetadata = actionMetadataStore.getTypeMetadata(implementation);
    boolean cacheable = implementation.isAnnotationPresent(CacheableTransform.class);
    DefaultTypeValidationContext validationContext = DefaultTypeValidationContext.withoutRootType(documentationRegistry, cacheable);
    actionMetadata.visitValidationFailures(null, validationContext);
    // Should retain this on the metadata rather than calculate on each invocation
    Class<? extends FileNormalizer> inputArtifactNormalizer = null;
    Class<? extends FileNormalizer> dependenciesNormalizer = null;
    DirectorySensitivity artifactDirectorySensitivity = DirectorySensitivity.DEFAULT;
    DirectorySensitivity dependenciesDirectorySensitivity = DirectorySensitivity.DEFAULT;
    LineEndingSensitivity artifactLineEndingSensitivity = LineEndingSensitivity.DEFAULT;
    LineEndingSensitivity dependenciesLineEndingSensitivity = LineEndingSensitivity.DEFAULT;
    for (PropertyMetadata propertyMetadata : actionMetadata.getPropertiesMetadata()) {
        Class<? extends Annotation> propertyType = propertyMetadata.getPropertyType();
        if (propertyType.equals(InputArtifact.class)) {
            // Should ask the annotation handler to figure this out instead
            NormalizerCollectingVisitor visitor = new NormalizerCollectingVisitor();
            actionMetadata.getAnnotationHandlerFor(propertyMetadata).visitPropertyValue(propertyMetadata.getPropertyName(), null, propertyMetadata, visitor, null);
            inputArtifactNormalizer = visitor.normalizer;
            artifactDirectorySensitivity = visitor.directorySensitivity;
            artifactLineEndingSensitivity = visitor.lineEndingSensitivity;
            DefaultTransformer.validateInputFileNormalizer(propertyMetadata.getPropertyName(), inputArtifactNormalizer, cacheable, validationContext);
        } else if (propertyType.equals(InputArtifactDependencies.class)) {
            NormalizerCollectingVisitor visitor = new NormalizerCollectingVisitor();
            actionMetadata.getAnnotationHandlerFor(propertyMetadata).visitPropertyValue(propertyMetadata.getPropertyName(), null, propertyMetadata, visitor, null);
            dependenciesNormalizer = visitor.normalizer;
            dependenciesDirectorySensitivity = visitor.directorySensitivity;
            dependenciesLineEndingSensitivity = visitor.lineEndingSensitivity;
            DefaultTransformer.validateInputFileNormalizer(propertyMetadata.getPropertyName(), dependenciesNormalizer, cacheable, validationContext);
        }
    }
    ImmutableMap<String, Severity> validationMessages = validationContext.getProblems();
    if (!validationMessages.isEmpty()) {
        String formatString = validationMessages.size() == 1 ? "A problem was found with the configuration of %s." : "Some problems were found with the configuration of %s.";
        throw new DefaultMultiCauseException(String.format(formatString, ModelType.of(implementation).getDisplayName()), validationMessages.keySet().stream().sorted().map(InvalidUserDataException::new).collect(Collectors.toList()));
    }
    Transformer transformer = new DefaultTransformer(implementation, parameterObject, from, FileParameterUtils.normalizerOrDefault(inputArtifactNormalizer), FileParameterUtils.normalizerOrDefault(dependenciesNormalizer), cacheable, artifactDirectorySensitivity, dependenciesDirectorySensitivity, artifactLineEndingSensitivity, dependenciesLineEndingSensitivity, buildOperationExecutor, classLoaderHierarchyHasher, isolatableFactory, fileCollectionFactory, fileLookup, parametersPropertyWalker, actionInstantiationScheme, owner, calculatedValueContainerFactory, internalServices, documentationRegistry);
    return new DefaultArtifactTransformRegistration(from, to, new TransformationStep(transformer, transformerInvocationFactory, owner, inputFingerprinter));
}
Also used : LineEndingSensitivity(org.gradle.internal.fingerprint.LineEndingSensitivity) TypeMetadata(org.gradle.api.internal.tasks.properties.TypeMetadata) InputArtifactDependencies(org.gradle.api.artifacts.transform.InputArtifactDependencies) DirectorySensitivity(org.gradle.internal.fingerprint.DirectorySensitivity) Severity(org.gradle.internal.reflect.validation.Severity) DefaultMultiCauseException(org.gradle.internal.exceptions.DefaultMultiCauseException) InvalidUserDataException(org.gradle.api.InvalidUserDataException) DefaultTypeValidationContext(org.gradle.internal.reflect.DefaultTypeValidationContext) PropertyMetadata(org.gradle.internal.reflect.PropertyMetadata)

Example 2 with Severity

use of org.gradle.internal.reflect.validation.Severity in project gradle by gradle.

the class DefaultTransformer method fingerprintParameters.

private static void fingerprintParameters(DocumentationRegistry documentationRegistry, InputFingerprinter inputFingerprinter, FileCollectionFactory fileCollectionFactory, PropertyWalker propertyWalker, Hasher hasher, Object parameterObject, boolean cacheable) {
    DefaultTypeValidationContext validationContext = DefaultTypeValidationContext.withoutRootType(documentationRegistry, cacheable);
    InputFingerprinter.Result result = inputFingerprinter.fingerprintInputProperties(ImmutableSortedMap.of(), ImmutableSortedMap.of(), ImmutableSortedMap.of(), ImmutableSortedMap.of(), visitor -> propertyWalker.visitProperties(parameterObject, validationContext, new PropertyVisitor.Adapter() {

        @Override
        public void visitInputProperty(String propertyName, PropertyValue value, boolean optional) {
            try {
                Object preparedValue = InputParameterUtils.prepareInputParameterValue(value);
                if (preparedValue == null && !optional) {
                    reportValueNotSet(propertyName, validationContext);
                }
                visitor.visitInputProperty(propertyName, () -> preparedValue);
            } catch (Throwable e) {
                throw new InvalidUserDataException(String.format("Error while evaluating property '%s' of %s", propertyName, getParameterObjectDisplayName(parameterObject)), e);
            }
        }

        @Override
        public void visitInputFileProperty(String propertyName, boolean optional, boolean skipWhenEmpty, DirectorySensitivity directorySensitivity, LineEndingSensitivity lineEndingNormalization, boolean incremental, @Nullable Class<? extends FileNormalizer> fileNormalizer, PropertyValue value, InputFilePropertyType filePropertyType) {
            validateInputFileNormalizer(propertyName, fileNormalizer, cacheable, validationContext);
            visitor.visitInputFileProperty(propertyName, incremental ? InputFingerprinter.InputPropertyType.INCREMENTAL : InputFingerprinter.InputPropertyType.NON_INCREMENTAL, new FileValueSupplier(value, fileNormalizer == null ? AbsolutePathInputNormalizer.class : fileNormalizer, directorySensitivity, lineEndingNormalization, () -> FileParameterUtils.resolveInputFileValue(fileCollectionFactory, filePropertyType, value)));
        }

        @Override
        public void visitOutputFileProperty(String propertyName, boolean optional, PropertyValue value, OutputFilePropertyType filePropertyType) {
            validationContext.visitPropertyProblem(problem -> problem.withId(ValidationProblemId.ARTIFACT_TRANSFORM_SHOULD_NOT_DECLARE_OUTPUT).reportAs(Severity.ERROR).forProperty(propertyName).withDescription("declares an output").happensBecause("is annotated with an output annotation").addPossibleSolution("Remove the output property and use the TransformOutputs parameter from transform(TransformOutputs) instead").documentedAt("validation_problems", "artifact_transform_should_not_declare_output"));
        }
    }));
    ImmutableMap<String, Severity> validationMessages = validationContext.getProblems();
    if (!validationMessages.isEmpty()) {
        throw new DefaultMultiCauseException(String.format(validationMessages.size() == 1 ? "A problem was found with the configuration of the artifact transform parameter %s." : "Some problems were found with the configuration of the artifact transform parameter %s.", getParameterObjectDisplayName(parameterObject)), validationMessages.keySet().stream().sorted().map(InvalidUserDataException::new).collect(Collectors.toList()));
    }
    for (Map.Entry<String, ValueSnapshot> entry : result.getValueSnapshots().entrySet()) {
        hasher.putString(entry.getKey());
        entry.getValue().appendToHasher(hasher);
    }
    for (Map.Entry<String, CurrentFileCollectionFingerprint> entry : result.getFileFingerprints().entrySet()) {
        hasher.putString(entry.getKey());
        hasher.putHash(entry.getValue().getHash());
    }
}
Also used : LineEndingSensitivity(org.gradle.internal.fingerprint.LineEndingSensitivity) ValueSnapshot(org.gradle.internal.snapshot.ValueSnapshot) FileValueSupplier(org.gradle.internal.execution.fingerprint.InputFingerprinter.FileValueSupplier) FileNormalizer(org.gradle.api.tasks.FileNormalizer) OutputFilePropertyType(org.gradle.api.internal.tasks.properties.OutputFilePropertyType) DefaultTypeValidationContext(org.gradle.internal.reflect.DefaultTypeValidationContext) DirectorySensitivity(org.gradle.internal.fingerprint.DirectorySensitivity) PropertyValue(org.gradle.api.internal.tasks.properties.PropertyValue) InputFilePropertyType(org.gradle.api.internal.tasks.properties.InputFilePropertyType) Severity(org.gradle.internal.reflect.validation.Severity) DefaultMultiCauseException(org.gradle.internal.exceptions.DefaultMultiCauseException) InputFingerprinter(org.gradle.internal.execution.fingerprint.InputFingerprinter) InvalidUserDataException(org.gradle.api.InvalidUserDataException) CurrentFileCollectionFingerprint(org.gradle.internal.fingerprint.CurrentFileCollectionFingerprint) DslObject(org.gradle.api.internal.plugins.DslObject) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Nullable(javax.annotation.Nullable)

Example 3 with Severity

use of org.gradle.internal.reflect.validation.Severity in project gradle by gradle.

the class ValidateStep method execute.

@Override
public R execute(UnitOfWork work, C context) {
    WorkValidationContext validationContext = context.getValidationContext();
    work.validate(validationContext);
    context.getBeforeExecutionState().ifPresent(beforeExecutionState -> validateImplementations(work, beforeExecutionState, validationContext));
    Map<Severity, List<String>> problems = validationContext.getProblems().stream().collect(groupingBy(BaseProblem::getSeverity, mapping(ValidateStep::renderedMessage, toList())));
    ImmutableCollection<String> warnings = ImmutableList.copyOf(problems.getOrDefault(Severity.WARNING, ImmutableList.of()));
    ImmutableCollection<String> errors = ImmutableList.copyOf(problems.getOrDefault(Severity.ERROR, ImmutableList.of()));
    if (!warnings.isEmpty()) {
        warningReporter.recordValidationWarnings(work, warnings);
    }
    if (!errors.isEmpty()) {
        int maxErrCount = Integer.getInteger(MAX_NB_OF_ERRORS, 5);
        ImmutableSortedSet<String> uniqueSortedErrors = ImmutableSortedSet.copyOf(errors);
        throw WorkValidationException.forProblems(uniqueSortedErrors).limitTo(maxErrCount).withSummary(helper -> String.format("%s found with the configuration of %s (%s).", helper.size() == 1 ? "A problem was" : "Some problems were", work.getDisplayName(), describeTypesChecked(validationContext.getValidatedTypes()))).get();
    }
    if (!warnings.isEmpty()) {
        LOGGER.info("Invalidating VFS because {} failed validation", work.getDisplayName());
        virtualFileSystem.invalidateAll();
    }
    return delegate.execute(work, new ValidationFinishedContext() {

        @Override
        public Optional<BeforeExecutionState> getBeforeExecutionState() {
            return context.getBeforeExecutionState();
        }

        @Override
        public Optional<ValidationResult> getValidationProblems() {
            return warnings.isEmpty() ? Optional.empty() : Optional.of(() -> warnings);
        }

        @Override
        public Optional<PreviousExecutionState> getPreviousExecutionState() {
            return context.getPreviousExecutionState();
        }

        @Override
        public File getWorkspace() {
            return context.getWorkspace();
        }

        @Override
        public Optional<ExecutionHistoryStore> getHistory() {
            return context.getHistory();
        }

        @Override
        public ImmutableSortedMap<String, ValueSnapshot> getInputProperties() {
            return context.getInputProperties();
        }

        @Override
        public ImmutableSortedMap<String, CurrentFileCollectionFingerprint> getInputFileProperties() {
            return context.getInputFileProperties();
        }

        @Override
        public UnitOfWork.Identity getIdentity() {
            return context.getIdentity();
        }

        @Override
        public Optional<String> getNonIncrementalReason() {
            return context.getNonIncrementalReason();
        }

        @Override
        public WorkValidationContext getValidationContext() {
            return context.getValidationContext();
        }
    });
}
Also used : BeforeExecutionState(org.gradle.internal.execution.history.BeforeExecutionState) TypeValidationProblemRenderer.convertToSingleLine(org.gradle.internal.reflect.validation.TypeValidationProblemRenderer.convertToSingleLine) CurrentFileCollectionFingerprint(org.gradle.internal.fingerprint.CurrentFileCollectionFingerprint) LoggerFactory(org.slf4j.LoggerFactory) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) ImmutableCollection(com.google.common.collect.ImmutableCollection) Severity(org.gradle.internal.reflect.validation.Severity) PreviousExecutionState(org.gradle.internal.execution.history.PreviousExecutionState) WorkValidationContext(org.gradle.internal.execution.WorkValidationContext) ImmutableList(com.google.common.collect.ImmutableList) ExecutionHistoryStore(org.gradle.internal.execution.history.ExecutionHistoryStore) ModelType(org.gradle.model.internal.type.ModelType) Map(java.util.Map) Collectors.mapping(java.util.stream.Collectors.mapping) BaseProblem(org.gradle.problems.BaseProblem) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) UnitOfWork(org.gradle.internal.execution.UnitOfWork) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) ValidationProblemBuilder(org.gradle.internal.reflect.validation.ValidationProblemBuilder) GeneratedSubclasses(org.gradle.api.internal.GeneratedSubclasses) TypeValidationProblemRenderer.renderMinimalInformationAbout(org.gradle.internal.reflect.validation.TypeValidationProblemRenderer.renderMinimalInformationAbout) WorkValidationException(org.gradle.internal.execution.WorkValidationException) Logger(org.slf4j.Logger) ImplementationSnapshot(org.gradle.internal.snapshot.impl.ImplementationSnapshot) Collection(java.util.Collection) ValueSnapshot(org.gradle.internal.snapshot.ValueSnapshot) TypeValidationContext(org.gradle.internal.reflect.validation.TypeValidationContext) MutableReference(org.gradle.internal.MutableReference) Collectors(java.util.stream.Collectors) File(java.io.File) VirtualFileSystem(org.gradle.internal.vfs.VirtualFileSystem) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Optional(java.util.Optional) ValidationProblemId(org.gradle.internal.reflect.problems.ValidationProblemId) Optional(java.util.Optional) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Severity(org.gradle.internal.reflect.validation.Severity) CurrentFileCollectionFingerprint(org.gradle.internal.fingerprint.CurrentFileCollectionFingerprint) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) File(java.io.File) WorkValidationContext(org.gradle.internal.execution.WorkValidationContext)

Aggregations

Severity (org.gradle.internal.reflect.validation.Severity)3 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)2 Map (java.util.Map)2 InvalidUserDataException (org.gradle.api.InvalidUserDataException)2 DefaultMultiCauseException (org.gradle.internal.exceptions.DefaultMultiCauseException)2 CurrentFileCollectionFingerprint (org.gradle.internal.fingerprint.CurrentFileCollectionFingerprint)2 DirectorySensitivity (org.gradle.internal.fingerprint.DirectorySensitivity)2 LineEndingSensitivity (org.gradle.internal.fingerprint.LineEndingSensitivity)2 DefaultTypeValidationContext (org.gradle.internal.reflect.DefaultTypeValidationContext)2 ValueSnapshot (org.gradle.internal.snapshot.ValueSnapshot)2 ImmutableCollection (com.google.common.collect.ImmutableCollection)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 File (java.io.File)1 Collection (java.util.Collection)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Collectors.groupingBy (java.util.stream.Collectors.groupingBy)1