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);
}
}
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()));
}
}
}
}
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);
}
}
}
}
Aggregations