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();
}
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> {");
}
}
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());
}
Aggregations