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

The following examples show how to use javax.tools.StandardJavaFileManager#getClassLoader() . 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 File: VanillaJavaBuilder.java    From bazel with 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
Source File: AnnotationProcessorManager.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
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 File: FileReadingTest.java    From javapoet with 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);
}