use of javax.lang.model.element.AnnotationValue in project auto by google.
the class AutoBuilderProcessor method propertyInitializers.
private ImmutableMap<String, String> propertyInitializers(TypeElement autoBuilderType, ExecutableElement executable) {
boolean autoAnnotation = MoreElements.getAnnotationMirror(executable, AUTO_ANNOTATION_NAME).isPresent();
if (!autoAnnotation) {
return ImmutableMap.of();
}
// We expect the return type of an @AutoAnnotation method to be an annotation type. If it isn't,
// AutoAnnotation will presumably complain, so we don't need to complain further.
TypeMirror returnType = executable.getReturnType();
if (!returnType.getKind().equals(TypeKind.DECLARED)) {
return ImmutableMap.of();
}
// This might not actually be an annotation (if the code is wrong), but if that's the case we
// just won't see any contained ExecutableElement where getDefaultValue() returns something.
TypeElement annotation = MoreTypes.asTypeElement(returnType);
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
for (ExecutableElement method : methodsIn(annotation.getEnclosedElements())) {
AnnotationValue defaultValue = method.getDefaultValue();
if (defaultValue != null) {
String memberName = method.getSimpleName().toString();
builder.put(memberName, AnnotationOutput.sourceFormForInitializer(defaultValue, processingEnv, memberName, autoBuilderType));
}
}
return builder.build();
}
use of javax.lang.model.element.AnnotationValue in project auto by google.
the class AutoBuilderProcessor method findOfClassValue.
private TypeElement findOfClassValue(AnnotationMirror autoBuilderAnnotation) {
AnnotationValue ofClassValue = AnnotationMirrors.getAnnotationValue(autoBuilderAnnotation, "ofClass");
Object value = ofClassValue.getValue();
if (value instanceof TypeMirror) {
TypeMirror ofClassType = (TypeMirror) value;
switch(ofClassType.getKind()) {
case DECLARED:
return MoreTypes.asTypeElement(ofClassType);
case ERROR:
throw new MissingTypeException(MoreTypes.asError(ofClassType));
default:
break;
}
}
throw new MissingTypeException(null);
}
use of javax.lang.model.element.AnnotationValue in project auto by google.
the class AutoAnnotationProcessor method getDefaultValues.
private ImmutableMap<String, AnnotationValue> getDefaultValues(TypeElement annotationElement) {
ImmutableMap.Builder<String, AnnotationValue> defaultValues = ImmutableMap.builder();
for (ExecutableElement member : ElementFilter.methodsIn(annotationElement.getEnclosedElements())) {
String name = member.getSimpleName().toString();
AnnotationValue defaultValue = member.getDefaultValue();
if (defaultValue != null) {
defaultValues.put(name, defaultValue);
}
}
return defaultValues.build();
}
use of javax.lang.model.element.AnnotationValue in project auto by google.
the class AutoAnnotationProcessor method invariableHash.
private static Optional<Integer> invariableHash(List<? extends AnnotationValue> annotationValues) {
int h = 1;
for (AnnotationValue annotationValue : annotationValues) {
Optional<Integer> maybeHash = invariableHash(annotationValue);
if (!maybeHash.isPresent()) {
return Optional.empty();
}
h = h * 31 + maybeHash.get();
}
return Optional.of(h);
}
use of javax.lang.model.element.AnnotationValue in project tiger by google.
the class GeneralInjectorGeneratorHubClone method generateMapContributors.
protected final void generateMapContributors(BindingKey key, ParameterizedTypeName returnType, MethodSpec.Builder methodSpecBuilder) {
Set<DependencyInfo> dependencyInfos = utils.getDependencyInfo(dependencies, key);
// TODO: remove this hack
if (dependencyInfos == null) {
dependencyInfos = new HashSet<>();
logger.w("no dI for key: " + key);
}
Preconditions.checkNotNull(dependencyInfos, String.format("dependencyInfo not found for key: %s", key));
TypeName mapKeyType = returnType.typeArguments.get(0);
TypeName mapValueType = returnType.typeArguments.get(1);
BindingKey mapValueKey = BindingKey.get(mapValueType);
methodSpecBuilder.addStatement("$T mapKey", mapKeyType);
methodSpecBuilder.addStatement("$T mapValue", mapValueType);
for (DependencyInfo di : dependencyInfos) {
if (utils.isMultibindsMethod(di.getProvisionMethodElement())) {
continue;
}
AnnotationMirror mapKeyMirror = Utils.getAnnotationMirrorWithMetaAnnotation(di.getProvisionMethodElement(), MapKey.class);
AnnotationValue unwrapValueAnnotationValue = Utils.getAnnotationValue(elements, mapKeyMirror, "unwrapValue");
if (unwrapValueAnnotationValue != null && !((boolean) unwrapValueAnnotationValue.getValue())) {
logger.e("unwrapValue = false not supported yet. Consider using set binding.");
return;
}
AnnotationValue mapKey = Utils.getAnnotationValue(elements, mapKeyMirror, "value");
logger.l(Kind.NOTE, "mapKey: %s", mapKey.toString());
methodSpecBuilder.addStatement("mapKey = ($T) $L", mapKeyType, mapKey);
if (utils.isMapWithBuiltinValueType(key)) {
methodSpecBuilder.addStatement("mapValue = $L", createAnonymousBuiltinTypeForMultiBinding(mapValueKey, di));
} else {
addNewStatementToMethodSpec(methodSpecBuilder, di, "mapValue");
}
methodSpecBuilder.addStatement("result.put(mapKey, mapValue)");
}
}
Aggregations