diff --git a/pom.xml b/pom.xml index ba046f19..25f13637 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,14 @@ 1.3.9 true + + + + org.eclipse.jdt.core.compiler + ecj + 4.4.2 + test + diff --git a/src/main/java/com/google/testing/compile/Compilation.java b/src/main/java/com/google/testing/compile/Compilation.java index af8ba4c3..f35d93ba 100644 --- a/src/main/java/com/google/testing/compile/Compilation.java +++ b/src/main/java/com/google/testing/compile/Compilation.java @@ -55,9 +55,8 @@ private Compilation() {} * * @throws RuntimeException if compilation fails. */ - static Result compile(Iterable processors, + static Result compile(JavaCompiler compiler, Iterable processors, Set options, Iterable sources) { - JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); if (compiler == null) { throw new IllegalStateException("Java Compiler is not present. " + "May be, you need to include tools.jar on your dependency list."); @@ -71,7 +70,7 @@ static Result compile(Iterable processors, fileManager, diagnosticCollector, ImmutableSet.copyOf(options), - ImmutableSet.of(), + null, // explicitly use the default behaviour because Eclipse compiler fails with empty Set sources); task.setProcessors(processors); boolean successful = task.call(); @@ -83,8 +82,7 @@ static Result compile(Iterable processors, * Parse {@code sources} into {@linkplain CompilationUnitTree compilation units}. This method * does not compile the sources. */ - static ParseResult parse(Iterable sources) { - JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + static ParseResult parse(JavaCompiler compiler, Iterable sources) { DiagnosticCollector diagnosticCollector = new DiagnosticCollector(); InMemoryJavaFileManager fileManager = new InMemoryJavaFileManager( @@ -94,7 +92,7 @@ static ParseResult parse(Iterable sources) { fileManager, diagnosticCollector, ImmutableSet.of(), - ImmutableSet.of(), + null, // explicitly use the default behaviour because Eclipse compiler fails with empty Set sources); try { Iterable parsedCompilationUnits = task.parse(); diff --git a/src/main/java/com/google/testing/compile/CompilationRule.java b/src/main/java/com/google/testing/compile/CompilationRule.java index 0be82843..f8fa94ed 100644 --- a/src/main/java/com/google/testing/compile/CompilationRule.java +++ b/src/main/java/com/google/testing/compile/CompilationRule.java @@ -37,6 +37,7 @@ import javax.lang.model.element.TypeElement; import javax.lang.model.util.Elements; import javax.lang.model.util.Types; +import javax.tools.ToolProvider; /** * A {@link JUnit4} {@link Rule} that executes tests such that a instances of {@link Elements} and @@ -56,7 +57,8 @@ public Statement apply(final Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { final AtomicReference thrown = new AtomicReference(); - Result result = Compilation.compile(ImmutableList.of(new AbstractProcessor() { + Result result = Compilation.compile(ToolProvider.getSystemJavaCompiler(), + ImmutableList.of(new AbstractProcessor() { @Override public SourceVersion getSupportedSourceVersion() { return SourceVersion.latest(); diff --git a/src/main/java/com/google/testing/compile/JavaSourcesSubject.java b/src/main/java/com/google/testing/compile/JavaSourcesSubject.java index 0ddd83e7..670bb3e6 100644 --- a/src/main/java/com/google/testing/compile/JavaSourcesSubject.java +++ b/src/main/java/com/google/testing/compile/JavaSourcesSubject.java @@ -44,11 +44,8 @@ import java.util.Set; import javax.annotation.processing.Processor; -import javax.tools.Diagnostic; +import javax.tools.*; import javax.tools.Diagnostic.Kind; -import javax.tools.FileObject; -import javax.tools.JavaFileManager; -import javax.tools.JavaFileObject; /** * A Truth {@link Subject} that evaluates the result @@ -59,13 +56,20 @@ @SuppressWarnings("restriction") // Sun APIs usage intended public final class JavaSourcesSubject extends Subject> - implements CompileTester, ProcessedCompileTesterFactory { + implements ProcessedCompileTesterFactory { private final Set options = Sets.newHashSet(); + private JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); JavaSourcesSubject(FailureStrategy failureStrategy, Iterable subject) { super(failureStrategy, subject); } - + + @Override + public ProcessedCompileTesterFactory withCompiler(JavaCompiler javaCompiler) { + this.compiler = javaCompiler; + return this; + } + @Override public ProcessedCompileTesterFactory withCompilerOptions(Iterable options) { Iterables.addAll(this.options, options); @@ -155,7 +159,7 @@ private String reportFilesGenerated(Compilation.Result result) { @Override public void parsesAs(JavaFileObject first, JavaFileObject... rest) { - Compilation.ParseResult actualResult = Compilation.parse(getSubject()); + Compilation.ParseResult actualResult = Compilation.parse(compiler, getSubject()); ImmutableList> errors = actualResult.diagnosticsByKind().get(Kind.ERROR); if (!errors.isEmpty()) { @@ -166,7 +170,7 @@ public void parsesAs(JavaFileObject first, JavaFileObject... rest) { } failureStrategy.fail(message.toString()); } - final Compilation.ParseResult expectedResult = Compilation.parse(Lists.asList(first, rest)); + final Compilation.ParseResult expectedResult = Compilation.parse(compiler, Lists.asList(first, rest)); final FluentIterable actualTrees = FluentIterable.from( actualResult.compilationUnits()); final FluentIterable expectedTrees = FluentIterable.from( @@ -280,7 +284,7 @@ private void failWithCandidate(JavaFileObject expectedSource, @Override public SuccessfulCompilationClause compilesWithoutError() { Compilation.Result result = - Compilation.compile(processors, ImmutableSet.copyOf(options), getSubject()); + Compilation.compile(compiler, processors, ImmutableSet.copyOf(options), getSubject()); if (!result.successful()) { ImmutableList> errors = result.diagnosticsByKind().get(Kind.ERROR); @@ -298,7 +302,7 @@ public SuccessfulCompilationClause compilesWithoutError() { @Override public UnsuccessfulCompilationClause failsToCompile() { - Result result = Compilation.compile(processors, ImmutableSet.copyOf(options), getSubject()); + Result result = Compilation.compile(compiler, processors, ImmutableSet.copyOf(options), getSubject()); if (result.successful()) { String message = Joiner.on('\n').join( "Compilation was expected to fail, but contained no errors.", @@ -571,7 +575,7 @@ public SuccessfulFileClause withContents(ByteSource expectedByteSource) { public static final class SingleSourceAdapter extends Subject - implements CompileTester, ProcessedCompileTesterFactory { + implements ProcessedCompileTesterFactory { private final JavaSourcesSubject delegate; SingleSourceAdapter(FailureStrategy failureStrategy, JavaFileObject subject) { @@ -579,7 +583,11 @@ public static final class SingleSourceAdapter this.delegate = new JavaSourcesSubject(failureStrategy, ImmutableList.of(subject)); } - + + public ProcessedCompileTesterFactory withCompiler(JavaCompiler javaCompiler) { + return this.delegate.withCompiler(javaCompiler); + } + @Override public ProcessedCompileTesterFactory withCompilerOptions(Iterable options) { return delegate.withCompilerOptions(options); diff --git a/src/main/java/com/google/testing/compile/MoreTrees.java b/src/main/java/com/google/testing/compile/MoreTrees.java index e3833895..2cb2f543 100644 --- a/src/main/java/com/google/testing/compile/MoreTrees.java +++ b/src/main/java/com/google/testing/compile/MoreTrees.java @@ -38,6 +38,7 @@ import java.util.Arrays; import javax.annotation.Nullable; +import javax.tools.ToolProvider; /** * A class containing methods which are useful for gaining access to {@code Tree} instances from @@ -52,8 +53,8 @@ static CompilationUnitTree parseLinesToTree(String... source) { /** Parses the source given into a {@link CompilationUnitTree}. */ static CompilationUnitTree parseLinesToTree(Iterable source) { - Iterable parseResults = Compilation.parse(ImmutableList.of( - JavaFileObjects.forSourceLines("", source))).compilationUnits(); + Iterable parseResults = Compilation.parse(ToolProvider.getSystemJavaCompiler(), + ImmutableList.of(JavaFileObjects.forSourceLines("", source))).compilationUnits(); return Iterables.getOnlyElement(parseResults); } @@ -64,7 +65,8 @@ static Compilation.ParseResult parseLines(String... source) { /** Parses the source given and produces a {@link Compilation.ParseResult}. */ static Compilation.ParseResult parseLines(Iterable source) { - return Compilation.parse(ImmutableList.of(JavaFileObjects.forSourceLines("", source))); + return Compilation.parse(ToolProvider.getSystemJavaCompiler(), + ImmutableList.of(JavaFileObjects.forSourceLines("", source))); } /** diff --git a/src/main/java/com/google/testing/compile/ProcessedCompileTesterFactory.java b/src/main/java/com/google/testing/compile/ProcessedCompileTesterFactory.java index 3c5ac158..92b46241 100644 --- a/src/main/java/com/google/testing/compile/ProcessedCompileTesterFactory.java +++ b/src/main/java/com/google/testing/compile/ProcessedCompileTesterFactory.java @@ -17,6 +17,7 @@ import javax.annotation.CheckReturnValue; import javax.annotation.processing.Processor; +import javax.tools.JavaCompiler; /** * Creates {@link CompileTester} instances that test compilation with provided {@link Processor} @@ -24,7 +25,11 @@ * * @author Gregory Kick */ -public interface ProcessedCompileTesterFactory { +public interface ProcessedCompileTesterFactory extends CompileTester{ + + /** Specify compiler (Javac, Eclipse ECJ, ...) **/ + @CheckReturnValue + ProcessedCompileTesterFactory withCompiler(JavaCompiler var1); /** Adds options that will be passed to the compiler. */ @CheckReturnValue ProcessedCompileTesterFactory withCompilerOptions(Iterable options); diff --git a/src/test/java/com/google/testing/compile/JavaSourcesSubjectFactoryTest.java b/src/test/java/com/google/testing/compile/JavaSourcesSubjectFactoryTest.java index f6a010a9..7c55d08d 100644 --- a/src/test/java/com/google/testing/compile/JavaSourcesSubjectFactoryTest.java +++ b/src/test/java/com/google/testing/compile/JavaSourcesSubjectFactoryTest.java @@ -29,6 +29,7 @@ import com.google.common.truth.FailureStrategy; import com.google.common.truth.TestVerb; +import org.eclipse.jdt.internal.compiler.tool.EclipseCompiler; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -81,6 +82,15 @@ public void compilesWithoutError() { .compilesWithoutError(); } + @Test + public void compilesWithoutErrorWithEclipseCompiler() { + assertAbout(javaSource()) + .that(JavaFileObjects.forResource(Resources.getResource("HelloWorld.java"))) + .withCompiler(new EclipseCompiler()) + .withCompilerOptions("-nowarn", "-1.6") + .compilesWithoutError(); + } + @Test public void compilesWithoutError_failureReportsFiles() { try { @@ -116,16 +126,16 @@ public void compilesWithoutError_exceptionCreatedOrPassedThrough() { VERIFY.about(javaSource()) .that(JavaFileObjects.forResource("HelloWorld.java")) .processedWith(new AbstractProcessor() { - @Override - public Set getSupportedAnnotationTypes() { - return ImmutableSet.of("*"); - } - - @Override - public boolean process(Set annotations, - RoundEnvironment roundEnv) { - throw e; - } + @Override + public Set getSupportedAnnotationTypes() { + return ImmutableSet.of("*"); + } + + @Override + public boolean process(Set annotations, + RoundEnvironment roundEnv) { + throw e; + } }) .compilesWithoutError(); fail();