Search in sources :

Example 1 with StarlarkBuiltin

use of net.starlark.java.annot.StarlarkBuiltin in project copybara by google.

the class ModuleSupplier method findClosestStarlarkBuiltinName.

private String findClosestStarlarkBuiltinName(Object o) {
    Class<?> cls = o.getClass();
    while (cls != null && cls != Object.class) {
        StarlarkBuiltin annotation = cls.getAnnotation(StarlarkBuiltin.class);
        if (annotation != null) {
            return annotation.name();
        }
        cls = cls.getSuperclass();
    }
    throw new IllegalStateException("Cannot find @StarlarkBuiltin for " + o.getClass());
}
Also used : StarlarkBuiltin(net.starlark.java.annot.StarlarkBuiltin)

Example 2 with StarlarkBuiltin

use of net.starlark.java.annot.StarlarkBuiltin in project copybara by google.

the class SkylarkParser method createEnvironment.

/**
 * Create the environment for all evaluations (will be shared between all the dependent files
 * loaded).
 */
private ImmutableMap<String, Object> createEnvironment(ModuleSet moduleSet, Supplier<ImmutableMap<String, ConfigFile>> configFilesSupplier) {
    Map<String, Object> env = Maps.newHashMap();
    for (Entry<String, Object> module : moduleSet.getModules().entrySet()) {
        logger.atInfo().log("Creating variable for %s", module.getKey());
        if (module.getValue() instanceof LabelsAwareModule) {
            ((LabelsAwareModule) module.getValue()).setAllConfigResources(configFilesSupplier);
        }
        // Modules shouldn't use the same name
        env.put(module.getKey(), module.getValue());
    }
    for (Class<?> module : modules) {
        logger.atInfo().log("Creating variable for %s", module.getName());
        // Create the module object and associate it with the functions
        ImmutableMap.Builder<String, Object> envBuilder = ImmutableMap.builder();
        try {
            StarlarkBuiltin annot = StarlarkAnnotations.getStarlarkBuiltin(module);
            if (annot != null) {
                envBuilder.put(annot.name(), module.getConstructor().newInstance());
            } else if (module.isAnnotationPresent(Library.class)) {
                Starlark.addMethods(envBuilder, module.getConstructor().newInstance());
            }
        } catch (ReflectiveOperationException e) {
            throw new LinkageError(e.getMessage(), e);
        }
        env.putAll(envBuilder.buildOrThrow());
        // Add the options to the module that require them
        if (OptionsAwareModule.class.isAssignableFrom(module)) {
            ((OptionsAwareModule) env.get(getModuleName(module))).setOptions(moduleSet.getOptions());
        }
        if (LabelsAwareModule.class.isAssignableFrom(module)) {
            ((LabelsAwareModule) env.get(getModuleName(module))).setAllConfigResources(configFilesSupplier);
        }
    }
    return ImmutableMap.copyOf(env);
}
Also used : StarlarkBuiltin(net.starlark.java.annot.StarlarkBuiltin) ImmutableMap(com.google.common.collect.ImmutableMap) Library(com.google.copybara.doc.annotations.Library)

Aggregations

StarlarkBuiltin (net.starlark.java.annot.StarlarkBuiltin)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Library (com.google.copybara.doc.annotations.Library)1