use of javax.lang.model.element.AnnotationValue in project javapoet by square.
the class AnnotationSpec method get.
public static AnnotationSpec get(AnnotationMirror annotation) {
TypeElement element = (TypeElement) annotation.getAnnotationType().asElement();
AnnotationSpec.Builder builder = AnnotationSpec.builder(ClassName.get(element));
Visitor visitor = new Visitor(builder);
for (ExecutableElement executableElement : annotation.getElementValues().keySet()) {
String name = executableElement.getSimpleName().toString();
AnnotationValue value = annotation.getElementValues().get(executableElement);
value.accept(visitor, name);
}
return builder.build();
}
use of javax.lang.model.element.AnnotationValue in project antlr4 by tunnelvisionlabs.
the class RuleDependencyProcessor method findRuleDependencyProperty.
@Nullable
private Tuple2<AnnotationMirror, AnnotationValue> findRuleDependencyProperty(@NotNull Tuple2<RuleDependency, Element> dependency, @NotNull RuleDependencyProperty property) {
TypeElement ruleDependencyTypeElement = processingEnv.getElementUtils().getTypeElement(RuleDependencyClassName);
TypeElement ruleDependenciesTypeElement = processingEnv.getElementUtils().getTypeElement(RuleDependenciesClassName);
List<? extends AnnotationMirror> mirrors = dependency.getItem2().getAnnotationMirrors();
for (AnnotationMirror annotationMirror : mirrors) {
if (processingEnv.getTypeUtils().isSameType(ruleDependencyTypeElement.asType(), annotationMirror.getAnnotationType())) {
AnnotationValue element = findRuleDependencyProperty(dependency, annotationMirror, property);
if (element != null) {
return Tuple.create(annotationMirror, element);
}
} else if (processingEnv.getTypeUtils().isSameType(ruleDependenciesTypeElement.asType(), annotationMirror.getAnnotationType())) {
Map<? extends ExecutableElement, ? extends AnnotationValue> values = annotationMirror.getElementValues();
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> value : values.entrySet()) {
if ("value()".equals(value.getKey().toString())) {
AnnotationValue annotationValue = value.getValue();
if (!(annotationValue.getValue() instanceof List)) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Expected array of RuleDependency annotations for annotation property 'value()'.", dependency.getItem2(), annotationMirror, annotationValue);
break;
}
List<?> annotationValueList = (List<?>) annotationValue.getValue();
for (Object obj : annotationValueList) {
if (!(obj instanceof AnnotationMirror)) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Expected RuleDependency annotation mirror for element of property 'value()'.", dependency.getItem2(), annotationMirror, annotationValue);
break;
}
AnnotationValue element = findRuleDependencyProperty(dependency, (AnnotationMirror) obj, property);
if (element != null) {
return Tuple.create((AnnotationMirror) obj, element);
}
}
} else {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, String.format("Unexpected annotation property %s.", value.getKey().toString()), dependency.getItem2(), annotationMirror, value.getValue());
}
}
}
}
return null;
}
use of javax.lang.model.element.AnnotationValue in project robolectric by robolectric.
the class Helpers method getImplementedClassName.
private TypeMirror getImplementedClassName(AnnotationMirror am) {
AnnotationValue className = Helpers.getAnnotationTypeMirrorValue(am, "className");
if (className == null) {
return null;
}
String classNameString = Helpers.getAnnotationStringValue(className);
if (classNameString == null) {
return null;
}
TypeElement impElement = elements.getTypeElement(classNameString.replace('$', '.'));
if (impElement == null) {
return null;
}
return impElement.asType();
}
use of javax.lang.model.element.AnnotationValue in project robolectric by robolectric.
the class Helpers method getImplementedClass.
public TypeMirror getImplementedClass(AnnotationMirror am) {
if (am == null) {
return null;
}
// RobolectricWiringTest prefers className (if provided) to value, so we do the same here.
TypeMirror impType = getImplementedClassName(am);
if (impType != null) {
return impType;
}
AnnotationValue av = Helpers.getAnnotationTypeMirrorValue(am, "value");
if (av == null) {
return null;
}
TypeMirror type = Helpers.getAnnotationTypeMirrorValue(av);
if (type == null) {
return null;
}
return type;
}
use of javax.lang.model.element.AnnotationValue in project j2objc by google.
the class AnnotationRewriter method addDefaultAccessors.
// Create accessors for properties that have default values.
private void addDefaultAccessors(AnnotationTypeDeclaration node, List<AnnotationTypeMemberDeclaration> members) {
TypeElement type = node.getTypeElement();
for (AnnotationTypeMemberDeclaration member : members) {
ExecutableElement memberElement = member.getExecutableElement();
AnnotationValue defaultValue = memberElement.getDefaultValue();
if (defaultValue == null || defaultValue.getValue() == null) {
continue;
}
TypeMirror memberType = memberElement.getReturnType();
String propName = NameTable.getAnnotationPropertyName(memberElement);
ExecutableElement defaultGetterElement = GeneratedExecutableElement.newMethodWithSelector(propName + "Default", memberType, type).addModifiers(Modifier.STATIC);
MethodDeclaration defaultGetter = new MethodDeclaration(defaultGetterElement);
defaultGetter.setHasDeclaration(false);
Block defaultGetterBody = new Block();
defaultGetter.setBody(defaultGetterBody);
defaultGetterBody.addStatement(new ReturnStatement(translationUtil.createAnnotationValue(memberType, defaultValue)));
node.addBodyDeclaration(defaultGetter);
}
}
Aggregations