Add --incompatible_disallow_legacy_java_provider flag.

RELNOTES: None.
PiperOrigin-RevId: 228660460
diff --git a/src/main/java/com/google/devtools/build/lib/packages/SkylarkSemanticsOptions.java b/src/main/java/com/google/devtools/build/lib/packages/SkylarkSemanticsOptions.java
index 09d9d48..4f35a69 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/SkylarkSemanticsOptions.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/SkylarkSemanticsOptions.java
@@ -286,6 +286,18 @@
   public boolean incompatibleDisallowFileType;
 
   @Option(
+      name = "incompatible_disallow_legacy_java_provider",
+      defaultValue = "false",
+      documentationCategory = OptionDocumentationCategory.SKYLARK_SEMANTICS,
+      effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS},
+      metadataTags = {
+        OptionMetadataTag.INCOMPATIBLE_CHANGE,
+        OptionMetadataTag.TRIGGERED_BY_ALL_INCOMPATIBLE_CHANGES
+      },
+      help = "If set to true, usages of old .java provider are disallowed.")
+  public boolean incompatibleDisallowLegacyJavaProvider;
+
+  @Option(
       name = "incompatible_disallow_legacy_javainfo",
       defaultValue = "false",
       documentationCategory = OptionDocumentationCategory.SKYLARK_SEMANTICS,
@@ -546,6 +558,7 @@
         .incompatibleDisallowDictPlus(incompatibleDisallowDictPlus)
         .incompatibleDisallowFileType(incompatibleDisallowFileType)
         .incompatibleDisallowLegacyJavaInfo(incompatibleDisallowLegacyJavaInfo)
+        .incompatibleDisallowLegacyJavaProvider(incompatibleDisallowLegacyJavaProvider)
         .incompatibleDisallowLoadLabelsToCrossPackageBoundaries(
             incompatibleDisallowLoadLabelsToCrossPackageBoundaries)
         .incompatibleDisallowOldStyleArgsAdd(incompatibleDisallowOldStyleArgsAdd)
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiProvider.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiProvider.java
index 51bdea5..f6ff009 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiProvider.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiProvider.java
@@ -22,9 +22,12 @@
 import com.google.devtools.build.lib.collect.nestedset.NestedSet;
 import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
 import com.google.devtools.build.lib.collect.nestedset.Order;
+import com.google.devtools.build.lib.events.Location;
 import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier;
 import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
 import com.google.devtools.build.lib.skylarkbuildapi.java.JavaSkylarkApiProviderApi;
+import com.google.devtools.build.lib.syntax.EvalException;
+import com.google.devtools.build.lib.syntax.SkylarkSemantics;
 import javax.annotation.Nullable;
 
 /**
@@ -69,7 +72,9 @@
   }
 
   @Override
-  public NestedSet<Artifact> getSourceJars() {
+  public NestedSet<Artifact> getSourceJars(Location location, SkylarkSemantics semantics)
+      throws EvalException {
+    checkLegacyJavaProviderFlag(location, semantics);
     JavaSourceJarsProvider sourceJarsProvider = getProvider(JavaSourceJarsProvider.class);
     if (sourceJarsProvider == null) {
       return NestedSetBuilder.emptySet(Order.STABLE_ORDER);
@@ -78,7 +83,9 @@
   }
 
   @Override
-  public NestedSet<Artifact> getTransitiveDeps() {
+  public NestedSet<Artifact> getTransitiveDeps(Location location, SkylarkSemantics semantics)
+      throws EvalException {
+    checkLegacyJavaProviderFlag(location, semantics);
     JavaCompilationArgsProvider compilationArgsProvider =
         getProvider(JavaCompilationArgsProvider.class);
     if (compilationArgsProvider == null) {
@@ -88,7 +95,9 @@
   }
 
   @Override
-  public NestedSet<Artifact> getTransitiveRuntimeDeps() {
+  public NestedSet<Artifact> getTransitiveRuntimeDeps(Location location, SkylarkSemantics semantics)
+      throws EvalException {
+    checkLegacyJavaProviderFlag(location, semantics);
     JavaCompilationArgsProvider compilationArgsProvider =
         getProvider(JavaCompilationArgsProvider.class);
     if (compilationArgsProvider == null) {
@@ -98,7 +107,9 @@
   }
 
   @Override
-  public NestedSet<Artifact> getTransitiveSourceJars() {
+  public NestedSet<Artifact> getTransitiveSourceJars(Location location, SkylarkSemantics semantics)
+      throws EvalException {
+    checkLegacyJavaProviderFlag(location, semantics);
     JavaSourceJarsProvider sourceJarsProvider = getProvider(JavaSourceJarsProvider.class);
     if (sourceJarsProvider == null) {
       return NestedSetBuilder.emptySet(Order.STABLE_ORDER);
@@ -107,12 +118,16 @@
   }
 
   @Override
-  public JavaRuleOutputJarsProvider getOutputJars() {
+  public JavaRuleOutputJarsProvider getOutputJars(Location location, SkylarkSemantics semantics)
+      throws EvalException {
+    checkLegacyJavaProviderFlag(location, semantics);
     return getProvider(JavaRuleOutputJarsProvider.class);
   }
 
   @Override
-  public NestedSet<Label> getTransitiveExports() {
+  public NestedSet<Label> getTransitiveExports(Location location, SkylarkSemantics semantics)
+      throws EvalException {
+    checkLegacyJavaProviderFlag(location, semantics);
     JavaExportsProvider exportsProvider = getProvider(JavaExportsProvider.class);
     if (exportsProvider != null) {
       return exportsProvider.getTransitiveExports();
@@ -122,12 +137,27 @@
   }
 
   @Override
-  public JavaGenJarsProvider getGenJarsProvider() {
+  public JavaGenJarsProvider getGenJarsProvider(Location location, SkylarkSemantics semantics)
+      throws EvalException {
+    checkLegacyJavaProviderFlag(location, semantics);
     return getProvider(JavaGenJarsProvider.class);
   }
 
   @Override
-  public JavaCompilationInfoProvider getCompilationInfoProvider() {
+  public JavaCompilationInfoProvider getCompilationInfoProvider(
+      Location location, SkylarkSemantics semantics) throws EvalException {
+    checkLegacyJavaProviderFlag(location, semantics);
     return getProvider(JavaCompilationInfoProvider.class);
   }
+
+  private void checkLegacyJavaProviderFlag(Location location, SkylarkSemantics semantics)
+      throws EvalException {
+    if (semantics.incompatibleDisallowLegacyJavaProvider()) {
+      throw new EvalException(
+          location,
+          "The .java provider is deprecated and cannot be used "
+              + "when --incompatible_disallow_legacy_java_provider is set. "
+              + "Please migrate to the JavaInfo Skylark provider.");
+    }
+  }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaSkylarkApiProviderApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaSkylarkApiProviderApi.java
index 6afac7e..b47b29c 100644
--- a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaSkylarkApiProviderApi.java
+++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaSkylarkApiProviderApi.java
@@ -16,10 +16,13 @@
 
 import com.google.devtools.build.lib.cmdline.Label;
 import com.google.devtools.build.lib.collect.nestedset.NestedSet;
+import com.google.devtools.build.lib.events.Location;
 import com.google.devtools.build.lib.skylarkbuildapi.FileApi;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
+import com.google.devtools.build.lib.syntax.EvalException;
+import com.google.devtools.build.lib.syntax.SkylarkSemantics;
 
 /**
  * Provides access to information about Java rules. Every Java-related target provides
@@ -37,62 +40,78 @@
 public interface JavaSkylarkApiProviderApi<FileT extends FileApi> {
 
   @SkylarkCallable(
-    name = "source_jars",
-    doc = "Returns the Jars containing Java source files for the target.",
-    structField = true
-  )
-  public NestedSet<FileT> getSourceJars();
+      name = "source_jars",
+      doc = "Returns the Jars containing Java source files for the target.",
+      useSkylarkSemantics = true,
+      useLocation = true,
+      structField = true)
+  public NestedSet<FileT> getSourceJars(Location location, SkylarkSemantics semantics)
+      throws EvalException;
 
   @SkylarkCallable(
-    name = "transitive_deps",
-    doc = "Returns the transitive set of Jars required to build the target.",
-    structField = true
-  )
-  public NestedSet<FileT> getTransitiveDeps();
+      name = "transitive_deps",
+      doc = "Returns the transitive set of Jars required to build the target.",
+      useSkylarkSemantics = true,
+      useLocation = true,
+      structField = true)
+  public NestedSet<FileT> getTransitiveDeps(Location location, SkylarkSemantics semantics)
+      throws EvalException;
 
   @SkylarkCallable(
-    name = "transitive_runtime_deps",
-    doc = "Returns the transitive set of Jars required on the target's runtime classpath.",
-    structField = true
-  )
-  public NestedSet<FileT> getTransitiveRuntimeDeps();
+      name = "transitive_runtime_deps",
+      doc = "Returns the transitive set of Jars required on the target's runtime classpath.",
+      useSkylarkSemantics = true,
+      useLocation = true,
+      structField = true)
+  public NestedSet<FileT> getTransitiveRuntimeDeps(Location location, SkylarkSemantics semantics)
+      throws EvalException;
 
   @SkylarkCallable(
-    name = "transitive_source_jars",
-    doc =
-        "Returns the Jars containing Java source files for the target and all of its transitive "
-            + "dependencies.",
-    structField = true
-  )
-  public NestedSet<FileT> getTransitiveSourceJars();
+      name = "transitive_source_jars",
+      doc =
+          "Returns the Jars containing Java source files for the target and all of its transitive "
+              + "dependencies.",
+      useSkylarkSemantics = true,
+      useLocation = true,
+      structField = true)
+  public NestedSet<FileT> getTransitiveSourceJars(Location location, SkylarkSemantics semantics)
+      throws EvalException;
 
   @SkylarkCallable(
-    name = "outputs",
-    doc = "Returns information about outputs of this Java target.",
-    structField = true
-  )
-  public JavaRuleOutputJarsProviderApi<?> getOutputJars();
+      name = "outputs",
+      doc = "Returns information about outputs of this Java target.",
+      useSkylarkSemantics = true,
+      useLocation = true,
+      structField = true)
+  public JavaRuleOutputJarsProviderApi<?> getOutputJars(
+      Location location, SkylarkSemantics semantics) throws EvalException;
 
   @SkylarkCallable(
-    name = "transitive_exports",
-    structField = true,
-    doc = "Returns transitive set of labels that are being exported from this rule."
-  )
-  public NestedSet<Label> getTransitiveExports();
+      name = "transitive_exports",
+      structField = true,
+      useSkylarkSemantics = true,
+      useLocation = true,
+      doc = "Returns transitive set of labels that are being exported from this rule.")
+  public NestedSet<Label> getTransitiveExports(Location location, SkylarkSemantics semantics)
+      throws EvalException;
 
   @SkylarkCallable(
-    name = "annotation_processing",
-    structField = true,
-    allowReturnNones = true,
-    doc = "Returns information about annotation processing for this Java target."
-  )
-  public JavaAnnotationProcessingApi<?> getGenJarsProvider();
+      name = "annotation_processing",
+      structField = true,
+      allowReturnNones = true,
+      useSkylarkSemantics = true,
+      useLocation = true,
+      doc = "Returns information about annotation processing for this Java target.")
+  public JavaAnnotationProcessingApi<?> getGenJarsProvider(
+      Location location, SkylarkSemantics semantics) throws EvalException;
 
   @SkylarkCallable(
-    name = "compilation_info",
-    structField = true,
-    allowReturnNones = true,
-    doc = "Returns compilation information for this Java target."
-  )
-  public JavaCompilationInfoProviderApi<?> getCompilationInfoProvider();
+      name = "compilation_info",
+      structField = true,
+      allowReturnNones = true,
+      useSkylarkSemantics = true,
+      useLocation = true,
+      doc = "Returns compilation information for this Java target.")
+  public JavaCompilationInfoProviderApi<?> getCompilationInfoProvider(
+      Location location, SkylarkSemantics semantics) throws EvalException;
 }
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkSemantics.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkSemantics.java
index 86c6565..6f9b309 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkSemantics.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkSemantics.java
@@ -152,6 +152,8 @@
 
   public abstract boolean incompatibleDisallowFileType();
 
+  public abstract boolean incompatibleDisallowLegacyJavaProvider();
+
   public abstract boolean incompatibleDisallowLegacyJavaInfo();
 
   public abstract boolean incompatibleDisallowLoadLabelsToCrossPackageBoundaries();
@@ -221,6 +223,7 @@
           .incompatibleDisallowDataTransition(false)
           .incompatibleDisallowDictPlus(false)
           .incompatibleDisallowFileType(false)
+          .incompatibleDisallowLegacyJavaProvider(false)
           .incompatibleDisallowLegacyJavaInfo(false)
           .incompatibleDisallowLoadLabelsToCrossPackageBoundaries(false)
           .incompatibleDisallowOldStyleArgsAdd(false)
@@ -284,6 +287,8 @@
 
     public abstract Builder incompatibleDisallowFileType(boolean value);
 
+    public abstract Builder incompatibleDisallowLegacyJavaProvider(boolean value);
+
     public abstract Builder incompatibleDisallowLegacyJavaInfo(boolean value);
 
     public abstract Builder incompatibleDisallowLoadLabelsToCrossPackageBoundaries(boolean value);
diff --git a/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java b/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java
index 7be8fb2..08bde63 100644
--- a/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java
+++ b/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java
@@ -143,6 +143,7 @@
         "--incompatible_disallow_dict_plus=" + rand.nextBoolean(),
         "--incompatible_disallow_filetype=" + rand.nextBoolean(),
         "--incompatible_disallow_legacy_javainfo=" + rand.nextBoolean(),
+        "--incompatible_disallow_legacy_java_provider=" + rand.nextBoolean(),
         "--incompatible_disallow_load_labels_to_cross_package_boundaries=" + rand.nextBoolean(),
         "--incompatible_disallow_old_style_args_add=" + rand.nextBoolean(),
         "--incompatible_disallow_slash_operator=" + rand.nextBoolean(),
@@ -190,6 +191,7 @@
         .incompatibleDisallowDictPlus(rand.nextBoolean())
         .incompatibleDisallowFileType(rand.nextBoolean())
         .incompatibleDisallowLegacyJavaInfo(rand.nextBoolean())
+        .incompatibleDisallowLegacyJavaProvider(rand.nextBoolean())
         .incompatibleDisallowLoadLabelsToCrossPackageBoundaries(rand.nextBoolean())
         .incompatibleDisallowOldStyleArgsAdd(rand.nextBoolean())
         .incompatibleDisallowSlashOperator(rand.nextBoolean())
diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java
index 4464f35..be6e8ba 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java
@@ -2105,4 +2105,29 @@
     assertThat(output.getClassJar().getFilename()).isEqualTo("libc.jar");
     assertThat(output.getIJar()).isNull();
   }
+
+  @Test
+  public void testDisallowLegacyJavaProvider() throws Exception {
+    setSkylarkSemanticsOptions("--incompatible_disallow_legacy_java_provider");
+    scratch.file(
+        "foo/custom_rule.bzl",
+        "def _impl(ctx):",
+        "  ctx.attr.java_lib.java.source_jars",
+        "java_custom_library = rule(",
+        "  implementation = _impl,",
+        "  attrs = {",
+        "    'java_lib': attr.label(),",
+        "   },",
+        ")");
+
+    scratch.file(
+        "foo/BUILD",
+        "load(':custom_rule.bzl', 'java_custom_library')",
+        "java_library(name = 'java_lib', srcs = ['java/A.java'])",
+        "java_custom_library(name = 'custom_lib', java_lib = ':java_lib')");
+    checkError(
+        "//foo:custom_lib",
+        "The .java provider is deprecated and cannot be used "
+            + "when --incompatible_disallow_legacy_java_provider is set.");
+  }
 }