Map<String, AnnotationValue> values =
Mirrors.simplifyAnnotationValueMap(elements.getElementValuesWithDefaults(mirror));
checkState(values.size() == 3);
// className value is a string, so we can just call toString
AnnotationValue classNameValue = values.get("className");
String className = classNameValue.getValue().toString();
if (!className.isEmpty() && !isValidIdentifier(className)) {
messager.printMessage(ERROR,
String.format("\"%s\" is not a valid Java identifier", className),
element, mirror, classNameValue);
return Optional.absent();
}
AnnotationValue extendingValue = checkNotNull(values.get("extending"));
TypeElement extendingType = AnnotationValues.asType(extendingValue);
if (extendingType == null) {
messager.printMessage(ERROR, "Unable to find the type: "
+ extendingValue.getValue().toString(),
element, mirror, extendingValue);
return Optional.absent();
} else if (!isValidSupertypeForClass(extendingType)) {
messager.printMessage(ERROR,
String.format("%s is not a valid supertype for a factory. "
+ "Supertypes must be non-final classes.",
extendingType.getQualifiedName()),
element, mirror, extendingValue);
return Optional.absent();
}
ImmutableList<ExecutableElement> noParameterConstructors =
FluentIterable.from(ElementFilter.constructorsIn(extendingType.getEnclosedElements()))
.filter(new Predicate<ExecutableElement>() {
@Override public boolean apply(ExecutableElement constructor) {
return constructor.getParameters().isEmpty();
}
})
.toList();
if (noParameterConstructors.size() == 0) {
messager.printMessage(ERROR,
String.format("%s is not a valid supertype for a factory. "
+ "Factory supertypes must have a no-arg constructor.",
extendingType.getQualifiedName()),
element, mirror, extendingValue);
return Optional.absent();
} else if (noParameterConstructors.size() > 1) {
throw new IllegalStateException("Multiple constructors with no parameters??");
}
AnnotationValue implementingValue = checkNotNull(values.get("implementing"));
ImmutableSet.Builder<TypeElement> builder = ImmutableSet.builder();
for (AnnotationValue implementingTypeValue : AnnotationValues.asList(implementingValue)) {
builder.add(AnnotationValues.asType(implementingTypeValue));
}
ImmutableSet<TypeElement> implementingTypes = builder.build();