Search in sources :

Example 1 with ClassObjectConstructor

use of com.google.devtools.build.lib.packages.ClassObjectConstructor in project bazel by bazelbuild.

the class AbstractConfiguredTarget method getIndex.

@Override
public Object getIndex(Object key, Location loc) throws EvalException {
    if (!(key instanceof ClassObjectConstructor)) {
        throw new EvalException(loc, String.format("Type Target only supports indexing by object constructors, got %s instead", EvalUtils.getDataTypeName(key)));
    }
    ClassObjectConstructor constructor = (ClassObjectConstructor) key;
    SkylarkProviders provider = getProvider(SkylarkProviders.class);
    if (provider != null) {
        Object declaredProvider = provider.getDeclaredProvider(constructor.getKey());
        if (declaredProvider != null) {
            return declaredProvider;
        }
    }
    // Either provider or declaredProvider is null
    throw new EvalException(loc, String.format("Object of type Target doesn't contain declared provider %s", constructor.getPrintableName()));
}
Also used : ClassObjectConstructor(com.google.devtools.build.lib.packages.ClassObjectConstructor) ClassObject(com.google.devtools.build.lib.syntax.ClassObject) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Example 2 with ClassObjectConstructor

use of com.google.devtools.build.lib.packages.ClassObjectConstructor in project bazel by bazelbuild.

the class RuleConfiguredTargetBuilder method addNativeDeclaredProvider.

/**
   * Adds a "declared provider" defined in native code to the rule.
   * Use this method for declared providers in definitions of native rules.
   *
   * Use {@link #addSkylarkDeclaredProvider(SkylarkClassObject, Location)}
   * for Skylark rule implementations.
   */
public RuleConfiguredTargetBuilder addNativeDeclaredProvider(SkylarkClassObject provider) {
    ClassObjectConstructor constructor = provider.getConstructor();
    Preconditions.checkState(constructor.isExported());
    skylarkDeclaredProviders.put(constructor.getKey(), provider);
    return this;
}
Also used : ClassObjectConstructor(com.google.devtools.build.lib.packages.ClassObjectConstructor)

Example 3 with ClassObjectConstructor

use of com.google.devtools.build.lib.packages.ClassObjectConstructor in project bazel by bazelbuild.

the class RuleConfiguredTargetBuilder method addSkylarkDeclaredProvider.

/**
   * Adds a "declared provider" defined in Skylark to the rule.
   * Use this method for declared providers defined in Skyark.
   *
   * Use {@link #addNativeDeclaredProvider(SkylarkClassObject)} in definitions of
   * native rules.
   */
public RuleConfiguredTargetBuilder addSkylarkDeclaredProvider(SkylarkClassObject provider, Location loc) throws EvalException {
    ClassObjectConstructor constructor = provider.getConstructor();
    SkylarkProviderValidationUtil.validateAndThrowEvalException(constructor.getPrintableName(), provider, loc);
    if (!constructor.isExported()) {
        throw new EvalException(constructor.getLocation(), "All providers must be top level values");
    }
    skylarkDeclaredProviders.put(constructor.getKey(), provider);
    return this;
}
Also used : ClassObjectConstructor(com.google.devtools.build.lib.packages.ClassObjectConstructor) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Example 4 with ClassObjectConstructor

use of com.google.devtools.build.lib.packages.ClassObjectConstructor in project bazel by bazelbuild.

the class AbstractConfiguredTarget method containsKey.

@Override
public boolean containsKey(Object key, Location loc) throws EvalException {
    if (!(key instanceof ClassObjectConstructor)) {
        throw new EvalException(loc, String.format("Type Target only supports querying by object constructors, got %s instead", EvalUtils.getDataTypeName(key)));
    }
    ClassObjectConstructor constructor = (ClassObjectConstructor) key;
    SkylarkProviders provider = getProvider(SkylarkProviders.class);
    if (provider != null) {
        Object declaredProvider = provider.getDeclaredProvider(constructor.getKey());
        if (declaredProvider != null) {
            return true;
        }
    }
    return false;
}
Also used : ClassObjectConstructor(com.google.devtools.build.lib.packages.ClassObjectConstructor) ClassObject(com.google.devtools.build.lib.syntax.ClassObject) EvalException(com.google.devtools.build.lib.syntax.EvalException)

Aggregations

ClassObjectConstructor (com.google.devtools.build.lib.packages.ClassObjectConstructor)4 EvalException (com.google.devtools.build.lib.syntax.EvalException)3 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)2