Search in sources :

Example 1 with ClassGenerationException

use of org.gradle.internal.instantiation.ClassGenerationException in project gradle by gradle.

the class AbstractClassGenerator method generateUnderLock.

private GeneratedClassImpl generateUnderLock(Class<?> type) {
    List<CustomInjectAnnotationPropertyHandler> customAnnotationPropertyHandlers = new ArrayList<>(enabledAnnotations.size());
    ServicesPropertyHandler servicesHandler = new ServicesPropertyHandler();
    InjectAnnotationPropertyHandler injectionHandler = new InjectAnnotationPropertyHandler();
    PropertyTypePropertyHandler propertyTypedHandler = new PropertyTypePropertyHandler();
    ManagedPropertiesHandler managedPropertiesHandler = new ManagedPropertiesHandler();
    NamePropertyHandler namePropertyHandler = new NamePropertyHandler();
    ExtensibleTypePropertyHandler extensibleTypeHandler = new ExtensibleTypePropertyHandler();
    DslMixInPropertyType dslMixInHandler = new DslMixInPropertyType(extensibleTypeHandler);
    // Order is significant. Injection handler should be at the end
    List<ClassGenerationHandler> handlers = new ArrayList<>(5 + enabledAnnotations.size() + disabledAnnotations.size());
    handlers.add(extensibleTypeHandler);
    handlers.add(dslMixInHandler);
    handlers.add(propertyTypedHandler);
    handlers.add(servicesHandler);
    handlers.add(namePropertyHandler);
    handlers.add(managedPropertiesHandler);
    for (Class<? extends Annotation> annotation : enabledAnnotations) {
        customAnnotationPropertyHandlers.add(new CustomInjectAnnotationPropertyHandler(annotation));
    }
    handlers.addAll(customAnnotationPropertyHandlers);
    handlers.add(injectionHandler);
    // Order is significant
    List<ClassValidator> validators = new ArrayList<>(2 + disabledAnnotations.size());
    for (Class<? extends Annotation> annotation : disabledAnnotations) {
        validators.add(new DisabledAnnotationValidator(annotation));
    }
    validators.add(new InjectionAnnotationValidator(enabledAnnotations, allowedTypesForAnnotation));
    Class<?> generatedClass;
    try {
        ClassInspectionVisitor inspectionVisitor = start(type);
        inspectType(type, validators, handlers, extensibleTypeHandler);
        for (ClassGenerationHandler handler : handlers) {
            handler.applyTo(inspectionVisitor);
        }
        ClassGenerationVisitor generationVisitor = inspectionVisitor.builder();
        for (ClassGenerationHandler handler : handlers) {
            handler.applyTo(generationVisitor);
        }
        boolean shouldImplementNameProperty = namePropertyHandler.hasNameProperty();
        if (type.isInterface()) {
            if (shouldImplementNameProperty) {
                generationVisitor.addNameConstructor();
            } else {
                generationVisitor.addDefaultConstructor();
            }
        } else {
            for (Constructor<?> constructor : type.getConstructors()) {
                generationVisitor.addConstructor(constructor, shouldImplementNameProperty);
            }
        }
        generatedClass = generationVisitor.generate();
    } catch (ClassGenerationException e) {
        throw e;
    } catch (Throwable e) {
        TreeFormatter formatter = new TreeFormatter();
        formatter.node("Could not generate a decorated class for type ");
        formatter.appendType(type);
        formatter.append(".");
        throw new ClassGenerationException(formatter.toString(), e);
    }
    ImmutableList.Builder<Class<? extends Annotation>> annotationsTriggeringServiceInjection = ImmutableList.builder();
    for (CustomInjectAnnotationPropertyHandler handler : customAnnotationPropertyHandlers) {
        if (handler.isUsed()) {
            annotationsTriggeringServiceInjection.add(handler.getAnnotation());
        }
    }
    // This is expensive to calculate, so cache the result
    Class<?> enclosingClass = type.getEnclosingClass();
    Class<?> outerType;
    if (enclosingClass != null && !Modifier.isStatic(type.getModifiers())) {
        outerType = enclosingClass;
    } else {
        outerType = null;
    }
    return new GeneratedClassImpl(generatedClass, outerType, injectionHandler.getInjectedServices(), annotationsTriggeringServiceInjection.build());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) TreeFormatter(org.gradle.internal.logging.text.TreeFormatter) ClassGenerationException(org.gradle.internal.instantiation.ClassGenerationException) Annotation(java.lang.annotation.Annotation)

Example 2 with ClassGenerationException

use of org.gradle.internal.instantiation.ClassGenerationException in project gradle by gradle.

the class AsmBackedClassGenerator method start.

@Override
protected ClassInspectionVisitor start(Class<?> type) {
    if (type.isAnnotation() || type.isEnum()) {
        TreeFormatter formatter = new TreeFormatter();
        formatter.node(type);
        formatter.append(" is not a class or interface.");
        throw new ClassGenerationException(formatter.toString());
    }
    return new ClassInspectionVisitorImpl(type, decorate, suffix, factoryId);
}
Also used : TreeFormatter(org.gradle.internal.logging.text.TreeFormatter) ClassGenerationException(org.gradle.internal.instantiation.ClassGenerationException)

Aggregations

ClassGenerationException (org.gradle.internal.instantiation.ClassGenerationException)2 TreeFormatter (org.gradle.internal.logging.text.TreeFormatter)2 ImmutableList (com.google.common.collect.ImmutableList)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1