Search in sources :

Example 21 with Target

use of java.lang.annotation.Target in project auto by google.

the class FactoryWriter method isTypeUseAnnotation.

private static boolean isTypeUseAnnotation(AnnotationMirror mirror) {
    Element annotationElement = mirror.getAnnotationType().asElement();
    // This is basically equivalent to:
    // Target target = annotationElement.getAnnotation(Target.class);
    // return target != null
    // && Arrays.asList(annotationElement.getAnnotation(Target.class)).contains(TYPE_USE);
    // but that might blow up if the annotation is being compiled at the same time and has an
    // undefined identifier in its @Target values. The rigmarole below avoids that problem.
    Optional<AnnotationMirror> maybeTargetMirror = Mirrors.getAnnotationMirror(annotationElement, Target.class);
    return maybeTargetMirror.map(targetMirror -> AnnotationValues.getEnums(AnnotationMirrors.getAnnotationValue(targetMirror, "value")).stream().map(VariableElement::getSimpleName).anyMatch(name -> name.contentEquals("TYPE_USE"))).orElse(false);
}
Also used : Provider(javax.inject.Provider) ClassName(com.squareup.javapoet.ClassName) Elements(javax.lang.model.util.Elements) FINAL(javax.lang.model.element.Modifier.FINAL) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) ImmutableSet(com.google.common.collect.ImmutableSet) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec.constructorBuilder(com.squareup.javapoet.MethodSpec.constructorBuilder) Element(javax.lang.model.element.Element) Streams(com.google.common.collect.Streams) Collectors.joining(java.util.stream.Collectors.joining) Sets(com.google.common.collect.Sets) TypeKind(javax.lang.model.type.TypeKind) JavaFile(com.squareup.javapoet.JavaFile) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) Stream(java.util.stream.Stream) Filer(javax.annotation.processing.Filer) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) TypeVariable(javax.lang.model.type.TypeVariable) Iterables(com.google.common.collect.Iterables) MoreTypes(com.google.auto.common.MoreTypes) VariableElement(javax.lang.model.element.VariableElement) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) TypeSpec.classBuilder(com.squareup.javapoet.TypeSpec.classBuilder) Objects.requireNonNull(java.util.Objects.requireNonNull) DeclaredType(javax.lang.model.type.DeclaredType) CodeBlock(com.squareup.javapoet.CodeBlock) TypeVariableName(com.squareup.javapoet.TypeVariableName) PRIVATE(javax.lang.model.element.Modifier.PRIVATE) Iterator(java.util.Iterator) MethodSpec(com.squareup.javapoet.MethodSpec) PUBLIC(javax.lang.model.element.Modifier.PUBLIC) GeneratedAnnotationSpecs.generatedAnnotationSpec(com.google.auto.common.GeneratedAnnotationSpecs.generatedAnnotationSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) IOException(java.io.IOException) Target(java.lang.annotation.Target) TypeSpec(com.squareup.javapoet.TypeSpec) AnnotationMirror(javax.lang.model.element.AnnotationMirror) STATIC(javax.lang.model.element.Modifier.STATIC) Collectors.toList(java.util.stream.Collectors.toList) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) AnnotationValues(com.google.auto.common.AnnotationValues) MethodSpec.methodBuilder(com.squareup.javapoet.MethodSpec.methodBuilder) AnnotationMirrors(com.google.auto.common.AnnotationMirrors) AnnotationMirror(javax.lang.model.element.AnnotationMirror) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement)

Example 22 with Target

use of java.lang.annotation.Target in project querydsl by querydsl.

the class ScalaWriterTest method Annotation_With_ArrayMethod.

@Test
public void Annotation_With_ArrayMethod() throws IOException {
    Target annotation = new Target() {

        @Override
        public ElementType[] value() {
            return new ElementType[] { ElementType.FIELD, ElementType.METHOD };
        }

        @Override
        public Class<? extends Annotation> annotationType() {
            return Target.class;
        }
    };
    writer.imports(Target.class.getPackage());
    writer.annotation(annotation);
    assertTrue(w.toString().contains("@Target(Array(FIELD, METHOD))"));
}
Also used : Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType) Test(org.junit.Test)

Example 23 with Target

use of java.lang.annotation.Target in project querydsl by querydsl.

the class JavaWriterTest method Annotation_With_ArrayMethod.

@Test
public void Annotation_With_ArrayMethod() throws IOException {
    Target annotation = new Target() {

        @Override
        public ElementType[] value() {
            return new ElementType[] { ElementType.FIELD, ElementType.METHOD };
        }

        @Override
        public Class<? extends Annotation> annotationType() {
            return Target.class;
        }
    };
    writer.imports(Target.class.getPackage());
    writer.annotation(annotation);
    assertTrue(w.toString().contains("@Target({FIELD, METHOD})"));
}
Also used : Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType) Test(org.junit.Test)

Example 24 with Target

use of java.lang.annotation.Target in project wire by square.

the class JavaGenerator method generateOptionType.

// Example:
// 
// @Retention(RetentionPolicy.RUNTIME)
// @Target(ElementType.FIELD)
// public @interface MyFieldOption {
// String value();
// }
@Nullable
public TypeSpec generateOptionType(Extend extend, Field field) {
    checkArgument(extend.getFields().contains(field));
    if (!emitDeclaredOptions)
        return null;
    ElementType elementType = annotationTargetType(extend);
    if (elementType == null)
        return null;
    if (!eligibleAsAnnotationMember(schema, field))
        return null;
    TypeName returnType = typeName(field.getType());
    ClassName javaType = generatedTypeName(field);
    TypeSpec.Builder builder = TypeSpec.annotationBuilder(javaType.simpleName()).addModifiers(PUBLIC).addAnnotation(AnnotationSpec.builder(Retention.class).addMember("value", "$T.$L", RetentionPolicy.class, RetentionPolicy.RUNTIME).build()).addAnnotation(AnnotationSpec.builder(Target.class).addMember("value", "$T.$L", ElementType.class, elementType).build());
    if (!field.getDocumentation().isEmpty()) {
        builder.addJavadoc("$L\n", field.getDocumentation());
    }
    builder.addMethod(MethodSpec.methodBuilder("value").returns(returnType).addModifiers(PUBLIC, ABSTRACT).build());
    return builder.build();
}
Also used : ElementType(java.lang.annotation.ElementType) TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Target(java.lang.annotation.Target) ClassName(com.squareup.javapoet.ClassName) RetentionPolicy(java.lang.annotation.RetentionPolicy) TypeSpec(com.squareup.javapoet.TypeSpec) Nullable(javax.annotation.Nullable)

Aggregations

Target (java.lang.annotation.Target)24 ElementType (java.lang.annotation.ElementType)15 Retention (java.lang.annotation.Retention)5 RetentionPolicy (java.lang.annotation.RetentionPolicy)4 AnnotationMirror (javax.lang.model.element.AnnotationMirror)4 ClassName (com.squareup.javapoet.ClassName)3 Element (javax.lang.model.element.Element)3 TypeKind (javax.lang.model.type.TypeKind)3 Test (org.junit.Test)3 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)2 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)2 NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)2 SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)2 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)2 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)2 TypeName (com.squareup.javapoet.TypeName)2 TypeSpec (com.squareup.javapoet.TypeSpec)2 AnnotationTree (com.sun.source.tree.AnnotationTree)2 Method (java.lang.reflect.Method)2 VariableElement (javax.lang.model.element.VariableElement)2