Skip to content

Commit c09dd40

Browse files
doanduyhaitbroyer
authored andcommitted
Support alternate JavaCompilers (such as ECJ) in JavaSourcesSubject
1 parent 7a21219 commit c09dd40

6 files changed

Lines changed: 71 additions & 43 deletions

File tree

src/main/java/com/google/testing/compile/Compilation.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ private Compilation() {}
5454
*
5555
* @throws RuntimeException if compilation fails.
5656
*/
57-
static Result compile(Iterable<? extends Processor> processors,
57+
static Result compile(JavaCompiler compiler, Iterable<? extends Processor> processors,
5858
Iterable<String> options, Iterable<? extends JavaFileObject> sources) {
59-
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
6059
if (compiler == null) {
6160
throw new IllegalStateException("Java Compiler is not present. "
6261
+ "May be, you need to include tools.jar on your dependency list.");
@@ -70,7 +69,7 @@ static Result compile(Iterable<? extends Processor> processors,
7069
fileManager,
7170
diagnosticCollector,
7271
ImmutableList.copyOf(options),
73-
ImmutableSet.<String>of(),
72+
null, // explicitly use the default behaviour because Eclipse compiler fails with empty Set
7473
sources);
7574
task.setProcessors(processors);
7675
boolean successful = task.call();
@@ -82,8 +81,7 @@ static Result compile(Iterable<? extends Processor> processors,
8281
* Parse {@code sources} into {@linkplain CompilationUnitTree compilation units}. This method
8382
* <b>does not</b> compile the sources.
8483
*/
85-
static ParseResult parse(Iterable<? extends JavaFileObject> sources) {
86-
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
84+
static ParseResult parse(JavaCompiler compiler, Iterable<? extends JavaFileObject> sources) {
8785
DiagnosticCollector<JavaFileObject> diagnosticCollector =
8886
new DiagnosticCollector<JavaFileObject>();
8987
InMemoryJavaFileManager fileManager = new InMemoryJavaFileManager(
@@ -93,7 +91,7 @@ static ParseResult parse(Iterable<? extends JavaFileObject> sources) {
9391
fileManager,
9492
diagnosticCollector,
9593
ImmutableSet.<String>of(),
96-
ImmutableSet.<String>of(),
94+
null, // explicitly use the default behaviour because Eclipse compiler fails with empty Set
9795
sources);
9896
try {
9997
Iterable<? extends CompilationUnitTree> parsedCompilationUnits = task.parse();

src/main/java/com/google/testing/compile/CompilationRule.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -87,37 +87,37 @@ public Statement apply(final Statement base, Description description) {
8787
CompilationTask task = compiler.getTask(null, fileManager, diagnosticCollector, null,
8888
ImmutableSet.of(CompilationRule.class.getCanonicalName()), null);
8989
task.setProcessors(ImmutableList.of(new AbstractProcessor() {
90-
@Override
91-
public SourceVersion getSupportedSourceVersion() {
92-
return SourceVersion.latest();
93-
}
90+
@Override
91+
public SourceVersion getSupportedSourceVersion() {
92+
return SourceVersion.latest();
93+
}
9494

95-
@Override
96-
public Set<String> getSupportedAnnotationTypes() {
97-
return ImmutableSet.of("*");
98-
}
95+
@Override
96+
public Set<String> getSupportedAnnotationTypes() {
97+
return ImmutableSet.of("*");
98+
}
9999

100-
@Override
101-
public synchronized void init(ProcessingEnvironment processingEnv) {
102-
super.init(processingEnv);
103-
elements = processingEnv.getElementUtils();
104-
types = processingEnv.getTypeUtils();
105-
}
100+
@Override
101+
public synchronized void init(ProcessingEnvironment processingEnv) {
102+
super.init(processingEnv);
103+
elements = processingEnv.getElementUtils();
104+
types = processingEnv.getTypeUtils();
105+
}
106106

107-
@Override
108-
public boolean process(Set<? extends TypeElement> annotations,
109-
RoundEnvironment roundEnv) {
110-
// just run the test on the last round after compilation is over
111-
if (roundEnv.processingOver()) {
112-
try {
113-
base.evaluate();
114-
} catch (Throwable e) {
115-
thrown.set(e);
107+
@Override
108+
public boolean process(Set<? extends TypeElement> annotations,
109+
RoundEnvironment roundEnv) {
110+
// just run the test on the last round after compilation is over
111+
if (roundEnv.processingOver()) {
112+
try {
113+
base.evaluate();
114+
} catch (Throwable e) {
115+
thrown.set(e);
116+
}
117+
}
118+
return false;
116119
}
117-
}
118-
return false;
119-
}
120-
}));
120+
}));
121121
boolean successful = task.call();
122122
checkState(successful);
123123
Throwable t = thrown.get();

src/main/java/com/google/testing/compile/JavaSourcesSubject.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@
4848
import javax.tools.Diagnostic;
4949
import javax.tools.Diagnostic.Kind;
5050
import javax.tools.FileObject;
51+
import javax.tools.JavaCompiler;
5152
import javax.tools.JavaFileManager;
5253
import javax.tools.JavaFileObject;
54+
import javax.tools.ToolProvider;
5355

5456
/**
5557
* A <a href="https://github.com/truth0/truth">Truth</a> {@link Subject} that evaluates the result
@@ -60,12 +62,19 @@
6062
@SuppressWarnings("restriction") // Sun APIs usage intended
6163
public final class JavaSourcesSubject
6264
extends Subject<JavaSourcesSubject, Iterable<? extends JavaFileObject>>
63-
implements CompileTester, ProcessedCompileTesterFactory {
65+
implements ProcessedCompileTesterFactory {
6466
private final List<String> options = new ArrayList<String>(Arrays.asList("-Xlint"));
67+
private JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
6568

6669
JavaSourcesSubject(FailureStrategy failureStrategy, Iterable<? extends JavaFileObject> subject) {
6770
super(failureStrategy, subject);
6871
}
72+
73+
@Override
74+
public JavaSourcesSubject withCompiler(JavaCompiler javaCompiler) {
75+
this.compiler = javaCompiler;
76+
return this;
77+
}
6978

7079
@Override
7180
public JavaSourcesSubject withCompilerOptions(Iterable<String> options) {
@@ -161,7 +170,7 @@ private String reportFilesGenerated(Compilation.Result result) {
161170

162171
@Override
163172
public void parsesAs(JavaFileObject first, JavaFileObject... rest) {
164-
Compilation.ParseResult actualResult = Compilation.parse(getSubject());
173+
Compilation.ParseResult actualResult = Compilation.parse(compiler, getSubject());
165174
ImmutableList<Diagnostic<? extends JavaFileObject>> errors =
166175
actualResult.diagnosticsByKind().get(Kind.ERROR);
167176
if (!errors.isEmpty()) {
@@ -172,7 +181,7 @@ public void parsesAs(JavaFileObject first, JavaFileObject... rest) {
172181
}
173182
failureStrategy.fail(message.toString());
174183
}
175-
final Compilation.ParseResult expectedResult = Compilation.parse(Lists.asList(first, rest));
184+
final Compilation.ParseResult expectedResult = Compilation.parse(compiler, Lists.asList(first, rest));
176185
final FluentIterable<? extends CompilationUnitTree> actualTrees = FluentIterable.from(
177186
actualResult.compilationUnits());
178187
final FluentIterable<? extends CompilationUnitTree> expectedTrees = FluentIterable.from(
@@ -295,7 +304,7 @@ public CleanCompilationClause compilesWithoutWarnings() {
295304

296305
private Compilation.Result successfulCompilationResult() {
297306
Compilation.Result result =
298-
Compilation.compile(processors, options, getSubject());
307+
Compilation.compile(compiler, processors, options, getSubject());
299308
if (!result.successful()) {
300309
ImmutableList<Diagnostic<? extends JavaFileObject>> errors =
301310
result.diagnosticsByKind().get(Kind.ERROR);
@@ -313,7 +322,7 @@ private Compilation.Result successfulCompilationResult() {
313322

314323
@Override
315324
public UnsuccessfulCompilationClause failsToCompile() {
316-
Result result = Compilation.compile(processors, options, getSubject());
325+
Result result = Compilation.compile(compiler, processors, options, getSubject());
317326
if (result.successful()) {
318327
String message = Joiner.on('\n').join(
319328
"Compilation was expected to fail, but contained no errors.",
@@ -748,14 +757,18 @@ public static JavaSourcesSubject assertThat(JavaFileObject... javaFileObjects) {
748757

749758
public static final class SingleSourceAdapter
750759
extends Subject<SingleSourceAdapter, JavaFileObject>
751-
implements CompileTester, ProcessedCompileTesterFactory {
760+
implements ProcessedCompileTesterFactory {
752761
private final JavaSourcesSubject delegate;
753762

754763
SingleSourceAdapter(FailureStrategy failureStrategy, JavaFileObject subject) {
755764
super(failureStrategy, subject);
756765
this.delegate =
757766
new JavaSourcesSubject(failureStrategy, ImmutableList.of(subject));
758767
}
768+
769+
public JavaSourcesSubject withCompiler(JavaCompiler javaCompiler) {
770+
return this.delegate.withCompiler(javaCompiler);
771+
}
759772

760773
@Override
761774
public JavaSourcesSubject withCompilerOptions(Iterable<String> options) {

src/main/java/com/google/testing/compile/MoreTrees.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Arrays;
3939

4040
import javax.annotation.Nullable;
41+
import javax.tools.ToolProvider;
4142

4243
/**
4344
* A class containing methods which are useful for gaining access to {@code Tree} instances from
@@ -52,8 +53,8 @@ static CompilationUnitTree parseLinesToTree(String... source) {
5253

5354
/** Parses the source given into a {@link CompilationUnitTree}. */
5455
static CompilationUnitTree parseLinesToTree(Iterable<String> source) {
55-
Iterable<? extends CompilationUnitTree> parseResults = Compilation.parse(ImmutableList.of(
56-
JavaFileObjects.forSourceLines("", source))).compilationUnits();
56+
Iterable<? extends CompilationUnitTree> parseResults = Compilation.parse(ToolProvider.getSystemJavaCompiler(),
57+
ImmutableList.of(JavaFileObjects.forSourceLines("", source))).compilationUnits();
5758
return Iterables.getOnlyElement(parseResults);
5859
}
5960

@@ -64,7 +65,8 @@ static Compilation.ParseResult parseLines(String... source) {
6465

6566
/** Parses the source given and produces a {@link Compilation.ParseResult}. */
6667
static Compilation.ParseResult parseLines(Iterable<String> source) {
67-
return Compilation.parse(ImmutableList.of(JavaFileObjects.forSourceLines("", source)));
68+
return Compilation.parse(ToolProvider.getSystemJavaCompiler(),
69+
ImmutableList.of(JavaFileObjects.forSourceLines("", source)));
6870
}
6971

7072
/**

src/main/java/com/google/testing/compile/ProcessedCompileTesterFactory.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717

1818
import javax.annotation.CheckReturnValue;
1919
import javax.annotation.processing.Processor;
20+
import javax.tools.JavaCompiler;
2021

2122
/**
2223
* Creates {@link CompileTester} instances that test compilation with provided {@link Processor}
2324
* instances.
2425
*
2526
* @author Gregory Kick
2627
*/
27-
public interface ProcessedCompileTesterFactory {
28+
public interface ProcessedCompileTesterFactory extends CompileTester{
29+
30+
/** Specify compiler (Javac, Eclipse ECJ, ...) **/
31+
@CheckReturnValue
32+
ProcessedCompileTesterFactory withCompiler(JavaCompiler var1);
2833

2934
/**
3035
* Adds options that will be passed to the compiler. {@code -Xlint} is the first option, by

src/test/java/com/google/testing/compile/JavaSourcesSubjectFactoryTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.common.truth.FailureStrategy;
3030
import com.google.common.truth.TestVerb;
3131

32+
import org.eclipse.jdt.internal.compiler.tool.EclipseCompiler;
3233
import org.junit.Test;
3334
import org.junit.runner.RunWith;
3435
import org.junit.runners.JUnit4;
@@ -107,6 +108,15 @@ public void compilesWithoutError() {
107108
.compilesWithoutError();
108109
}
109110

111+
@Test
112+
public void compilesWithoutErrorWithEclipseCompiler() {
113+
assertAbout(javaSource())
114+
.that(JavaFileObjects.forResource(Resources.getResource("HelloWorld.java")))
115+
.withCompiler(new EclipseCompiler())
116+
.withCompilerOptions("-nowarn", "-1.6")
117+
.compilesWithoutError();
118+
}
119+
110120
@Test
111121
public void compilesWithoutWarnings() {
112122
assertAbout(javaSource()).that(HELLO_WORLD).compilesWithoutWarnings();

0 commit comments

Comments
 (0)