javax.tools.StandardJavaFileManager#getClassLoader ( )源码实例Demo

下面列出了javax.tools.StandardJavaFileManager#getClassLoader ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: bazel   文件: VanillaJavaBuilder.java
/** 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());
}
 
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);
}
 
源代码3 项目: javapoet   文件: FileReadingTest.java
@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);
}