Java Code Examples for javax.tools.StandardJavaFileManager#getClassLoader()

The following examples show how to use javax.tools.StandardJavaFileManager#getClassLoader() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: bazel   File: VanillaJavaBuilder.java    License: Apache License 2.0 6 votes vote down vote up
/** Sets the compilation's annotation processors. */
private static void setProcessors(
    OptionsParser optionsParser, StandardJavaFileManager fileManager, CompilationTask task) {
  ClassLoader processorLoader =
      fileManager.getClassLoader(StandardLocation.ANNOTATION_PROCESSOR_PATH);
  ImmutableList.Builder<Processor> processors = ImmutableList.builder();
  for (String processor : optionsParser.getProcessorNames()) {
    try {
      processors.add(
          (Processor) processorLoader.loadClass(processor).getConstructor().newInstance());
    } catch (ReflectiveOperationException e) {
      throw new LinkageError(e.getMessage(), e);
    }
  }
  task.setProcessors(processors.build());
}
 
Example 2
public AnnotationProcessorManager(CompilerBuildContext context, ProcessingEnvImpl processingEnv, StandardJavaFileManager fileManager, String[] processors, CompilerJdt incrementalCompiler) {
  this.context = context;
  this._processingEnv = processingEnv;
  this.incrementalCompiler = incrementalCompiler;
  ClassLoader procLoader = fileManager.getClassLoader(StandardLocation.ANNOTATION_PROCESSOR_PATH);
  this.processors = processors != null //
      ? new SpecifiedProcessors(procLoader, processors) //
      : new DiscoveredProcessors(procLoader);
  processingEnv.addReferencedTypeObserver(this::recordReferencedType);
}
 
Example 3
Source Project: javapoet   File: FileReadingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void compileJavaFile() throws Exception {
  final String value = "Hello World!";
  TypeSpec type = TypeSpec.classBuilder("Test")
      .addModifiers(Modifier.PUBLIC)
      .addSuperinterface(ParameterizedTypeName.get(Callable.class, String.class))
      .addMethod(MethodSpec.methodBuilder("call")
          .returns(String.class)
          .addModifiers(Modifier.PUBLIC)
          .addStatement("return $S", value)
          .build())
      .build();
  JavaFile javaFile = JavaFile.builder("foo", type).build();

  JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
  StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, 
      Locale.getDefault(), UTF_8);
  fileManager.setLocation(StandardLocation.CLASS_OUTPUT,
      Collections.singleton(temporaryFolder.newFolder()));
  CompilationTask task = compiler.getTask(null, 
      fileManager,
      diagnosticCollector,
      Collections.emptySet(),
      Collections.emptySet(),
      Collections.singleton(javaFile.toJavaFileObject()));
  
  assertThat(task.call()).isTrue();
  assertThat(diagnosticCollector.getDiagnostics()).isEmpty();

  ClassLoader loader = fileManager.getClassLoader(StandardLocation.CLASS_OUTPUT);
  Callable<?> test = Class.forName("foo.Test", true, loader)
          .asSubclass(Callable.class)
          .getDeclaredConstructor()
          .newInstance();
  assertThat(Callable.class.getMethod("call").invoke(test)).isEqualTo(value);
}