use of javax.annotation.processing.Processor in project buck by facebook.
the class StubJarTest method createFullAndStubJars.
private JarPaths createFullAndStubJars(ImmutableSortedSet<Path> classPath, String fileName, String source) throws IOException {
File outputDir = temp.newFolder();
List<Processor> processors = Collections.emptyList();
StubJarGeneratingProcessor stubJarGenerator = null;
if (testingMode != MODE_JAR_BASED) {
stubJarGenerator = new StubJarGeneratingProcessor(filesystem, outputDir.toPath().resolve("sourceStub.jar"), SourceVersion.RELEASE_8);
processors = Collections.singletonList(stubJarGenerator);
}
Path fullJar = compileToJar(testingMode != MODE_SOURCE_BASED_MISSING_DEPS ? classPath : Collections.emptySortedSet(), processors, fileName, source, outputDir);
Path stubJar;
if (stubJarGenerator != null) {
stubJar = stubJarGenerator.getStubJarPath();
} else {
stubJar = createStubJar(fullJar);
}
return new JarPaths(fullJar, stubJar);
}
use of javax.annotation.processing.Processor in project j2objc by google.
the class AnnotationPreProcessor method hasAnnotationProcessors.
/**
* Check whether any javax.annotation.processing.Processor services are defined on
* the declared classpath. This is checked here to avoid batch compiling sources
* in case any might have annotations that should be processed.
*/
private boolean hasAnnotationProcessors() {
PathClassLoader loader = new PathClassLoader(options.fileUtil().getClassPathEntries());
loader.addPaths(options.getProcessorPathEntries());
ServiceLoader<Processor> serviceLoader = ServiceLoader.load(Processor.class, loader);
Iterator<Processor> iterator = serviceLoader.iterator();
return iterator.hasNext();
}
use of javax.annotation.processing.Processor in project mapstruct by mapstruct.
the class JdkCompilingStatement method compileWithSpecificCompiler.
@Override
protected CompilationOutcomeDescriptor compileWithSpecificCompiler(CompilationRequest compilationRequest, String sourceOutputDir, String classOutputDir, String additionalCompilerClasspath) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(getSourceFiles(compilationRequest.getSourceClasses()));
try {
fileManager.setLocation(StandardLocation.CLASS_PATH, COMPILER_CLASSPATH_FILES);
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(classOutputDir)));
fileManager.setLocation(StandardLocation.SOURCE_OUTPUT, Arrays.asList(new File(sourceOutputDir)));
} catch (IOException e) {
throw new RuntimeException(e);
}
ClassLoader processorClassloader;
if (additionalCompilerClasspath == null) {
processorClassloader = DEFAULT_PROCESSOR_CLASSLOADER;
} else {
processorClassloader = new ModifiableURLClassLoader(new FilteringParentClassLoader("org.mapstruct.")).withPaths(PROCESSOR_CLASSPATH).withPath(additionalCompilerClasspath).withOriginsOf(compilationRequest.getServices().values());
}
CompilationTask task = compiler.getTask(null, fileManager, diagnostics, compilationRequest.getProcessorOptions(), null, compilationUnits);
task.setProcessors(Arrays.asList((Processor) loadAndInstantiate(processorClassloader, MappingProcessor.class)));
boolean compilationSuccessful = task.call();
return CompilationOutcomeDescriptor.forResult(SOURCE_DIR, compilationSuccessful, diagnostics.getDiagnostics());
}
use of javax.annotation.processing.Processor in project btrace by btraceio.
the class Compiler method compile.
private Map<String, byte[]> compile(MemoryJavaFileManager manager, Iterable<? extends JavaFileObject> compUnits, Writer err, String sourcePath, final String classPath) {
// to collect errors, warnings etc.
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
// javac options
List<String> options = new ArrayList<>();
options.add("-Xlint:all");
options.add("-g:lines");
options.add("-deprecation");
options.add("-source");
options.add("1.7");
options.add("-target");
options.add("1.7");
if (sourcePath != null) {
options.add("-sourcepath");
options.add(sourcePath);
}
if (classPath != null) {
options.add("-classpath");
options.add(classPath);
}
// create a compilation task
JavacTask task = (JavacTask) compiler.getTask(err, manager, diagnostics, options, null, compUnits);
Verifier btraceVerifier = new Verifier();
task.setTaskListener(btraceVerifier);
// we add BTrace Verifier as a (JSR 269) Processor
List<Processor> processors = new ArrayList<>(1);
processors.add(btraceVerifier);
task.setProcessors(processors);
final PrintWriter perr = (err instanceof PrintWriter) ? (PrintWriter) err : new PrintWriter(err);
// print dignostics messages in case of failures.
if (task.call() == false || containsErrors(diagnostics)) {
for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
printDiagnostic(diagnostic, perr);
}
perr.flush();
return null;
}
// collect .class bytes of all compiled classes
Map<String, byte[]> result = new HashMap<>();
try {
Map<String, byte[]> classBytes = manager.getClassBytes();
List<String> classNames = btraceVerifier.getClassNames();
for (String name : classNames) {
if (classBytes.containsKey(name)) {
dump(name + "_before", classBytes.get(name));
ClassReader cr = new ClassReader(classBytes.get(name));
ClassWriter cw = new CompilerClassWriter(classPath, perr);
cr.accept(new Postprocessor(cw), ClassReader.EXPAND_FRAMES + ClassReader.SKIP_DEBUG);
byte[] classData = cw.toByteArray();
dump(name + "_after", classData);
if (generatePack) {
// temp hack; need turn off verifier
SharedSettings.GLOBAL.setTrusted(true);
BTraceProbeNode bpn = (BTraceProbeNode) new BTraceProbeFactory(SharedSettings.GLOBAL).createProbe(classData);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (DataOutputStream dos = new DataOutputStream(bos)) {
BTraceProbePersisted bpp = BTraceProbePersisted.from(bpn);
bpp.write(dos);
}
classData = bos.toByteArray();
}
result.put(name, classData);
}
}
} catch (IOException e) {
e.printStackTrace(perr);
} finally {
try {
manager.close();
} catch (IOException exp) {
}
}
return result;
}
use of javax.annotation.processing.Processor in project auto by google.
the class GeneratedDoesNotExistTest method test.
@Test
public void test() {
JavaFileObject javaFileObject = JavaFileObjects.forSourceLines("foo.bar.Baz", "package foo.bar;", "", "import com.google.auto.value.AutoValue;", "", "@AutoValue", "public abstract class Baz {", " public static Baz create() {", " return new AutoValue_Baz();", " }", "}");
JavaFileObject expectedOutput = JavaFileObjects.forSourceLines("foo.bar.AutoValue_Baz", "package foo.bar;", "", "final class AutoValue_Baz extends Baz {", " AutoValue_Baz() {", " }", "", " @Override public String toString() {", " return \"Baz{\"", " + \"}\";", " }", "", " @Override public boolean equals(Object o) {", " if (o == this) {", " return true;", " }", " if (o instanceof Baz) {", " return true;", " }", " return false;", " }", "", " @Override public int hashCode() {", " int h$ = 1;", " return h$;", " }", "}");
Set<String> ignoredGenerated = ConcurrentHashMap.newKeySet();
Processor autoValueProcessor = new AutoValueProcessor();
ProcessorHandler handler = new ProcessorHandler(autoValueProcessor, ignoredGenerated);
Processor noGeneratedProcessor = partialProxy(Processor.class, handler);
Compilation compilation = javac().withOptions(javacOptions).withProcessors(noGeneratedProcessor).compile(javaFileObject);
assertThat(compilation).succeededWithoutWarnings();
assertThat(compilation).generatedSourceFile("foo.bar.AutoValue_Baz").hasSourceEquivalentTo(expectedOutput);
assertThat(ignoredGenerated).containsExactly(expectedAnnotation);
}
Aggregations