Search in sources :

Example 31 with TreeFormatter

use of org.gradle.internal.logging.text.TreeFormatter in project gradle by gradle.

the class InjectUtil method selectConstructor.

/**
 * Selects the single injectable constructor for the given type.
 * The type must either have only one public or package-private default constructor,
 * or it should have a single constructor annotated with {@literal @}{@link Inject}.
 *
 * @param type the type to find the injectable constructor of.
 * @param reportAs errors are reported against this type, useful when the real type is generated, and we'd like to show the original type in messages instead.
 */
public static <T> ClassGenerator.GeneratedConstructor<T> selectConstructor(ClassGenerator.GeneratedClass<T> type, Class<?> reportAs) {
    List<ClassGenerator.GeneratedConstructor<T>> constructors = type.getConstructors();
    if (constructors.size() == 1) {
        ClassGenerator.GeneratedConstructor<T> constructor = constructors.get(0);
        if (constructor.getParameterTypes().length == 0 && isPublicOrPackageScoped(type.getGeneratedClass(), constructor)) {
            return constructor;
        }
        if (constructor.getAnnotation(Inject.class) != null) {
            return constructor;
        }
        if (constructor.getParameterTypes().length == 0) {
            TreeFormatter formatter = new TreeFormatter();
            formatter.node("The constructor for type ");
            formatter.appendType(reportAs);
            formatter.append(" should be public or package protected or annotated with @Inject.");
            throw new IllegalArgumentException(formatter.toString());
        } else {
            TreeFormatter formatter = new TreeFormatter();
            formatter.node("The constructor for type ");
            formatter.appendType(reportAs);
            formatter.append(" should be annotated with @Inject.");
            throw new IllegalArgumentException(formatter.toString());
        }
    }
    List<ClassGenerator.GeneratedConstructor<T>> injectConstructors = new ArrayList<ClassGenerator.GeneratedConstructor<T>>();
    for (ClassGenerator.GeneratedConstructor<T> constructor : constructors) {
        if (constructor.getAnnotation(Inject.class) != null) {
            injectConstructors.add(constructor);
        }
    }
    if (injectConstructors.isEmpty()) {
        TreeFormatter formatter = new TreeFormatter();
        formatter.node(reportAs);
        formatter.append(" has no constructor that is annotated with @Inject.");
        throw new IllegalArgumentException(formatter.toString());
    }
    if (injectConstructors.size() > 1) {
        TreeFormatter formatter = new TreeFormatter();
        formatter.node(reportAs);
        formatter.append(" has multiple constructors that are annotated with @Inject.");
        throw new IllegalArgumentException(formatter.toString());
    }
    return injectConstructors.get(0);
}
Also used : Inject(javax.inject.Inject) TreeFormatter(org.gradle.internal.logging.text.TreeFormatter) ArrayList(java.util.ArrayList)

Example 32 with TreeFormatter

use of org.gradle.internal.logging.text.TreeFormatter in project gradle by gradle.

the class IsolationScheme method parameterTypeFor.

/**
 * Determines the parameters type found at the given type argument index for the given implementation.
 *
 * @return The parameters type, or {@code null} when the implementation takes no parameters.
 */
@Nullable
public <T extends IMPLEMENTATION, P extends PARAMS> Class<P> parameterTypeFor(Class<T> implementationType, int typeArgumentIndex) {
    if (implementationType == interfaceType) {
        return null;
    }
    Class<P> parametersType = inferParameterType(implementationType, typeArgumentIndex);
    if (parametersType == paramsType) {
        TreeFormatter formatter = new TreeFormatter();
        formatter.node("Could not create the parameters for ");
        formatter.appendType(implementationType);
        formatter.append(": must use a sub-type of ");
        formatter.appendType(parametersType);
        formatter.append(" as the parameters type. Use ");
        formatter.appendType(noParamsType);
        formatter.append(" as the parameters type for implementations that do not take parameters.");
        throw new IllegalArgumentException(formatter.toString());
    }
    if (parametersType == noParamsType) {
        return null;
    }
    return parametersType;
}
Also used : TreeFormatter(org.gradle.internal.logging.text.TreeFormatter) Nullable(javax.annotation.Nullable)

Example 33 with TreeFormatter

use of org.gradle.internal.logging.text.TreeFormatter in project gradle by gradle.

the class DefaultValueSourceProviderFactory method couldNotCreateProviderOf.

private String couldNotCreateProviderOf(Class<?> valueSourceType) {
    TreeFormatter formatter = new TreeFormatter();
    formatter.node("Could not create provider for value source ");
    formatter.appendType(valueSourceType);
    formatter.append(".");
    return formatter.toString();
}
Also used : TreeFormatter(org.gradle.internal.logging.text.TreeFormatter)

Example 34 with TreeFormatter

use of org.gradle.internal.logging.text.TreeFormatter in project gradle by gradle.

the class UnavailablePlatformToolProvider method failure.

private RuntimeException failure() {
    TreeFormatter formatter = new TreeFormatter();
    this.explain(formatter);
    return new GradleException(formatter.toString());
}
Also used : TreeFormatter(org.gradle.internal.logging.text.TreeFormatter) GradleException(org.gradle.api.GradleException)

Example 35 with TreeFormatter

use of org.gradle.internal.logging.text.TreeFormatter in project gradle by gradle.

the class InvalidPublicationChecker method validate.

public void validate() {
    if (variants.isEmpty()) {
        failWith("This publication must publish at least one variant");
    }
    checkVariantDependencyVersions();
    if (errors != null) {
        TreeFormatter formatter = new TreeFormatter();
        formatter.node("Invalid publication '" + publicationName + "'");
        formatter.startChildren();
        for (String error : errors) {
            formatter.node(error);
        }
        formatter.endChildren();
        if (explanations != null) {
            for (String explanation : explanations) {
                formatter.node(explanation);
            }
        }
        throw new InvalidUserCodeException(formatter.toString());
    }
}
Also used : InvalidUserCodeException(org.gradle.api.InvalidUserCodeException) TreeFormatter(org.gradle.internal.logging.text.TreeFormatter)

Aggregations

TreeFormatter (org.gradle.internal.logging.text.TreeFormatter)46 ImmutableList (com.google.common.collect.ImmutableList)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Nullable (javax.annotation.Nullable)3 GradleException (org.gradle.api.GradleException)3 VariantTransformConfigurationException (org.gradle.api.artifacts.transform.VariantTransformConfigurationException)3 ResolvedVariant (org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedVariant)3 Lists (com.google.common.collect.Lists)2 File (java.io.File)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 InvalidUserDataException (org.gradle.api.InvalidUserDataException)2 ArtifactTransformRegistration (org.gradle.api.internal.artifacts.ArtifactTransformRegistration)2 ConfigurationMetadata (org.gradle.internal.component.model.ConfigurationMetadata)2 ClassGenerationException (org.gradle.internal.instantiation.ClassGenerationException)2 Solution (org.gradle.problems.Solution)2 ImmutableMap (com.google.common.collect.ImmutableMap)1