Skip to content

Commit

Permalink
Add the fourth (and last) variant of native Java provider wrappers fo…
Browse files Browse the repository at this point in the history
…r use in `WORKSPACE` mode

PiperOrigin-RevId: 696033794
Change-Id: I0d49b21567a82f673694c558bbcb8a08950559f3
  • Loading branch information
hvadehra authored and copybara-github committed Nov 13, 2024
1 parent 67ff5a1 commit 5b09ab6
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class BootClassPathInfo extends StarlarkInfoWrapper {
public static final StarlarkProviderWrapper<BootClassPathInfo> PROVIDER = new Provider();
public static final StarlarkProviderWrapper<BootClassPathInfo> RULES_JAVA_PROVIDER =
new RulesJavaProvider();
public static final StarlarkProviderWrapper<BootClassPathInfo> WORKSPACE_PROVIDER =
new WorkspaceProvider();

private static final BootClassPathInfo EMPTY =
new BootClassPathInfo(null) {
Expand Down Expand Up @@ -89,6 +91,8 @@ public static BootClassPathInfo wrap(Info info) throws RuleErrorException {
return LEGACY_BUILTINS_PROVIDER.wrap(info);
} else if (key.equals(RULES_JAVA_PROVIDER.getKey())) {
return RULES_JAVA_PROVIDER.wrap(info);
} else if (key.equals(WORKSPACE_PROVIDER.getKey())) {
return WORKSPACE_PROVIDER.wrap(info);
} else {
throw new RuleErrorException("expected BootClassPathInfo, got: " + key);
}
Expand Down Expand Up @@ -139,6 +143,15 @@ private RulesJavaProvider() {
}
}

private static class WorkspaceProvider extends Provider {
private WorkspaceProvider() {
super(
keyForBuild(
Label.parseCanonicalUnchecked(
"@@rules_java//java/private:boot_class_path_info.bzl")));
}
}

private static class Provider extends StarlarkProviderWrapper<BootClassPathInfo> {
private Provider() {
this(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
@Immutable
public sealed class JavaInfo extends NativeInfo
implements JavaInfoApi<Artifact, JavaOutput, JavaPluginData>
permits JavaInfo.BuiltinsJavaInfo, JavaInfo.RulesJavaJavaInfo {
permits JavaInfo.BuiltinsJavaInfo, JavaInfo.RulesJavaJavaInfo, JavaInfo.WorkspaceJavaInfo {

public static final String STARLARK_NAME = "JavaInfo";

Expand All @@ -78,6 +78,8 @@ public sealed class JavaInfo extends NativeInfo

// Not serialized
public static final JavaInfoProvider RULES_JAVA_PROVIDER = new RulesJavaJavaInfoProvider();
// Not serialized
public static final JavaInfoProvider WORKSPACE_PROVIDER = new WorkspaceJavaInfoProvider();

@SerializationConstant public static final JavaInfoProvider PROVIDER = new JavaInfoProvider();

Expand Down Expand Up @@ -222,6 +224,9 @@ public static JavaInfo getJavaInfo(TransitiveInfoCollection target) throws RuleE
if (info == null) {
info = target.get(RULES_JAVA_PROVIDER);
}
if (info == null) {
info = target.get(WORKSPACE_PROVIDER);
}
return info;
}

Expand All @@ -231,6 +236,8 @@ public static JavaInfo wrap(Info info) throws RuleErrorException {
return LEGACY_BUILTINS_PROVIDER.wrap(info);
} else if (key.equals(RULES_JAVA_PROVIDER.getKey())) {
return RULES_JAVA_PROVIDER.wrap(info);
} else if (key.equals(WORKSPACE_PROVIDER.getKey())) {
return WORKSPACE_PROVIDER.wrap(info);
} else {
return JavaInfo.PROVIDER.wrap(info);
}
Expand Down Expand Up @@ -567,6 +574,19 @@ public JavaInfoProvider getProvider() {
}
}

static final class WorkspaceJavaInfo extends JavaInfo {

private WorkspaceJavaInfo(StructImpl javaInfo)
throws EvalException, TypeException, RuleErrorException {
super(javaInfo);
}

@Override
public JavaInfoProvider getProvider() {
return WORKSPACE_PROVIDER;
}
}

/** Legacy Provider class for {@link JavaInfo} objects. */
public static final class BuiltinsJavaInfoProvider extends JavaInfoProvider {
private BuiltinsJavaInfoProvider() {
Expand Down Expand Up @@ -594,9 +614,23 @@ protected JavaInfo makeNewInstance(StructImpl info)
}
}

/** Legacy Provider class for {@link JavaInfo} objects in WORKSPACE mode. */
public static final class WorkspaceJavaInfoProvider extends JavaInfoProvider {
private WorkspaceJavaInfoProvider() {
super(keyForBuild(Label.parseCanonicalUnchecked("@@rules_java//java/private:java_info.bzl")));
}

@Override
protected JavaInfo makeNewInstance(StructImpl info)
throws RuleErrorException, TypeException, EvalException {
return new WorkspaceJavaInfo(info);
}
}

/** Provider class for {@link JavaInfo} objects. */
public static sealed class JavaInfoProvider extends StarlarkProviderWrapper<JavaInfo>
implements Provider permits BuiltinsJavaInfoProvider, RulesJavaJavaInfoProvider {
implements Provider
permits BuiltinsJavaInfoProvider, RulesJavaJavaInfoProvider, WorkspaceJavaInfoProvider {
private JavaInfoProvider() {
this(
keyForBuild(
Expand Down Expand Up @@ -808,6 +842,8 @@ public void serialize(SerializationContext context, JavaInfo obj, CodedOutputStr
case BuiltinsJavaInfoProvider unused -> codedOut.writeBoolNoTag(true);
case RulesJavaJavaInfoProvider unused ->
throw new UnsupportedOperationException("not implemented");
case WorkspaceJavaInfoProvider unused ->
throw new UnsupportedOperationException("not implemented");
case JavaInfoProvider unused -> codedOut.writeBoolNoTag(false);
}
for (FieldHandler handler : handlers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public abstract class JavaPluginInfo extends NativeInfo
public static final Provider LEGACY_BUILTINS_PROVIDER = new BuiltinsProvider();
public static final Provider PROVIDER = new Provider();
public static final Provider RULES_JAVA_PROVIDER = new RulesJavaProvider();
public static final Provider WORKSPACE_PROVIDER = new WorkspaceProvider();

private static final JavaPluginInfo EMPTY_BUILTIN =
new AutoValue_JavaPluginInfo(
Expand All @@ -69,8 +70,14 @@ public abstract class JavaPluginInfo extends NativeInfo
new AutoValue_JavaPluginInfo(
ImmutableList.of(), JavaPluginData.empty(), JavaPluginData.empty(), RULES_JAVA_PROVIDER);

private static final JavaPluginInfo EMPTY_WORKSPACE =
new AutoValue_JavaPluginInfo(
ImmutableList.of(), JavaPluginData.empty(), JavaPluginData.empty(), WORKSPACE_PROVIDER);

public static JavaPluginInfo wrap(Info info) throws RuleErrorException {
com.google.devtools.build.lib.packages.Provider.Key key = info.getProvider().getKey();
// this wrapped instance is not propagated back to Starlark, so we don't need every type
// we just use the two types that are checked for in tests
if (key.equals(LEGACY_BUILTINS_PROVIDER.getKey())) {
return LEGACY_BUILTINS_PROVIDER.wrap(info);
} else {
Expand All @@ -80,6 +87,7 @@ public static JavaPluginInfo wrap(Info info) throws RuleErrorException {

@VisibleForTesting
public static JavaPluginInfo get(ConfiguredTarget target) throws RuleErrorException {
// we just use the two types that are checked for in tests
JavaPluginInfo info = target.get(PROVIDER);
JavaPluginInfo builtinInfo = target.get(LEGACY_BUILTINS_PROVIDER);
if (info == null) {
Expand Down Expand Up @@ -110,6 +118,13 @@ private RulesJavaProvider() {
}
}

/** Provider class for {@link JavaPluginInfo} objects in WORKSPACE mode. */
public static class WorkspaceProvider extends Provider {
private WorkspaceProvider() {
super(keyForBuild(Label.parseCanonicalUnchecked("@@rules_java//java/private:java_info.bzl")));
}
}

/** Provider class for {@link JavaPluginInfo} objects. */
public static class Provider extends StarlarkProviderWrapper<JavaPluginInfo>
implements com.google.devtools.build.lib.packages.Provider {
Expand Down Expand Up @@ -279,6 +294,8 @@ public static JavaPluginInfo empty(com.google.devtools.build.lib.packages.Provid
return EMPTY_BUILTIN;
} else if (providerType.equals(RULES_JAVA_PROVIDER)) {
return EMPTY_RULES_JAVA;
} else if (providerType.equals(WORKSPACE_PROVIDER)) {
return EMPTY_WORKSPACE;
}
return EMPTY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment envi
.mandatoryProvidersList(
ImmutableList.of(
ImmutableList.of(JavaPluginInfo.PROVIDER.id()),
ImmutableList.of(JavaPluginInfo.RULES_JAVA_PROVIDER.id()),
ImmutableList.of(JavaPluginInfo.WORKSPACE_PROVIDER.id()),
ImmutableList.of(JavaPluginInfo.LEGACY_BUILTINS_PROVIDER.id())))
.silentRuleClassFilter()
.value(JavaSemantics.JAVA_PLUGINS))
Expand Down Expand Up @@ -98,17 +100,26 @@ public ConfiguredTarget create(RuleContext ruleContext)
.getRulePrerequisitesCollection()
.getPrerequisites(":java_plugins", JavaPluginInfo.RULES_JAVA_PROVIDER);
}
if (plugins.isEmpty()) {
plugins =
ruleContext
.getRulePrerequisitesCollection()
.getPrerequisites(":java_plugins", JavaPluginInfo.WORKSPACE_PROVIDER);
}
JavaPluginInfo javaPluginInfo =
JavaPluginInfo.mergeWithoutJavaOutputs(plugins, JavaPluginInfo.PROVIDER);
JavaPluginInfo builtinsProviderInfo =
JavaPluginInfo.mergeWithoutJavaOutputs(plugins, JavaPluginInfo.LEGACY_BUILTINS_PROVIDER);
JavaPluginInfo rulesJavaProviderInfo =
JavaPluginInfo.mergeWithoutJavaOutputs(plugins, JavaPluginInfo.RULES_JAVA_PROVIDER);
JavaPluginInfo workspaceProviderInfo =
JavaPluginInfo.mergeWithoutJavaOutputs(plugins, JavaPluginInfo.WORKSPACE_PROVIDER);

return new RuleConfiguredTargetBuilder(ruleContext)
.addStarlarkDeclaredProvider(javaPluginInfo)
.addStarlarkDeclaredProvider(builtinsProviderInfo)
.addStarlarkDeclaredProvider(rulesJavaProviderInfo)
.addStarlarkDeclaredProvider(workspaceProviderInfo)
.addProvider(RunfilesProvider.class, RunfilesProvider.EMPTY)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public final class JavaRuntimeInfo extends StarlarkInfoWrapper {
new BuiltinsProvider();
public static final StarlarkProviderWrapper<JavaRuntimeInfo> RULES_JAVA_PROVIDER =
new RulesJavaProvider();
public static final StarlarkProviderWrapper<JavaRuntimeInfo> WORKSPACE_PROVIDER =
new WorkspaceProvider();
public static final StarlarkProviderWrapper<JavaRuntimeInfo> PROVIDER = new Provider();

// Helper methods to access an instance of JavaRuntimeInfo.
Expand Down Expand Up @@ -106,6 +108,8 @@ public static JavaRuntimeInfo wrap(Info info) throws RuleErrorException {
return LEGACY_BUILTINS_PROVIDER.wrap(info);
} else if (key.equals(RULES_JAVA_PROVIDER.getKey())) {
return RULES_JAVA_PROVIDER.wrap(info);
} else if (key.equals(WORKSPACE_PROVIDER.getKey())) {
return WORKSPACE_PROVIDER.wrap(info);
} else {
throw new RuleErrorException("expected JavaRuntimeInfo, got: " + key);
}
Expand Down Expand Up @@ -160,6 +164,14 @@ private RulesJavaProvider() {
}
}

private static class WorkspaceProvider extends Provider {
private WorkspaceProvider() {
super(
keyForBuild(
Label.parseCanonicalUnchecked("@@rules_java//java/common/rules:java_runtime.bzl")));
}
}

private static class Provider extends StarlarkProviderWrapper<JavaRuntimeInfo> {

private Provider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public final class JavaToolchainProvider extends StarlarkInfoWrapper {
new BuiltinsProvider();
public static final StarlarkProviderWrapper<JavaToolchainProvider> RULES_JAVA_PROVIDER =
new RulesJavaProvider();
public static final StarlarkProviderWrapper<JavaToolchainProvider> WORKSPACE_PROVIDER =
new WorkspaceProvider();
public static final StarlarkProviderWrapper<JavaToolchainProvider> PROVIDER = new Provider();

private JavaToolchainProvider(StarlarkInfo underlying) {
Expand All @@ -69,6 +71,8 @@ public static JavaToolchainProvider wrap(Info info) throws RuleErrorException {
return LEGACY_BUILTINS_PROVIDER.wrap(info);
} else if (key.equals(RULES_JAVA_PROVIDER.getKey())) {
return RULES_JAVA_PROVIDER.wrap(info);
} else if (key.equals(WORKSPACE_PROVIDER.getKey())) {
return WORKSPACE_PROVIDER.wrap(info);
} else {
throw new RuleErrorException("expected JavaToolchainInfo, got: " + key);
}
Expand Down Expand Up @@ -331,6 +335,14 @@ private RulesJavaProvider() {
}
}

private static class WorkspaceProvider extends Provider {
private WorkspaceProvider() {
super(
keyForBuild(
Label.parseCanonicalUnchecked("@@rules_java//java/common/rules:java_toolchain.bzl")));
}
}

private static class Provider extends StarlarkProviderWrapper<JavaToolchainProvider> {
private Provider() {
this(
Expand Down

0 comments on commit 5b09ab6

Please sign in to comment.