private void addOttoAnnotation(ExecutableElement element, JMethod method) { for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { if (annotationMirror.getAnnotationType().toString().equals(getTarget())) { codeModelHelper.copyAnnotation(method, annotationMirror); break; } } }
@NonNull private static Pair<List<AnnotationSpec>, Set<ClassName>> getAnnotations(@NonNull ExecutableElement method) { final List<AnnotationSpec> specs = method.getAnnotationMirrors().stream().map(AnnotationSpec::get).collect(Collectors.toList()); final Set<ClassName> markerAnnotations = new HashSet<>(); for (final Iterator<AnnotationSpec> iterator = specs.iterator(); iterator.hasNext(); ) { final AnnotationSpec spec = iterator.next(); for (ClassName a : org.acra.processor.util.Types.MARKER_ANNOTATIONS) { if (a.equals(spec.type)) { iterator.remove(); markerAnnotations.add(a); } } } return Pair.of(specs, markerAnnotations); }
private static boolean hasAnyOfAnnotation(ExecutableElement input, List<String> annotations) { return input.getAnnotationMirrors().stream() .map(annotationMirror -> asType(annotationMirror.getAnnotationType().asElement())) .anyMatch(type -> typeInAnnotations(type, annotations)); }
/** Returns the provided key for {@code method} wrapped by {@code Set}. */ public static String getSetKey(ExecutableElement method) { StringBuilder result = new StringBuilder(); AnnotationMirror qualifier = getQualifier(method.getAnnotationMirrors()); if (qualifier != null) { qualifierToString(qualifier, result); } result.append(SET_PREFIX); typeToString(method.getReturnType(), result, '$'); result.append(">"); return result.toString(); }
/** Returns the provided key for {@code method}. */ public static String get(ExecutableElement method) { StringBuilder result = new StringBuilder(); AnnotationMirror qualifier = getQualifier(method.getAnnotationMirrors()); if (qualifier != null) { qualifierToString(qualifier, result); } typeToString(method.getReturnType(), result, '$'); return result.toString(); }
private Builder addReturnAnnotations(Builder builder, ExecutableElement methodToOverride) { Elements elements = processingEnv.getElementUtils(); TypeElement visibleForTestingTypeElement = elements .getTypeElement(VISIBLE_FOR_TESTING_QUALIFIED_NAME_ANDROIDX); if (visibleForTestingTypeElement == null) { // Fall back to looking for the Support library version. visibleForTestingTypeElement = elements.getTypeElement(VISIBLE_FOR_TESTING_QUALIFIED_NAME); } String visibleForTestingTypeQualifiedName = visibleForTestingTypeElement.toString(); for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) { builder.addAnnotation(AnnotationSpec.get(mirror)); // Suppress a lint warning if we're overriding a VisibleForTesting method. // See #1977. String annotationQualifiedName = mirror.getAnnotationType().toString(); if (annotationQualifiedName.equals(visibleForTestingTypeQualifiedName)) { builder.addAnnotation( AnnotationSpec.builder( ClassName.get(SUPPRESS_LINT_PACKAGE_NAME, SUPPRESS_LINT_CLASS_NAME)) .addMember("value", "$S", "VisibleForTests") .build()); } } return builder; }
private void validateAnnotatedNonNull(ExecutableElement executableElement) { Set<String> annotationNames = FluentIterable.from(executableElement.getAnnotationMirrors()) .transform(new Function<AnnotationMirror, String>() { @Override public String apply(AnnotationMirror input) { return input.getAnnotationType().asElement().toString(); } }) .toSet(); boolean noNonNull = true; for (ClassName nonNull : nonNulls()) { if (annotationNames.contains(nonNull.reflectionName())) { noNonNull = false; break; } } if (noNonNull) { processingEnvironment.getMessager().printMessage( Kind.WARNING, getQualifiedMethodName(executableElement) + " is missing the " + nonNull().reflectionName() + " annotation," + " please add it to ensure that your extension methods are always returning non-null" + " values"); } } }
/** * Generates overrides of existing RequestManager methods so that they return our generated * RequestBuilder subtype. */ private MethodSpec generateRequestManagerRequestBuilderMethodOverride( ExecutableElement methodToOverride) { // We've already verified that this method returns a RequestBuilder and RequestBuilders have // exactly one type argument, so this is safe unless those assumptions change. TypeMirror typeArgument = ((DeclaredType) methodToOverride.getReturnType()).getTypeArguments().get(0); ParameterizedTypeName generatedRequestBuilderOfType = ParameterizedTypeName.get(generatedRequestBuilderClassName, ClassName.get(typeArgument)); MethodSpec.Builder builder = ProcessorUtil.overriding(methodToOverride) .returns(generatedRequestBuilderOfType); builder.addCode( ProcessorUtil.generateCastingSuperCall(generatedRequestBuilderOfType, builder.build())); for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) { builder.addAnnotation(AnnotationSpec.get(mirror)); } return builder.build(); }
int modifier = elementVisibilityModifierToJMod(executableElement); JMethod method = holder.getGeneratedClass().method(modifier, returnType, methodName); copyNonAAAnnotations(method, executableElement.getAnnotationMirrors());
elements, executableElement, annotationType, "name", String.class); List<? extends AnnotationMirror> annotationMirrors = executableElement.getAnnotationMirrors(); AnnotationMirror mirror = null; for (AnnotationMirror m : annotationMirrors) {
for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) { result.addAnnotation(AnnotationSpec.get(mirror));
.build()); for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) { builder = builder.addAnnotation(AnnotationSpec.get(mirror));
@Override public List<? extends AnnotationMirror> getAnnotations() { return getterMethod.getAnnotationMirrors(); }
private static boolean generateDefaultAnnotations(ExecutableElement getterMethod) { for (AnnotationMirror annotationMirror : getterMethod.getAnnotationMirrors()) { TypeElement annotationTypeElement = (TypeElement) (annotationMirror.getAnnotationType().asElement()); QualifiedName annotationType = QualifiedName.of(annotationTypeElement); if (DISABLE_PROPERTY_ANNOTATIONS.contains(annotationType)) { return false; } } return true; } }
/** * Check if there is a {@code @Valid} annotation present on the method. * * @param method a method to check for annotation presence * @return {@code true} if {@code @Valid} annotation is present on return value of a given method, {@code false} * otherwise */ private boolean methodIsAnnotatedWithValid(ExecutableElement method) { for ( AnnotationMirror annotationMirror : method.getAnnotationMirrors() ) { if ( ConstraintHelper.AnnotationType.GRAPH_VALIDATION_ANNOTATION.equals( constraintHelper.getAnnotationType( annotationMirror ) ) ) { return true; } } return false; } }
public void addJacksonAnnotations( Property.Builder resultBuilder, ExecutableElement getterMethod) { Optional<AnnotationMirror> jsonPropertyAnnotation = findAnnotationMirror(getterMethod, JSON_PROPERTY); if (jsonPropertyAnnotation.isPresent()) { resultBuilder.addAccessorAnnotations(Excerpts.add("%s%n", jsonPropertyAnnotation.get())); } else if (generateDefaultAnnotations(getterMethod)) { resultBuilder.addAccessorAnnotations(Excerpts.add( "@%s(\"%s\")%n", JSON_PROPERTY, resultBuilder.getName())); } getterMethod .getAnnotationMirrors() .stream() .filter(this::isXmlAnnotation) .forEach(annotation -> { resultBuilder.addAccessorAnnotations(code -> code.addLine("%s", annotation)); }); }
@Test public void nullablePrimitive() { ExecutableElement getterMethod = (ExecutableElement) model.newElementWithMarker( "package com.example;", "public class DataType {", " ---> public abstract @" + Nullable.class.getName() + " int getAge();", " public static class Builder extends DataType_Builder {}", "}"); Property property = new Property.Builder() .setType(getterMethod.getReturnType()) .buildPartial(); when(config.getProperty()).thenReturn(property); doReturn(getterMethod.getAnnotationMirrors()).when(config).getAnnotations(); Optional<NullableProperty> codeGenerator = factory.create(config); assertThat(codeGenerator).isEqualTo(Optional.empty()); } }
@Test public void notNullable() { ExecutableElement getterMethod = (ExecutableElement) model.newElementWithMarker( "package com.example;", "public class DataType {", " ---> public abstract String getName();", " public static class Builder extends DataType_Builder {}", "}"); Property property = new Property.Builder() .setType(getterMethod.getReturnType()) .buildPartial(); when(config.getProperty()).thenReturn(property); doReturn(getterMethod.getAnnotationMirrors()).when(config).getAnnotations(); Optional<NullableProperty> codeGenerator = factory.create(config); assertThat(codeGenerator).isEqualTo(Optional.empty()); }
@Test public void nullable() { TypeElement dataType = model.newType( "package com.example;", "public class DataType {", " public abstract @" + Nullable.class.getName() + " String getName();", " public static class Builder extends DataType_Builder {}", "}"); DeclaredType builderType = (DeclaredType) getOnlyElement(typesIn(dataType.getEnclosedElements())).asType(); ExecutableElement getterMethod = getOnlyElement(methodsIn(dataType.getEnclosedElements())); Property property = new Property.Builder() .setType(getterMethod.getReturnType()) .setCapitalizedName("Name") .buildPartial(); when(config.getBuilder()).thenReturn(builderType); when(config.getProperty()).thenReturn(property); when(config.getElements()).thenReturn(model.elementUtils()); doReturn(getterMethod.getAnnotationMirrors()).when(config).getAnnotations(); Optional<NullableProperty> codeGenerator = factory.create(config); assertThat(codeGenerator.get()).isEqualTo(new NullableProperty( datatype, property, ImmutableSet.of(model.typeElement(Nullable.class)), unaryOperator(model.typeMirror(String.class)))); }
@Test public void arbitraryNullableAnnotation() { model.newType( "package foo.bar;", "public @interface Nullable {}"); TypeElement dataType = model.newType( "package com.example;", "public class DataType {", " public abstract @foo.bar.Nullable String getName();", " public static class Builder extends DataType_Builder {}", "}"); DeclaredType builderType = (DeclaredType) getOnlyElement(typesIn(dataType.getEnclosedElements())).asType(); ExecutableElement getterMethod = getOnlyElement(methodsIn(dataType.getEnclosedElements())); Property property = new Property.Builder() .setType(getterMethod.getReturnType()) .setCapitalizedName("Name") .buildPartial(); when(config.getBuilder()).thenReturn(builderType); when(config.getProperty()).thenReturn(property); when(config.getElements()).thenReturn(model.elementUtils()); doReturn(getterMethod.getAnnotationMirrors()).when(config).getAnnotations(); Optional<NullableProperty> codeGenerator = factory.create(config); assertThat(codeGenerator.get()).isEqualTo(new NullableProperty( datatype, property, ImmutableSet.of(model.typeElement("foo.bar.Nullable")), unaryOperator(model.typeMirror(String.class)))); }