diff options
author | Googler <noreply@google.com> | 2018-02-21 11:35:55 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-02-21 11:37:32 -0800 |
commit | ab37330bb7e549a24031418b1885048f75c111ce (patch) | |
tree | e4ff472b3fbcc912d8544251c71bbbcff10b4a6a /src/java_tools/buildjar | |
parent | 86f7e685e766faf642ffab029c7c1968340ac028 (diff) |
Add --experimental_fix_deps_tool to JavaBuilder
RELNOTES: None
PiperOrigin-RevId: 186494122
Diffstat (limited to 'src/java_tools/buildjar')
3 files changed, 68 insertions, 12 deletions
diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/OptionsParser.java b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/OptionsParser.java index 5413e5312d..24747e9f7b 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/OptionsParser.java +++ b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/OptionsParser.java @@ -49,6 +49,7 @@ public final class OptionsParser { private final Set<String> directJars = new HashSet<>(); private String strictJavaDeps; + private String fixDepsTool; private String outputDepsProtoFile; private final Set<String> depsArtifacts = new HashSet<>(); @@ -143,6 +144,9 @@ public final class OptionsParser { case "--strict_java_deps": strictJavaDeps = getArgument(argQueue, arg); break; + case "--experimental_fix_deps_tool": + fixDepsTool = getArgument(argQueue, arg); + break; case "--output_deps_proto": outputDepsProtoFile = getArgument(argQueue, arg); break; @@ -436,6 +440,10 @@ public final class OptionsParser { return strictJavaDeps; } + public String getFixDepsTool() { + return fixDepsTool; + } + public String getOutputDepsProtoFile() { return outputDepsProtoFile; } diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins/dependency/DependencyModule.java b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins/dependency/DependencyModule.java index 4534eb571e..2428245f91 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins/dependency/DependencyModule.java +++ b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins/dependency/DependencyModule.java @@ -36,7 +36,10 @@ import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.Optional; import java.util.Set; +import javax.tools.Diagnostic; +import javax.tools.JavaFileObject; /** * Wrapper class for managing dependencies on top of {@link @@ -70,6 +73,7 @@ public final class DependencyModule { } private final StrictJavaDeps strictJavaDeps; + private final FixTool fixDepsTool; private final ImmutableSet<Path> directJars; private final ImmutableMap<Path, JarOwner> jarsToTargets; private final boolean strictClasspathMode; @@ -88,6 +92,7 @@ public final class DependencyModule { DependencyModule( StrictJavaDeps strictJavaDeps, + FixTool fixDepsTool, ImmutableSet<Path> directJars, ImmutableMap<Path, JarOwner> jarsToTargets, boolean strictClasspathMode, @@ -99,6 +104,7 @@ public final class DependencyModule { FixMessage fixMessage, Set<String> exemptGenerators) { this.strictJavaDeps = strictJavaDeps; + this.fixDepsTool = fixDepsTool; this.directJars = directJars; this.jarsToTargets = jarsToTargets; this.strictClasspathMode = strictClasspathMode; @@ -186,6 +192,11 @@ public final class DependencyModule { return strictJavaDeps; } + /** Returns which tool to use for adding missing dependencies. */ + public FixTool getFixDepsTool() { + return fixDepsTool; + } + /** Returns the map collecting precise explicit dependency information. */ public Map<Path, Dependency> getExplicitDependenciesMap() { return explicitDependenciesMap; @@ -293,18 +304,47 @@ public final class DependencyModule { } } - /** - * A functional that formats a message for the user about a missing dependency that they should - * add to unbreak their build. - */ + /** Emits a message to the user about missing dependencies to add to unbreak their build. */ public interface FixMessage { - String get(Iterable<JarOwner> missing, String recipient, boolean useColor); + + /** + * Gets a message describing what dependencies are missing and how to fix them. + * + * @param missing the missing dependencies to be added. + * @param recipient the target from which the dependencies are missing. + * @param dependencyModule {@link DependencyModule} instance for compilation context. + * @return the string message describing the dependency build issues, including fix. + */ + String get(Iterable<JarOwner> missing, String recipient, DependencyModule dependencyModule); + } + + /** Tool with which to fix dependency issues. */ + public interface FixTool { + + /** + * Applies this tool to find the missing import/dependency. + * + * @param diagnostic a full javac diagnostic, possibly containing an import for a class which + * cannot be found on the classpath. + * @param javacopts list of all javac options/flags. + * @return the missing import or dependency as a String, or empty Optional if the diagnostic did + * not contain exactly one unresolved import that we know how to fix. + */ + Optional<String> resolveMissingImport( + Diagnostic<JavaFileObject> diagnostic, ImmutableList<String> javacopts); + + /** + * Returns a command for this tool to fix {@code recipient} by adding all {@code missing} + * dependencies for this target. + */ + String getFixCommand(Iterable<String> missing, String recipient); } /** Builder for {@link DependencyModule}. */ public static class Builder { private StrictJavaDeps strictJavaDeps = StrictJavaDeps.OFF; + private FixTool fixDepsTool = null; private ImmutableSet<Path> directJars = ImmutableSet.of(); private ImmutableMap<Path, JarOwner> jarsToTargets = ImmutableMap.of(); private final Set<Path> depsArtifacts = new HashSet<>(); @@ -318,7 +358,7 @@ public final class DependencyModule { private static class DefaultFixMessage implements FixMessage { @Override - public String get(Iterable<JarOwner> missing, String recipient, boolean useColor) { + public String get(Iterable<JarOwner> missing, String recipient, DependencyModule depModule) { StringBuilder missingTargetsStr = new StringBuilder(); for (JarOwner owner : missing) { missingTargetsStr.append(owner.label()); @@ -329,10 +369,7 @@ public final class DependencyModule { "%1$s ** Please add the following dependencies:%2$s \n %3$s to %4$s \n" + "%1$s ** You can use the following buildozer command:%2$s " + "\nbuildozer 'add deps %3$s' %4$s \n\n", - useColor ? "\033[35m\033[1m" : "", - useColor ? "\033[0m" : "", - missingTargetsStr.toString(), - recipient); + "\033[35m\033[1m", "\033[0m", missingTargetsStr.toString(), recipient); } } @@ -345,6 +382,7 @@ public final class DependencyModule { public DependencyModule build() { return new DependencyModule( strictJavaDeps, + fixDepsTool, directJars, jarsToTargets, strictClasspathMode, @@ -369,6 +407,17 @@ public final class DependencyModule { } /** + * Sets which tool to use for fixing missing dependencies. + * + * @param fixDepsTool tool name + * @return this Builder instance + */ + public Builder setFixDepsTool(FixTool fixDepsTool) { + this.fixDepsTool = fixDepsTool; + return this; + } + + /** * Sets the type (rule kind) of the originating target. * * @param ruleKind kind, such as the rule kind of a RuleConfiguredTarget diff --git a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins/dependency/StrictJavaDepsPlugin.java b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins/dependency/StrictJavaDepsPlugin.java index 8d78db439e..62fe8ddb46 100644 --- a/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins/dependency/StrictJavaDepsPlugin.java +++ b/src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins/dependency/StrictJavaDepsPlugin.java @@ -69,7 +69,6 @@ public final class StrictJavaDepsPlugin extends BlazeJavaCompilerPlugin { static String targetMapping = "com/google/devtools/build/buildjar/javac/resources/target.properties"; - public static final boolean USE_COLOR = true; private ImplicitDependencyExtractor implicitDependencyExtractor; private CheckingTreeScanner checkingTreeScanner; private final DependencyModule dependencyModule; @@ -214,7 +213,7 @@ public final class StrictJavaDepsPlugin extends BlazeJavaCompilerPlugin { errWriter.print( dependencyModule .getFixMessage() - .get(canonicalizedMissing, canonicalizedLabel, USE_COLOR)); + .get(canonicalizedMissing, canonicalizedLabel, dependencyModule)); } } |