org.eclipse.jdt.internal.compiler.tool.EclipseCompiler Java Examples

The following examples show how to use org.eclipse.jdt.internal.compiler.tool.EclipseCompiler. 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: OverridesTest.java    From auto with Apache License 2.0 6 votes vote down vote up
private void evaluate(File dummySourceFile) throws Throwable {
  JavaCompiler compiler = new EclipseCompiler();
  StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, UTF_8);
  // This hack is only needed in a Google-internal Java 8 environment where symbolic links make
  // it hard for ecj to find the boot class path. Elsewhere it is unnecessary but harmless.
  File rtJar = new File(StandardSystemProperty.JAVA_HOME.value() + "/lib/rt.jar");
  if (rtJar.exists()) {
    List<File> bootClassPath = ImmutableList.<File>builder()
        .add(rtJar)
        .addAll(fileManager.getLocation(StandardLocation.PLATFORM_CLASS_PATH))
        .build();
    fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, bootClassPath);
  }
  Iterable<? extends JavaFileObject> sources = fileManager.getJavaFileObjects(dummySourceFile);
  JavaCompiler.CompilationTask task =
      compiler.getTask(null, fileManager, null, null, null, sources);
  EcjTestProcessor processor = new EcjTestProcessor(statement);
  task.setProcessors(ImmutableList.of(processor));
  assertThat(task.call()).isTrue();
  processor.maybeThrow();
}
 
Example #2
Source File: JavaCompilerProvider.java    From lambdaFromString with MIT License 5 votes vote down vote up
static Optional<JavaCompiler> getEclipseJavaCompiler() {
    try {
        return Optional.of(new EclipseCompiler());
    } catch (NoClassDefFoundError err) {
        return Optional.empty();
    }
}
 
Example #3
Source File: LambdaFactoryConfigurationTest.java    From lambdaFromString with MIT License 5 votes vote down vote up
@Test
public void usingWithSetsParameters(){
    HelperClassSourceProvider helper = new DefaultHelperClassSourceProvider();
    ClassFactory classFactory = new DefaultClassFactory();
    String[] staticImports = new String[]{"si1","si2"};
    String[] imports = new String[]{"i1","i2"};
    String compilationClassPath = "compilationClassPath";
    ClassLoader parentClassLoader = new URLClassLoader(new URL[]{});
    JavaCompiler javaCompiler = new EclipseCompiler();

    LambdaFactoryConfiguration changedConfiguration = LambdaFactoryConfiguration.get()
            .withHelperClassSourceProvider(helper)
            .withClassFactory(classFactory)
            .withStaticImports(staticImports)
            .withImports(imports)
            .withCompilationClassPath(compilationClassPath)
            .withParentClassLoader(parentClassLoader)
            .withJavaCompiler(javaCompiler);


    assertSame(helper, changedConfiguration.getDefaultHelperClassSourceProvider());
    assertSame(classFactory, changedConfiguration.getClassFactory());
    assertEquals(Arrays.asList(staticImports), changedConfiguration.getStaticImports());
    assertEquals(Arrays.asList(imports), changedConfiguration.getImports());
    assertEquals(compilationClassPath, changedConfiguration.getCompilationClassPath());
    assertSame(parentClassLoader, changedConfiguration.getParentClassLoader());
    assertSame(javaCompiler, changedConfiguration.getJavaCompiler());

}
 
Example #4
Source File: LambdaFactoryTest.java    From lambdaFromString with MIT License 5 votes vote down vote up
@Parameterized.Parameters(name = "{1}")
public static Iterable<Object[]> factories(){
    JavaCompiler jdkCompiler = JavaCompilerProvider.getJdkJavaCompiler().get();
    JavaCompiler eclipseCompiler = new EclipseCompiler();
    return Arrays.asList(new Object[][]{
            {eclipseCompiler, "Default (Eclipse Compiler)"},
            {jdkCompiler, "JDK Compiler"}
    });
}
 
Example #5
Source File: TypesEclipseTest.java    From javapoet with Apache License 2.0 5 votes vote down vote up
static private boolean compile(Iterable<? extends Processor> processors) {
  JavaCompiler compiler = new EclipseCompiler();
  DiagnosticCollector<JavaFileObject> diagnosticCollector =
      new DiagnosticCollector<>();
  JavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, Locale.getDefault(), UTF_8);
  JavaCompiler.CompilationTask task = compiler.getTask(
      null,
      fileManager,
      diagnosticCollector,
      ImmutableSet.of(),
      ImmutableSet.of(TypesEclipseTest.class.getCanonicalName()),
      ImmutableSet.of());
  task.setProcessors(processors);
  return task.call();
}
 
Example #6
Source File: CompileWithEclipseTest.java    From auto with Apache License 2.0 4 votes vote down vote up
private void compileWithEclipse(String version, Predicate<File> predicate) throws IOException {
  File sourceRootFile = new File(SOURCE_ROOT);
  File javaDir = new File(sourceRootFile, "src/main/java");
  File javatestsDir = new File(sourceRootFile, "src/test/java");
  Set<File> sources =
      new ImmutableSet.Builder<File>()
          .addAll(filesUnderDirectory(javaDir, predicate))
          .addAll(filesUnderDirectory(javatestsDir, predicate))
          .build();
  assertThat(sources).isNotEmpty();
  JavaCompiler compiler = new EclipseCompiler();
  StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
  // This hack is only needed in a Google-internal Java 8 environment where symbolic links make it
  // hard for ecj to find the boot class path. Elsewhere it is unnecessary but harmless. Notably,
  // on Java 9+ there is no rt.jar. There, fileManager.getLocation(PLATFORM_CLASS_PATH) returns
  // null, because the relevant classes are in modules inside
  // fileManager.getLocation(SYSTEM_MODULES).
  File rtJar = new File(JAVA_HOME.value() + "/lib/rt.jar");
  if (rtJar.exists()) {
    List<File> bootClassPath = ImmutableList.<File>builder()
        .add(rtJar)
        .addAll(fileManager.getLocation(StandardLocation.PLATFORM_CLASS_PATH))
        .build();
    fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, bootClassPath);
  }
  Iterable<? extends JavaFileObject> sourceFileObjects =
      fileManager.getJavaFileObjectsFromFiles(sources);
  String outputDir = tmp.getRoot().toString();
  ImmutableList<String> options =
      ImmutableList.of("-d", outputDir, "-s", outputDir, "-source", version, "-target", version);
  JavaCompiler.CompilationTask task =
      compiler.getTask(null, fileManager, null, options, null, sourceFileObjects);
  // Explicitly supply an empty list of extensions for AutoValueProcessor, because otherwise this
  // test will pick up a test one and get confused.
  AutoValueProcessor autoValueProcessor = new AutoValueProcessor(ImmutableList.of());
  ImmutableList<? extends Processor> processors =
      ImmutableList.of(
          autoValueProcessor, new AutoOneOfProcessor(), new AutoAnnotationProcessor());
  task.setProcessors(processors);
  assertWithMessage("Compilation should succeed").that(task.call()).isTrue();
}