Skip to content

Commit

Permalink
Encode JavaInfo provider type.
Browse files Browse the repository at this point in the history
da1aba9 introduces `BuiltinsJavaInfo`, which uses a different provider type (`LEGACY_BUILTINS_PROVIDER`). This information needs to be encoded along with the serialized provider fields for the deserializer to re-instantiate it back to `BuiltinsJavaInfo` instead of `JavaInfo`.

PiperOrigin-RevId: 695991580
Change-Id: I3b7a5b87a2619d83ca6e7d3a3eee0349769b0640
  • Loading branch information
jin authored and copybara-github committed Nov 13, 2024
1 parent 60d1759 commit dc4fb9c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/rules/cpp",
"//src/main/java/com/google/devtools/build/lib/skyframe:bzl_load_value",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec:serialization-constant",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/core",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/java",
Expand Down Expand Up @@ -107,7 +108,6 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/analysis:config/invalid_configuration_exception",
"//src/main/java/com/google/devtools/build/lib/analysis:config/toolchain_type_requirement",
"//src/main/java/com/google/devtools/build/lib/analysis:configured_target",
"//src/main/java/com/google/devtools/build/lib/analysis:file_provider",
"//src/main/java/com/google/devtools/build/lib/analysis:platform_options",
"//src/main/java/com/google/devtools/build/lib/analysis:provider_collection",
"//src/main/java/com/google/devtools/build/lib/analysis:rule_definition_environment",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.google.devtools.build.lib.skyframe.serialization.DynamicCodec.FieldHandler;
import com.google.devtools.build.lib.skyframe.serialization.SerializationContext;
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CcInfoApi;
import com.google.devtools.build.lib.starlarkbuildapi.java.JavaInfoApi;
import com.google.devtools.build.lib.starlarkbuildapi.java.JavaModuleFlagsProviderApi;
Expand All @@ -66,14 +67,19 @@

/** A Starlark declared provider that encapsulates all providers that are needed by Java rules. */
@Immutable
public class JavaInfo extends NativeInfo
implements JavaInfoApi<Artifact, JavaOutput, JavaPluginData> {
public sealed class JavaInfo extends NativeInfo
implements JavaInfoApi<Artifact, JavaOutput, JavaPluginData>
permits JavaInfo.BuiltinsJavaInfo, JavaInfo.RulesJavaJavaInfo {

public static final String STARLARK_NAME = "JavaInfo";

@SerializationConstant
public static final JavaInfoProvider LEGACY_BUILTINS_PROVIDER = new BuiltinsJavaInfoProvider();

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

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

// Ideally we would check if the target has a JavaInfo, but this check predates the Starlark
// sandwich and consumers depend on this returning `false` for java_binary/java_test targets. When
Expand Down Expand Up @@ -137,7 +143,9 @@ static <T> T nullIfNone(Object object, Class<T> type) {
return object != Starlark.NONE ? type.cast(object) : null;
}

public static final JavaInfo EMPTY = JavaInfo.Builder.create().build();
static final JavaInfo EMPTY_JAVA_INFO_FOR_TESTING = Builder.create().build();
static final BuiltinsJavaInfo EMPTY_BUILTINS_JAVA_INFO_FOR_TESTING =
(BuiltinsJavaInfo) new Builder().setProvider(LEGACY_BUILTINS_PROVIDER).build();

private final JavaCompilationArgsProvider providerJavaCompilationArgs;
private final JavaSourceJarsProvider providerJavaSourceJars;
Expand Down Expand Up @@ -504,7 +512,8 @@ public int hashCode() {
providerJavaPlugin);
}

private static class BuiltinsJavaInfo extends JavaInfo {
@VisibleForTesting // package-private for testing.
static final class BuiltinsJavaInfo extends JavaInfo {

private BuiltinsJavaInfo(StructImpl javaInfo)
throws EvalException, TypeException, RuleErrorException {
Expand Down Expand Up @@ -545,7 +554,7 @@ public JavaInfoProvider getProvider() {
}
}

private static class RulesJavaJavaInfo extends JavaInfo {
static final class RulesJavaJavaInfo extends JavaInfo {

private RulesJavaJavaInfo(StructImpl javaInfo)
throws EvalException, TypeException, RuleErrorException {
Expand All @@ -559,7 +568,7 @@ public JavaInfoProvider getProvider() {
}

/** Legacy Provider class for {@link JavaInfo} objects. */
public static class BuiltinsJavaInfoProvider extends JavaInfoProvider {
public static final class BuiltinsJavaInfoProvider extends JavaInfoProvider {
private BuiltinsJavaInfoProvider() {
super(
keyForBuiltins(Label.parseCanonicalUnchecked("@_builtins//:common/java/java_info.bzl")));
Expand All @@ -573,7 +582,7 @@ protected JavaInfo makeNewInstance(StructImpl info)
}

/** Legacy Provider class for {@link JavaInfo} objects. */
public static class RulesJavaJavaInfoProvider extends JavaInfoProvider {
public static final class RulesJavaJavaInfoProvider extends JavaInfoProvider {
private RulesJavaJavaInfoProvider() {
super(keyForBuild(Label.parseCanonicalUnchecked("//java/private:java_info.bzl")));
}
Expand All @@ -586,8 +595,8 @@ protected JavaInfo makeNewInstance(StructImpl info)
}

/** Provider class for {@link JavaInfo} objects. */
public static class JavaInfoProvider extends StarlarkProviderWrapper<JavaInfo>
implements com.google.devtools.build.lib.packages.Provider {
public static sealed class JavaInfoProvider extends StarlarkProviderWrapper<JavaInfo>
implements Provider permits BuiltinsJavaInfoProvider, RulesJavaJavaInfoProvider {
private JavaInfoProvider() {
this(
keyForBuild(
Expand Down Expand Up @@ -795,6 +804,12 @@ public Class<? extends JavaInfo> getEncodedClass() {
@Override
public void serialize(SerializationContext context, JavaInfo obj, CodedOutputStream codedOut)
throws SerializationException, IOException {
switch (obj.getProvider()) {
case BuiltinsJavaInfoProvider unused -> codedOut.writeBoolNoTag(true);
case RulesJavaJavaInfoProvider unused ->
throw new UnsupportedOperationException("not implemented");
case JavaInfoProvider unused -> codedOut.writeBoolNoTag(false);
}
for (FieldHandler handler : handlers) {
handler.serialize(context, codedOut, obj);
}
Expand All @@ -808,10 +823,14 @@ public DeferredValue<JavaInfo> deserializeDeferred(

JavaInfo obj;
try {
obj = (JavaInfo) unsafe().allocateInstance(JavaInfo.class);
obj =
codedIn.readBool()
? (BuiltinsJavaInfo) unsafe().allocateInstance(BuiltinsJavaInfo.class)
: (JavaInfo) unsafe().allocateInstance(JavaInfo.class);
} catch (InstantiationException e) {
throw new SerializationException("Could not instantiate JavaInfo with Unsafe", e);
}

for (FieldHandler handler : handlers) {
handler.deserialize(context, codedIn, obj);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.devtools.build.lib.analysis.config.BuildOptions.MapBackedChecksumCache;
import com.google.devtools.build.lib.analysis.config.BuildOptions.OptionsChecksumCache;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.rules.java.JavaInfo.BuiltinsJavaInfo;
import com.google.devtools.build.lib.skyframe.serialization.testutils.Dumper;
import com.google.devtools.build.lib.skyframe.serialization.testutils.SerializationDepsUtils;
import com.google.devtools.build.lib.skyframe.serialization.testutils.SerializationTester;
Expand All @@ -33,12 +34,24 @@ public class JavaInfoCodecTest extends BuildViewTestCase {

@Test
public void emptyJavaInfo_canBeSerializedAndDeserialized() throws Exception {
new SerializationTester(JavaInfo.EMPTY)
new SerializationTester(JavaInfo.EMPTY_JAVA_INFO_FOR_TESTING)
.makeMemoizingAndAllowFutureBlocking(/* allowFutureBlocking= */ true)
.setVerificationFunction((in, out) -> assertThat(in).isEqualTo(out))
.runTests();
}

@Test
public void emptyBuiltinJavaInfo_canBeSerializedAndDeserialized() throws Exception {
new SerializationTester(JavaInfo.EMPTY_BUILTINS_JAVA_INFO_FOR_TESTING)
.makeMemoizingAndAllowFutureBlocking(/* allowFutureBlocking= */ true)
.setVerificationFunction(
(in, out) -> {
assertThat(in).isEqualTo(out);
assertThat(out.getClass()).isEqualTo(BuiltinsJavaInfo.class);
})
.runTests();
}

@Test
public void javaInfo_canBeSerializedAndDeserialized() throws Exception {
scratch.file(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,27 @@ public class JavaInfoTest {

@Test
public void getTransitiveRuntimeJars_noJavaCompilationArgsProvider() {
assertThat(JavaInfo.EMPTY.getTransitiveRuntimeJars().isEmpty()).isTrue();
assertThat(JavaInfo.EMPTY_JAVA_INFO_FOR_TESTING.getTransitiveRuntimeJars().isEmpty()).isTrue();
}

@Test
public void getTransitiveCompileTimeJarsJars_noJavaCompilationArgsProvider() {
assertThat(JavaInfo.EMPTY.getTransitiveCompileTimeJars().isEmpty()).isTrue();
assertThat(JavaInfo.EMPTY_JAVA_INFO_FOR_TESTING.getTransitiveCompileTimeJars().isEmpty())
.isTrue();
}

@Test
public void getCompileTimeJarsJars_noJavaCompilationArgsProvider() {
assertThat(JavaInfo.EMPTY.getCompileTimeJars().isEmpty()).isTrue();
assertThat(JavaInfo.EMPTY_JAVA_INFO_FOR_TESTING.getCompileTimeJars().isEmpty()).isTrue();
}

@Test
public void getFullCompileTimeJarsJars_noJavaCompilationArgsProvider() {
assertThat(JavaInfo.EMPTY.getFullCompileTimeJars().isEmpty()).isTrue();
assertThat(JavaInfo.EMPTY_JAVA_INFO_FOR_TESTING.getFullCompileTimeJars().isEmpty()).isTrue();
}

@Test
public void getSourceJars_noJavaSourceJarsProvider() {
assertThat(JavaInfo.EMPTY.getSourceJars()).isEmpty();
assertThat(JavaInfo.EMPTY_JAVA_INFO_FOR_TESTING.getSourceJars()).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4951,7 +4951,7 @@ def _impl(ctx):

@Test
public void testNativeJavaInfoPrintableType_isJavaInfo() {
String type = JavaStarlarkCommon.printableType(JavaInfo.EMPTY);
String type = JavaStarlarkCommon.printableType(JavaInfo.EMPTY_JAVA_INFO_FOR_TESTING);

assertThat(type).isEqualTo("JavaInfo");
}
Expand Down

0 comments on commit dc4fb9c

Please sign in to comment.