use of javax.lang.model.element.AnnotationValue in project auto by google.
the class AnnotationMirrorsTest method testGetValueEntry.
@Test
public void testGetValueEntry() {
Map.Entry<ExecutableElement, AnnotationValue> elementValue = AnnotationMirrors.getAnnotationElementAndValue(annotationOn(TestClassBlah.class), "value");
assertThat(elementValue.getKey().getSimpleName().toString()).isEqualTo("value");
assertThat(elementValue.getValue().getValue()).isInstanceOf(VariableElement.class);
AnnotationValue value = AnnotationMirrors.getAnnotationValue(annotationOn(TestClassBlah.class), "value");
assertThat(value.getValue()).isInstanceOf(VariableElement.class);
}
use of javax.lang.model.element.AnnotationValue in project auto by google.
the class AutoAnnotationProcessor method processMethod.
private void processMethod(ExecutableElement method) {
if (!method.getModifiers().contains(Modifier.STATIC)) {
throw abortWithError("@AutoAnnotation method must be static", method);
}
TypeElement annotationElement = getAnnotationReturnType(method);
TypeMirror annotationTypeMirror = annotationElement.asType();
Set<Class<?>> wrapperTypesUsedInCollections = wrapperTypesUsedInCollections(method);
ImmutableMap<String, ExecutableElement> memberMethods = getMemberMethods(annotationElement);
Set<TypeMirror> memberTypes = getMemberTypes(memberMethods.values());
Set<TypeMirror> referencedTypes = getReferencedTypes(annotationTypeMirror, method, memberTypes, wrapperTypesUsedInCollections);
TypeElement methodClass = (TypeElement) method.getEnclosingElement();
String pkg = TypeSimplifier.packageNameOf(methodClass);
TypeSimplifier typeSimplifier = new TypeSimplifier(typeUtils, pkg, referencedTypes, annotationTypeMirror);
AnnotationOutput annotationOutput = new AnnotationOutput(typeSimplifier);
ImmutableMap<String, AnnotationValue> defaultValues = getDefaultValues(annotationElement);
ImmutableMap<String, Member> members = getMembers(method, memberMethods, typeSimplifier, annotationOutput);
ImmutableMap<String, Parameter> parameters = getParameters(annotationElement, method, members, typeSimplifier);
validateParameters(annotationElement, method, members, parameters, defaultValues);
String generatedClassName = generatedClassName(method);
AutoAnnotationTemplateVars vars = new AutoAnnotationTemplateVars();
vars.annotationFullName = annotationElement.toString();
vars.annotationName = typeSimplifier.simplify(annotationElement.asType());
vars.className = generatedClassName;
vars.imports = typeSimplifier.typesToImport();
vars.generated = typeSimplifier.simplify(getTypeMirror(Generated.class));
vars.arrays = typeSimplifier.simplify(getTypeMirror(Arrays.class));
vars.members = members;
vars.params = parameters;
vars.pkg = pkg;
vars.wrapperTypesUsedInCollections = wrapperTypesUsedInCollections;
vars.gwtCompatible = isGwtCompatible(annotationElement);
ImmutableMap<String, Integer> invariableHashes = invariableHashes(members, parameters.keySet());
vars.invariableHashSum = 0;
for (int h : invariableHashes.values()) {
vars.invariableHashSum += h;
}
vars.invariableHashes = invariableHashes.keySet();
String text = vars.toText();
text = Reformatter.fixup(text);
String fullName = fullyQualifiedName(pkg, generatedClassName);
writeSourceFile(fullName, text, methodClass);
}
use of javax.lang.model.element.AnnotationValue in project auto by google.
the class AutoAnnotationProcessor method invariableHashes.
/**
* Returns a map from the names of members with invariable hashCodes to the values of those
* hashCodes.
*/
private static ImmutableMap<String, Integer> invariableHashes(ImmutableMap<String, Member> members, ImmutableSet<String> parameters) {
ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder();
for (String element : members.keySet()) {
if (!parameters.contains(element)) {
Member member = members.get(element);
AnnotationValue annotationValue = member.method.getDefaultValue();
Optional<Integer> invariableHash = invariableHash(annotationValue);
if (invariableHash.isPresent()) {
builder.put(element, (element.hashCode() * 127) ^ invariableHash.get());
}
}
}
return builder.build();
}
use of javax.lang.model.element.AnnotationValue in project auto by google.
the class AutoServiceProcessor method getProviderInterface.
private DeclaredType getProviderInterface(AnnotationMirror providerAnnotation) {
// The very simplest of way of doing this, is also unfortunately unworkable.
// We'd like to do:
// ServiceProvider provider = e.getAnnotation(ServiceProvider.class);
// Class<?> providerInterface = provider.value();
//
// but unfortunately we can't load the arbitrary class at annotation
// processing time. So, instead, we have to use the mirror to get at the
// value (much more painful).
Map<? extends ExecutableElement, ? extends AnnotationValue> valueIndex = providerAnnotation.getElementValues();
log("annotation values: " + valueIndex);
AnnotationValue value = valueIndex.values().iterator().next();
return (DeclaredType) value.getValue();
}
use of javax.lang.model.element.AnnotationValue in project neo4j-mobile-android by neo4j-contrib.
the class ServiceProcessor method process.
@SuppressWarnings("unchecked")
@Override
protected void process(TypeElement annotationType, Element annotated, AnnotationMirror annotation, Map<? extends ExecutableElement, ? extends AnnotationValue> values) throws IOException {
for (AnnotationValue o : (List<? extends AnnotationValue>) values.values().iterator().next().getValue()) {
TypeMirror service = (TypeMirror) o.getValue();
addTo(((TypeElement) annotated).getQualifiedName().toString(), "META-INF", "services", service.toString());
}
}
Aggregations