Search in sources :

Example 1 with AutoFactory

use of com.google.auto.factory.AutoFactory in project auto by google.

the class FactoryDescriptorGenerator method generateDescriptor.

ImmutableSet<FactoryMethodDescriptor> generateDescriptor(Element element) {
    final AnnotationMirror mirror = Mirrors.getAnnotationMirror(element, AutoFactory.class).get();
    final Optional<AutoFactoryDeclaration> declaration = declarationFactory.createIfValid(element);
    if (!declaration.isPresent()) {
        return ImmutableSet.of();
    }
    return element.accept(new ElementKindVisitor6<ImmutableSet<FactoryMethodDescriptor>, Void>() {

        @Override
        protected ImmutableSet<FactoryMethodDescriptor> defaultAction(Element e, Void p) {
            throw new AssertionError("@AutoFactory applied to an impossible element");
        }

        @Override
        public ImmutableSet<FactoryMethodDescriptor> visitTypeAsClass(TypeElement type, Void p) {
            if (type.getModifiers().contains(ABSTRACT)) {
                // applied to an abstract factory
                messager.printMessage(ERROR, "Auto-factory doesn't support being applied to abstract classes.", type, mirror);
                return ImmutableSet.of();
            } else {
                // applied to the type to be created
                ImmutableSet<ExecutableElement> constructors = Elements2.getConstructors(type);
                if (constructors.isEmpty()) {
                    return generateDescriptorForDefaultConstructor(declaration.get(), type);
                } else {
                    return FluentIterable.from(constructors).transform(new Function<ExecutableElement, FactoryMethodDescriptor>() {

                        @Override
                        public FactoryMethodDescriptor apply(ExecutableElement constructor) {
                            return generateDescriptorForConstructor(declaration.get(), constructor);
                        }
                    }).toSet();
                }
            }
        }

        @Override
        public ImmutableSet<FactoryMethodDescriptor> visitTypeAsInterface(TypeElement type, Void p) {
            // applied to the factory interface
            messager.printMessage(ERROR, "Auto-factory doesn't support being applied to interfaces.", type, mirror);
            return ImmutableSet.of();
        }

        @Override
        public ImmutableSet<FactoryMethodDescriptor> visitExecutableAsConstructor(ExecutableElement e, Void p) {
            // applied to a constructor of a type to be created
            return ImmutableSet.of(generateDescriptorForConstructor(declaration.get(), e));
        }
    }, null);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationMirror(javax.lang.model.element.AnnotationMirror) AutoFactory(com.google.auto.factory.AutoFactory) ImmutableSet(com.google.common.collect.ImmutableSet)

Aggregations

AutoFactory (com.google.auto.factory.AutoFactory)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1