Gracefully handle exceptions in java_common.default_javac_opts.
Replacing checkNotNulls (which throws NPE) with throwing EvalExceptions in java_common, in order for blaze to output a nice error message instead of crashing.
// limitations under the License.
package com.google.devtools.build.lib.rules.java;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
@@ -343,7 +341,10 @@ public class JavaSkylarkCommon {
? NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER)
: hostJavabaseProvider.getMiddlemanArtifact();
JavaToolchainProvider javaToolchainProvider =
- checkNotNull(javaToolchain.getProvider(JavaToolchainProvider.class));
+ javaToolchain.getProvider(JavaToolchainProvider.class);
+ if (javaToolchain == null) {
+ throw new EvalException(null, javaToolchain.getLabel() + " is not a java_toolchain rule.");
+ }
JavaCompilationArtifacts artifacts =
helper.build(
javaSemantics,
@@ -400,9 +401,12 @@ public class JavaSkylarkCommon {
SkylarkRuleContext skylarkRuleContext, String javaToolchainAttr) throws EvalException {
RuleContext ruleContext = skylarkRuleContext.getRuleContext();
ConfiguredTarget javaToolchainConfigTarget =
- (ConfiguredTarget) checkNotNull(skylarkRuleContext.getAttr().getValue(javaToolchainAttr));
+ (ConfiguredTarget) skylarkRuleContext.getAttr().getValue(javaToolchainAttr);
JavaToolchainProvider toolchain =
- checkNotNull(javaToolchainConfigTarget.getProvider(JavaToolchainProvider.class));
+ javaToolchainConfigTarget.getProvider(JavaToolchainProvider.class);
+ if (toolchain == null) {
+ throw new EvalException(null, javaToolchainAttr + " is not a java_toolchain rule label");
+ }
return ImmutableList.copyOf(Iterables.concat(
toolchain.getJavacOptions(), ruleContext.getTokenizedStringListAttr("javacopts")));
}
