Search in sources :

Example 1 with SkylarkSignature

use of com.google.devtools.build.lib.skylarkinterface.SkylarkSignature in project bazel by bazelbuild.

the class Runtime method registerModuleGlobals.

/**
   * Registers global fields with SkylarkSignature into the specified Environment.
   * @param env the Environment into which to register fields.
   * @param moduleClass the Class object containing globals.
   */
public static void registerModuleGlobals(Environment env, Class<?> moduleClass) {
    try {
        if (moduleClass.isAnnotationPresent(SkylarkModule.class)) {
            env.setup(moduleClass.getAnnotation(SkylarkModule.class).name(), moduleClass.getConstructor().newInstance());
        }
        for (Field field : moduleClass.getDeclaredFields()) {
            if (field.isAnnotationPresent(SkylarkSignature.class)) {
                // Fields in Skylark modules are sometimes private.
                // Nevertheless they have to be annotated with SkylarkSignature.
                field.setAccessible(true);
                SkylarkSignature annotation = field.getAnnotation(SkylarkSignature.class);
                Object value = field.get(null);
                // Ignore function factories and non-global functions
                if (!(value instanceof BuiltinFunction.Factory || (value instanceof BaseFunction && !annotation.objectType().equals(Object.class)))) {
                    env.setup(annotation.name(), value);
                }
            }
        }
    } catch (ReflectiveOperationException e) {
        throw new AssertionError(e);
    }
}
Also used : Field(java.lang.reflect.Field) SkylarkSignature(com.google.devtools.build.lib.skylarkinterface.SkylarkSignature)

Example 2 with SkylarkSignature

use of com.google.devtools.build.lib.skylarkinterface.SkylarkSignature in project bazel by bazelbuild.

the class SkylarkDocumentationCollector method collectBuiltinDoc.

private static void collectBuiltinDoc(Map<String, SkylarkModuleDoc> modules, Field[] fields) {
    for (Field field : fields) {
        if (field.isAnnotationPresent(SkylarkSignature.class)) {
            SkylarkSignature skylarkSignature = field.getAnnotation(SkylarkSignature.class);
            Class<?> moduleClass = skylarkSignature.objectType();
            SkylarkModule skylarkModule = moduleClass.equals(Object.class) ? getTopLevelModule() : Runtime.getCanonicalRepresentation(moduleClass).getAnnotation(SkylarkModule.class);
            if (skylarkModule == null) {
                // TODO(bazel-team): we currently have undocumented methods on undocumented data
                // structures, namely java.util.List. Remove this case when we are done.
                Preconditions.checkState(!skylarkSignature.documented());
                Preconditions.checkState(moduleClass == List.class);
            } else {
                if (!modules.containsKey(skylarkModule.name())) {
                    modules.put(skylarkModule.name(), new SkylarkModuleDoc(skylarkModule, moduleClass));
                }
                SkylarkModuleDoc module = modules.get(skylarkModule.name());
                module.addMethod(new SkylarkBuiltinMethodDoc(module, skylarkSignature, field.getType()));
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) SkylarkModule(com.google.devtools.build.lib.skylarkinterface.SkylarkModule) List(java.util.List) LinkedList(java.util.LinkedList) SkylarkBuiltinMethodDoc(com.google.devtools.build.docgen.skylark.SkylarkBuiltinMethodDoc) SkylarkSignature(com.google.devtools.build.lib.skylarkinterface.SkylarkSignature) SkylarkModuleDoc(com.google.devtools.build.docgen.skylark.SkylarkModuleDoc)

Example 3 with SkylarkSignature

use of com.google.devtools.build.lib.skylarkinterface.SkylarkSignature in project bazel by bazelbuild.

the class SkylarkSignatureProcessor method configureSkylarkFunctions.

/**
   * Configure all BaseFunction-s in a class from their @SkylarkSignature annotations
   * @param type a class containing BuiltinFunction fields that need be configured.
   * This function is typically called in a static block to initialize a class,
   * i.e. a class {@code Foo} containing @SkylarkSignature annotations would end with
   * {@code static { SkylarkSignatureProcessor.configureSkylarkFunctions(Foo.class); }}
   */
public static void configureSkylarkFunctions(Class<?> type) {
    for (Field field : type.getDeclaredFields()) {
        if (field.isAnnotationPresent(SkylarkSignature.class)) {
            // The annotated fields are often private, but we need access them.
            field.setAccessible(true);
            SkylarkSignature annotation = field.getAnnotation(SkylarkSignature.class);
            Object value = null;
            try {
                value = Preconditions.checkNotNull(field.get(null), String.format("Error while trying to configure %s.%s: its value is null", type, field));
                if (BaseFunction.class.isAssignableFrom(field.getType())) {
                    BaseFunction function = (BaseFunction) value;
                    if (!function.isConfigured()) {
                        function.configure(annotation);
                    }
                    Class<?> nameSpace = function.getObjectType();
                    if (nameSpace != null) {
                        Preconditions.checkState(!(function instanceof BuiltinFunction.Factory));
                        nameSpace = Runtime.getCanonicalRepresentation(nameSpace);
                        Runtime.registerFunction(nameSpace, function);
                    }
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(String.format("Error while trying to configure %s.%s (value %s)", type, field, value), e);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) SkylarkSignature(com.google.devtools.build.lib.skylarkinterface.SkylarkSignature)

Aggregations

SkylarkSignature (com.google.devtools.build.lib.skylarkinterface.SkylarkSignature)3 Field (java.lang.reflect.Field)3 SkylarkBuiltinMethodDoc (com.google.devtools.build.docgen.skylark.SkylarkBuiltinMethodDoc)1 SkylarkModuleDoc (com.google.devtools.build.docgen.skylark.SkylarkModuleDoc)1 SkylarkModule (com.google.devtools.build.lib.skylarkinterface.SkylarkModule)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1