FactoryMethodDescriptor generateDescriptorForConstructor(final AutoFactoryDeclaration declaration,
ExecutableElement constructor) {
checkNotNull(constructor);
checkArgument(constructor.getKind() == ElementKind.CONSTRUCTOR);
Element classElement = constructor.getEnclosingElement();
Name returnType = classElement.accept(
new ElementKindVisitor6<Name, Void>() {
@Override
protected Name defaultAction(Element e, Void p) {
throw new AssertionError();
}
@Override
public Name visitTypeAsClass(TypeElement e, Void p) {
if (!e.getTypeParameters().isEmpty()) {
messager.printMessage(ERROR, "AutoFactory does not support generic types", e);
}
return e.getQualifiedName();
}
}, null);
ImmutableListMultimap<Boolean, ? extends VariableElement> parameterMap =
Multimaps.index(constructor.getParameters(), Functions.forPredicate(
new Predicate<VariableElement>() {
@Override
public boolean apply(VariableElement parameter) {
return parameter.getAnnotation(Provided.class) != null;
}
}));
ImmutableSet<Parameter> providedParameters = Parameter.forParameterList(parameterMap.get(true));
ImmutableSet<Parameter> passedParameters = Parameter.forParameterList(parameterMap.get(false));
return new FactoryMethodDescriptor.Builder(declaration)
.factoryName(declaration.getFactoryName(
elements.getPackageOf(constructor).getQualifiedName(), classElement.getSimpleName()))
.name("create")
.returnType(returnType.toString())
.publicMethod(constructor.getEnclosingElement().getModifiers().contains(PUBLIC))
.providedParameters(providedParameters)
.passedParameters(passedParameters)
.creationParameters(Parameter.forParameterList(constructor.getParameters()))
.build();