Skip to content

Commit

Permalink
Move a function to JavacProviders
Browse files Browse the repository at this point in the history
Summary: ^

Reviewed By: ttsugriy

fbshipit-source-id: c55abec
  • Loading branch information
cjhopman authored and facebook-github-bot committed Jun 6, 2018
1 parent c45e971 commit 2ebc606
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
21 changes: 1 addition & 20 deletions src/com/facebook/buck/jvm/java/AbstractJavacSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Either<BuiltInJavac, SourcePath>> getCompiler();

Expand All @@ -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() {
Expand Down
27 changes: 27 additions & 0 deletions src/com/facebook/buck/jvm/java/ExternalJavacFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -335,7 +336,7 @@ private Jsr199Javac createJavac(boolean withSyntaxError, Optional<Path> javacJar
Optional<SourcePath> 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();
Expand Down

0 comments on commit 2ebc606

Please sign in to comment.