diff --git a/src/com/facebook/buck/jvm/java/AbstractJavacSpec.java b/src/com/facebook/buck/jvm/java/AbstractJavacSpec.java index b95654a7dbb..3dbceb5aae5 100644 --- a/src/com/facebook/buck/jvm/java/AbstractJavacSpec.java +++ b/src/com/facebook/buck/jvm/java/AbstractJavacSpec.java @@ -26,8 +26,6 @@ @Value.Immutable(copy = true) @BuckStyleImmutable abstract class AbstractJavacSpec { - public static final String COM_SUN_TOOLS_JAVAC_API_JAVAC_TOOL = - "com.sun.tools.javac.api.JavacTool"; protected abstract Optional> getCompiler(); @@ -39,24 +37,7 @@ abstract class AbstractJavacSpec { @Value.Lazy public JavacProvider getJavacProvider() { - if (getCompiler().isPresent() && getCompiler().get().isRight()) { - return new ExternalOrJarBackedJavacProvider( - getCompiler().get().getRight(), - // compiler_class_name has no effect when compiler is specified - COM_SUN_TOOLS_JAVAC_API_JAVAC_TOOL); - } - - String compilerClassName = getCompilerClassName().orElse(COM_SUN_TOOLS_JAVAC_API_JAVAC_TOOL); - Javac.Source javacSource = getJavacSource(); - switch (javacSource) { - case EXTERNAL: - return new ConstantJavacProvider(new ExternalJavacFactory().create(getJavacPath().get())); - case JAR: - return new JarBackedJavacProvider(getJavacJarPath().get(), compilerClassName); - case JDK: - return new ConstantJavacProvider(new JdkProvidedInMemoryJavac()); - } - throw new AssertionError("Unknown javac source: " + javacSource); + return ExternalJavacFactory.getProviderForSpec((JavacSpec) this); } public Javac.Source getJavacSource() { diff --git a/src/com/facebook/buck/jvm/java/ExternalJavacFactory.java b/src/com/facebook/buck/jvm/java/ExternalJavacFactory.java index dc47cf9c39f..a87d5db2a2f 100644 --- a/src/com/facebook/buck/jvm/java/ExternalJavacFactory.java +++ b/src/com/facebook/buck/jvm/java/ExternalJavacFactory.java @@ -40,6 +40,9 @@ public class ExternalJavacFactory { private final ProcessExecutor processExecutor; + public static final String COM_SUN_TOOLS_JAVAC_API_JAVAC_TOOL = + "com.sun.tools.javac.api.JavacTool"; + @VisibleForTesting ExternalJavacFactory(ProcessExecutor processExecutor) { this.processExecutor = processExecutor; @@ -49,6 +52,30 @@ public ExternalJavacFactory() { this(new DefaultProcessExecutor(Console.createNullConsole())); } + /** Creates a JavacProvider based on a spec. */ + public static JavacProvider getProviderForSpec(JavacSpec spec) { + if (spec.getCompiler().isPresent() && spec.getCompiler().get().isRight()) { + return new ExternalOrJarBackedJavacProvider( + spec.getCompiler().get().getRight(), + // compiler_class_name has no effect when compiler is specified + COM_SUN_TOOLS_JAVAC_API_JAVAC_TOOL); + } + + String compilerClassName = + spec.getCompilerClassName().orElse(COM_SUN_TOOLS_JAVAC_API_JAVAC_TOOL); + Javac.Source javacSource = spec.getJavacSource(); + switch (javacSource) { + case EXTERNAL: + return new ConstantJavacProvider( + new ExternalJavacFactory().create(spec.getJavacPath().get())); + case JAR: + return new JarBackedJavacProvider(spec.getJavacJarPath().get(), compilerClassName); + case JDK: + return new ConstantJavacProvider(new JdkProvidedInMemoryJavac()); + } + throw new AssertionError("Unknown javac source: " + javacSource); + } + /** Creates an ExternalJavac. */ public ExternalJavac create(final SourcePath pathToJavac) { if (pathToJavac instanceof BuildTargetSourcePath) { diff --git a/test/com/facebook/buck/jvm/java/Jsr199JavacIntegrationTest.java b/test/com/facebook/buck/jvm/java/Jsr199JavacIntegrationTest.java index ecd22f5b51d..819bd7e8b85 100644 --- a/test/com/facebook/buck/jvm/java/Jsr199JavacIntegrationTest.java +++ b/test/com/facebook/buck/jvm/java/Jsr199JavacIntegrationTest.java @@ -263,7 +263,8 @@ public void shouldUseSpecifiedJavacJar() throws Exception { MockClassLoader mockClassLoader = new MockClassLoader( ClassLoader.getSystemClassLoader(), - ImmutableMap.of(JavacSpec.COM_SUN_TOOLS_JAVAC_API_JAVAC_TOOL, MockJavac.class)); + ImmutableMap.of( + ExternalJavacFactory.COM_SUN_TOOLS_JAVAC_API_JAVAC_TOOL, MockJavac.class)); executionContext .getClassLoaderCache() .injectClassLoader( @@ -335,7 +336,7 @@ private Jsr199Javac createJavac(boolean withSyntaxError, Optional javacJar Optional jar = javacJar.map(p -> PathSourcePath.of(new FakeProjectFilesystem(), p)); if (jar.isPresent()) { return new JarBackedJavac( - JavacSpec.COM_SUN_TOOLS_JAVAC_API_JAVAC_TOOL, ImmutableSet.of(jar.get())); + ExternalJavacFactory.COM_SUN_TOOLS_JAVAC_API_JAVAC_TOOL, ImmutableSet.of(jar.get())); } return new JdkProvidedInMemoryJavac();