Search in sources :

Example 1 with AbstractProcessor

use of javax.annotation.processing.AbstractProcessor in project javapoet by square.

the class AbstractTypesTest method errorTypes.

@Test
public void errorTypes() {
    JavaFileObject hasErrorTypes = JavaFileObjects.forSourceLines("com.squareup.tacos.ErrorTypes", "package com.squareup.tacos;", "", "@SuppressWarnings(\"hook-into-compiler\")", "class ErrorTypes {", "  Tacos tacos;", "  Ingredients.Guacamole guacamole;", "}");
    Compilation compilation = javac().withProcessors(new AbstractProcessor() {

        @Override
        public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
            TypeElement classFile = processingEnv.getElementUtils().getTypeElement("com.squareup.tacos.ErrorTypes");
            List<VariableElement> fields = fieldsIn(classFile.getEnclosedElements());
            ErrorType topLevel = (ErrorType) fields.get(0).asType();
            ErrorType member = (ErrorType) fields.get(1).asType();
            assertThat(TypeName.get(topLevel)).isEqualTo(ClassName.get("", "Tacos"));
            assertThat(TypeName.get(member)).isEqualTo(ClassName.get("Ingredients", "Guacamole"));
            return false;
        }

        @Override
        public Set<String> getSupportedAnnotationTypes() {
            return Collections.singleton("*");
        }
    }).compile(hasErrorTypes);
    assertThat(compilation).failed();
}
Also used : RoundEnvironment(javax.annotation.processing.RoundEnvironment) JavaFileObject(javax.tools.JavaFileObject) Compilation(com.google.testing.compile.Compilation) Set(java.util.Set) ErrorType(javax.lang.model.type.ErrorType) TypeElement(javax.lang.model.element.TypeElement) AbstractProcessor(javax.annotation.processing.AbstractProcessor) VariableElement(javax.lang.model.element.VariableElement) Test(org.junit.Test)

Example 2 with AbstractProcessor

use of javax.annotation.processing.AbstractProcessor in project auto by google.

the class AutoValueCompilationTest method testTypeParametersWithAnnotationsOnBounds.

// Tests that type annotations are correctly copied from the bounds of type parameters in the
// @AutoValue class to the bounds of the corresponding parameters in the generated class. For
// example, if we have `@AutoValue abstract class Foo<T extends @NullableType Object>`, then the
// generated class should be `class AutoValue_Foo<T extends @NullableType Object> extends Foo<T>`.
// Some buggy versions of javac do not report type annotations correctly in this context.
// AutoValue can't copy them if it can't see them, so we make a special annotation processor to
// detect if we are in the presence of this bug and if so we don't fail.
@Test
public void testTypeParametersWithAnnotationsOnBounds() {
    @SupportedAnnotationTypes("*")
    class CompilerBugProcessor extends AbstractProcessor {

        boolean checkedAnnotationsOnTypeBounds;

        boolean reportsAnnotationsOnTypeBounds;

        @Override
        public SourceVersion getSupportedSourceVersion() {
            return SourceVersion.latestSupported();
        }

        @Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
            if (roundEnv.processingOver()) {
                TypeElement test = processingEnv.getElementUtils().getTypeElement("com.example.Test");
                TypeParameterElement t = test.getTypeParameters().get(0);
                this.checkedAnnotationsOnTypeBounds = true;
                this.reportsAnnotationsOnTypeBounds = !t.getBounds().get(0).getAnnotationMirrors().isEmpty();
            }
            return false;
        }
    }
    CompilerBugProcessor compilerBugProcessor = new CompilerBugProcessor();
    JavaFileObject nullableTypeFileObject = JavaFileObjects.forSourceLines("foo.bar.NullableType", "package foo.bar;", "", "import java.lang.annotation.ElementType;", "import java.lang.annotation.Target;", "", "@Target(ElementType.TYPE_USE)", "public @interface NullableType {}");
    JavaFileObject autoValueFileObject = JavaFileObjects.forSourceLines("com.example.Test", "package com.example;", "", "import com.google.auto.value.AutoValue;", "import foo.bar.NullableType;", "", "@AutoValue", "abstract class Test<T extends @NullableType Object & @NullableType Cloneable> {}");
    Compilation compilation = javac().withProcessors(new AutoValueProcessor(), compilerBugProcessor).withOptions("-Xlint:-processing", "-implicit:none").compile(nullableTypeFileObject, autoValueFileObject);
    assertThat(compilation).succeededWithoutWarnings();
    assertThat(compilerBugProcessor.checkedAnnotationsOnTypeBounds).isTrue();
    if (compilerBugProcessor.reportsAnnotationsOnTypeBounds) {
        assertThat(compilation).generatedSourceFile("com.example.AutoValue_Test").contentsAsUtf8String().contains("class AutoValue_Test<T extends @NullableType Object & @NullableType Cloneable>" + " extends Test<T> {");
    }
}
Also used : RoundEnvironment(javax.annotation.processing.RoundEnvironment) JavaFileObject(javax.tools.JavaFileObject) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Compilation(com.google.testing.compile.Compilation) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) TypeElement(javax.lang.model.element.TypeElement) AbstractProcessor(javax.annotation.processing.AbstractProcessor) TypeParameterElement(javax.lang.model.element.TypeParameterElement) Test(org.junit.Test)

Example 3 with AbstractProcessor

use of javax.annotation.processing.AbstractProcessor in project vertx-docgen by vert-x3.

the class BaseProcessorTest method testGen.

@Test
public void testGen() throws Exception {
    AtomicInteger count = new AtomicInteger();
    AbstractProcessor proc = new AbstractProcessor() {

        @Override
        public Set<String> getSupportedAnnotationTypes() {
            return Collections.singleton("*");
        }

        @Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
            if (count.getAndIncrement() == 0) {
                try {
                    Filer filer = processingEnv.getFiler();
                    Element elt = processingEnv.getElementUtils().getTypeElement("gen.GeneratedClass");
                    JavaFileObject src = filer.createSourceFile("io.vertx.test.gen.GeneratedClass", elt);
                    try (Writer writer = src.openWriter()) {
                        writer.append("package io.vertx.test.gen;\npublic class GeneratedClass {\n}");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return true;
        }
    };
    Compiler<TestGenProcessor> compiler = buildCompiler(new TestGenProcessor(), "io.vertx.test.gen");
    compiler.addProcessor(proc);
    compiler.assertCompile();
    assertEquals(3, count.get());
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) AbstractProcessor(javax.annotation.processing.AbstractProcessor) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IOException(java.io.IOException) RoundEnvironment(javax.annotation.processing.RoundEnvironment) JavaFileObject(javax.tools.JavaFileObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Filer(javax.annotation.processing.Filer) Writer(java.io.Writer) Test(org.junit.Test)

Aggregations

AbstractProcessor (javax.annotation.processing.AbstractProcessor)3 RoundEnvironment (javax.annotation.processing.RoundEnvironment)3 TypeElement (javax.lang.model.element.TypeElement)3 JavaFileObject (javax.tools.JavaFileObject)3 Test (org.junit.Test)3 Compilation (com.google.testing.compile.Compilation)2 Set (java.util.Set)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Filer (javax.annotation.processing.Filer)1 SupportedAnnotationTypes (javax.annotation.processing.SupportedAnnotationTypes)1 Element (javax.lang.model.element.Element)1 TypeParameterElement (javax.lang.model.element.TypeParameterElement)1 VariableElement (javax.lang.model.element.VariableElement)1 ErrorType (javax.lang.model.type.ErrorType)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1