Search in sources :

Example 1 with Processor

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);
}
Also used : Path(java.nio.file.Path) Processor(javax.annotation.processing.Processor) File(java.io.File)

Example 2 with Processor

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();
}
Also used : PathClassLoader(com.google.devtools.j2objc.util.PathClassLoader) Processor(javax.annotation.processing.Processor)

Example 3 with Processor

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());
}
Also used : Processor(javax.annotation.processing.Processor) MappingProcessor(org.mapstruct.ap.MappingProcessor) JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) CompilationTask(javax.tools.JavaCompiler.CompilationTask) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) MappingProcessor(org.mapstruct.ap.MappingProcessor) DiagnosticCollector(javax.tools.DiagnosticCollector) File(java.io.File)

Example 4 with Processor

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;
}
Also used : Processor(javax.annotation.processing.Processor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Diagnostic(javax.tools.Diagnostic) JavaFileObject(javax.tools.JavaFileObject) DiagnosticCollector(javax.tools.DiagnosticCollector) JavacTask(com.sun.source.util.JavacTask) ClassWriter(com.sun.btrace.org.objectweb.asm.ClassWriter) BTraceProbeFactory(com.sun.btrace.runtime.BTraceProbeFactory) BTraceProbePersisted(com.sun.btrace.runtime.BTraceProbePersisted) ClassReader(com.sun.btrace.org.objectweb.asm.ClassReader) BTraceProbeNode(com.sun.btrace.runtime.BTraceProbeNode)

Example 5 with Processor

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);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Processor(javax.annotation.processing.Processor) Compilation(com.google.testing.compile.Compilation) Test(org.junit.Test)

Aggregations

Processor (javax.annotation.processing.Processor)9 ArrayList (java.util.ArrayList)5 JavaFileObject (javax.tools.JavaFileObject)4 File (java.io.File)3 IOException (java.io.IOException)3 DiagnosticCollector (javax.tools.DiagnosticCollector)3 PathClassLoader (com.google.devtools.j2objc.util.PathClassLoader)2 JavaCompiler (javax.tools.JavaCompiler)2 CompilationTask (javax.tools.JavaCompiler.CompilationTask)2 StandardJavaFileManager (javax.tools.StandardJavaFileManager)2 AggregatingProcessor (org.gradle.api.internal.tasks.compile.processing.AggregatingProcessor)2 AnnotationProcessorDeclaration (org.gradle.api.internal.tasks.compile.processing.AnnotationProcessorDeclaration)2 IsolatingProcessor (org.gradle.api.internal.tasks.compile.processing.IsolatingProcessor)2 InputFile (com.google.devtools.j2objc.file.InputFile)1 RegularInputFile (com.google.devtools.j2objc.file.RegularInputFile)1 ProcessingContext (com.google.devtools.j2objc.pipeline.ProcessingContext)1 Compilation (com.google.testing.compile.Compilation)1 ClassReader (com.sun.btrace.org.objectweb.asm.ClassReader)1 ClassWriter (com.sun.btrace.org.objectweb.asm.ClassWriter)1 BTraceProbeFactory (com.sun.btrace.runtime.BTraceProbeFactory)1