Skip to content

Commit

Permalink
Inline JavaInfo internal providers as fields instead of a `Transiti…
Browse files Browse the repository at this point in the history
…veInfoProviderMap`

This makes progress towards rewriting `JavaInfo` in Starlark. The primary purpose of this (and the next few changes) is to first make the native provider resemble what the Starlark implementation will eventually look like. This has two benefits:
- Early identification of potential blockers - i.e. required refactoring or API changes
- Ensure we won't eventually cause a performance regression[^1]

This does make the class look worse but only temporarily - most of this code will go away once we switch over the Starlark provider.

PiperOrigin-RevId: 534754632
Change-Id: Ib5f776357b290e1f03e0c4e318c5a3c8bd331773
  • Loading branch information
hvadehra authored and copybara-github committed May 24, 2023
1 parent 12cb338 commit a31baa0
Showing 1 changed file with 78 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ private static <T> T nullIfNone(Object object, Class<T> type) {
JavaCcInfoProvider.class,
JavaModuleFlagsProvider.class);

private final TransitiveInfoProviderMap providers;
private final JavaCompilationArgsProvider providerJavaCompilationArgs;
private final JavaSourceJarsProvider providerJavaSourceJars;
private final JavaRuleOutputJarsProvider providerJavaRuleOutputJars;
private final JavaGenJarsProvider providerJavaGenJars;
private final JavaCompilationInfoProvider providerJavaCompilationInfo;
private final JavaCcInfoProvider providerJavaCcInfo;
private final JavaModuleFlagsProvider providerModuleFlags;
private final JavaPluginInfo providerJavaPlugin;

/*
* Contains the .jar files to be put on the runtime classpath by the configured target.
Expand All @@ -100,12 +107,37 @@ private static <T> T nullIfNone(Object object, Class<T> type) {
private final boolean neverlink;

public TransitiveInfoProviderMap getProviders() {
return providers;
TransitiveInfoProviderMapBuilder builder = new TransitiveInfoProviderMapBuilder();
if (providerJavaCompilationArgs != null) {
builder.add(providerJavaCompilationArgs);
}
if (providerJavaSourceJars != null) {
builder.add(providerJavaSourceJars);
}
if (providerJavaRuleOutputJars != null) {
builder.add(providerJavaRuleOutputJars);
}
if (providerJavaGenJars != null) {
builder.add(providerJavaGenJars);
}
if (providerJavaCompilationInfo != null) {
builder.add(providerJavaCompilationInfo);
}
if (providerJavaCcInfo != null) {
builder.add(providerJavaCcInfo);
}
if (providerModuleFlags != null) {
builder.add(providerModuleFlags);
}
if (providerJavaPlugin != null) {
builder.put(providerJavaPlugin);
}
return builder.build();
}

@Nullable
public JavaPluginInfo getJavaPluginInfo() {
return providers.get(JavaPluginInfo.PROVIDER);
return providerJavaPlugin;
}

/**
Expand Down Expand Up @@ -196,10 +228,25 @@ public static <C extends TransitiveInfoProvider> Stream<C> streamProviders(
/** Returns the instance for the provided providerClass, or <tt>null</tt> if not present. */
// TODO(adonovan): rename these three overloads of getProvider to avoid
// confusion with the unrelated no-arg Info.getProvider method.
@SuppressWarnings("UngroupedOverloads")
@SuppressWarnings({"UngroupedOverloads", "unchecked"})
@Nullable
public <P extends TransitiveInfoProvider> P getProvider(Class<P> providerClass) {
return providers.getProvider(providerClass);
if (providerClass == JavaCompilationArgsProvider.class) {
return (P) providerJavaCompilationArgs;
} else if (providerClass == JavaSourceJarsProvider.class) {
return (P) providerJavaSourceJars;
} else if (providerClass == JavaRuleOutputJarsProvider.class) {
return (P) providerJavaRuleOutputJars;
} else if (providerClass == JavaGenJarsProvider.class) {
return (P) providerJavaGenJars;
} else if (providerClass == JavaCompilationInfoProvider.class) {
return (P) providerJavaCompilationInfo;
} else if (providerClass == JavaCcInfoProvider.class) {
return (P) providerJavaCcInfo;
} else if (providerClass == JavaModuleFlagsProvider.class) {
return (P) providerModuleFlags;
}
throw new IllegalArgumentException("unexpected provider: " + providerClass);
}

/**
Expand Down Expand Up @@ -248,9 +295,16 @@ private JavaInfo(
Location creationLocation) {
super(creationLocation);
this.directRuntimeJars = directRuntimeJars;
this.providers = providers;
this.neverlink = neverlink;
this.javaConstraints = javaConstraints;
this.providerJavaCompilationArgs = providers.getProvider(JavaCompilationArgsProvider.class);
this.providerJavaSourceJars = providers.getProvider(JavaSourceJarsProvider.class);
this.providerJavaRuleOutputJars = providers.getProvider(JavaRuleOutputJarsProvider.class);
this.providerJavaGenJars = providers.getProvider(JavaGenJarsProvider.class);
this.providerJavaCompilationInfo = providers.getProvider(JavaCompilationInfoProvider.class);
this.providerJavaCcInfo = providers.getProvider(JavaCcInfoProvider.class);
this.providerModuleFlags = providers.getProvider(JavaModuleFlagsProvider.class);
this.providerJavaPlugin = providers.get(JavaPluginInfo.PROVIDER);
}

@Override
Expand Down Expand Up @@ -293,7 +347,7 @@ public Depset getFullCompileTimeJars() {
@Override
public Sequence<Artifact> getSourceJars() {
// TODO(#4221) change return type to NestedSet<Artifact>
JavaSourceJarsProvider provider = providers.getProvider(JavaSourceJarsProvider.class);
JavaSourceJarsProvider provider = providerJavaSourceJars;
ImmutableList<Artifact> sourceJars =
provider == null ? ImmutableList.of() : provider.getSourceJars();
return StarlarkList.immutableCopyOf(sourceJars);
Expand Down Expand Up @@ -432,12 +486,27 @@ public boolean equals(Object otherObject) {
}

JavaInfo other = (JavaInfo) otherObject;
return providers.equals(other.providers);
return Objects.equals(providerJavaCompilationArgs, other.providerJavaCompilationArgs)
&& Objects.equals(providerJavaSourceJars, other.providerJavaSourceJars)
&& Objects.equals(providerJavaRuleOutputJars, other.providerJavaRuleOutputJars)
&& Objects.equals(providerJavaGenJars, other.providerJavaGenJars)
&& Objects.equals(providerJavaCompilationInfo, other.providerJavaCompilationInfo)
&& Objects.equals(providerJavaCcInfo, other.providerJavaCcInfo)
&& Objects.equals(providerModuleFlags, other.providerModuleFlags)
&& Objects.equals(providerJavaPlugin, other.providerJavaPlugin);
}

@Override
public int hashCode() {
return providers.hashCode();
return Objects.hash(
providerJavaCompilationArgs,
providerJavaSourceJars,
providerJavaRuleOutputJars,
providerJavaGenJars,
providerJavaCompilationInfo,
providerJavaCcInfo,
providerModuleFlags,
providerJavaPlugin);
}

/** Provider class for {@link JavaInfo} objects. */
Expand Down

0 comments on commit a31baa0

Please sign in to comment.