Initialize JavaCommon.sources eagerly, rather than in initCommon(). This allows us to make the field final, and guarantees that it can't be mutated after construction. -- MOS_MIGRATED_REVID=115430793
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java index cba5c47..2771da6 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java
@@ -103,7 +103,7 @@ private final ImmutableMap<ClasspathType, ImmutableList<TransitiveInfoCollection>> targetsTreatedAsDeps; - private ImmutableList<Artifact> sources = ImmutableList.of(); + private final ImmutableList<Artifact> sources; private ImmutableList<JavaPluginInfoProvider> activePlugins = ImmutableList.of(); private final RuleContext ruleContext; @@ -112,6 +112,16 @@ public JavaCommon(RuleContext ruleContext, JavaSemantics semantics) { this(ruleContext, semantics, + ruleContext.getPrerequisiteArtifacts("srcs", Mode.TARGET).list(), + collectTargetsTreatedAsDeps(ruleContext, semantics, ClasspathType.COMPILE_ONLY), + collectTargetsTreatedAsDeps(ruleContext, semantics, ClasspathType.RUNTIME_ONLY), + collectTargetsTreatedAsDeps(ruleContext, semantics, ClasspathType.BOTH)); + } + + public JavaCommon(RuleContext ruleContext, JavaSemantics semantics, + ImmutableList<Artifact> sources) { + this(ruleContext, semantics, + sources, collectTargetsTreatedAsDeps(ruleContext, semantics, ClasspathType.COMPILE_ONLY), collectTargetsTreatedAsDeps(ruleContext, semantics, ClasspathType.RUNTIME_ONLY), collectTargetsTreatedAsDeps(ruleContext, semantics, ClasspathType.BOTH)); @@ -122,8 +132,20 @@ ImmutableList<TransitiveInfoCollection> compileDeps, ImmutableList<TransitiveInfoCollection> runtimeDeps, ImmutableList<TransitiveInfoCollection> bothDeps) { + this(ruleContext, semantics, + ruleContext.getPrerequisiteArtifacts("srcs", Mode.TARGET).list(), + compileDeps, runtimeDeps, bothDeps); + } + + public JavaCommon(RuleContext ruleContext, + JavaSemantics semantics, + ImmutableList<Artifact> sources, + ImmutableList<TransitiveInfoCollection> compileDeps, + ImmutableList<TransitiveInfoCollection> runtimeDeps, + ImmutableList<TransitiveInfoCollection> bothDeps) { this.ruleContext = ruleContext; this.semantics = semantics; + this.sources = sources; this.targetsTreatedAsDeps = ImmutableMap.of( ClasspathType.COMPILE_ONLY, compileDeps, ClasspathType.RUNTIME_ONLY, runtimeDeps, @@ -504,7 +526,6 @@ */ public JavaTargetAttributes.Builder initCommon(Collection<Artifact> extraSrcs) { Preconditions.checkState(javacOpts != null); - sources = ruleContext.getPrerequisiteArtifacts("srcs", Mode.TARGET).list(); activePlugins = collectPlugins(); JavaTargetAttributes.Builder javaTargetAttributes = new JavaTargetAttributes.Builder(semantics);
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java index fa3805b..46e4d81 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java
@@ -63,7 +63,7 @@ .addAll(ruleContext.getPrerequisites("exports", Mode.TARGET)) .build(); final JavaCommon common = new JavaCommon( - ruleContext, semantics, targets, targets, targets); + ruleContext, semantics, /*srcs=*/ImmutableList.<Artifact>of(), targets, targets, targets); semantics.checkRule(ruleContext, common); // No need for javac options - no compilation happening here.