use of com.google.devtools.build.lib.skylarkinterface.SkylarkModule 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.SkylarkModule in project bazel by bazelbuild.
the class SkylarkDocumentationCollector method collectBuiltinModule.
private static void collectBuiltinModule(Map<SkylarkModule, Class<?>> modules, Class<?> moduleClass) {
if (moduleClass.isAnnotationPresent(SkylarkModule.class)) {
SkylarkModule skylarkModule = moduleClass.getAnnotation(SkylarkModule.class);
modules.put(skylarkModule, moduleClass);
}
}
use of com.google.devtools.build.lib.skylarkinterface.SkylarkModule in project bazel by bazelbuild.
the class SkylarkDocumentationCollector method collectJavaObjects.
/**
* Collects and returns all the Java objects reachable in Skylark from (and including)
* firstClass with the corresponding SkylarkModule annotation.
*
* <p>Note that the {@link SkylarkModule} annotation for firstClass - firstModule -
* is also an input parameter, because some top level Skylark built-in objects and methods
* are not annotated on the class, but on a field referencing them.
*/
@VisibleForTesting
static void collectJavaObjects(SkylarkModule firstModule, Class<?> firstClass, Map<String, SkylarkModuleDoc> modules) {
Set<Class<?>> done = new HashSet<>();
Deque<Class<?>> toProcess = new LinkedList<>();
Map<Class<?>, SkylarkModule> annotations = new HashMap<>();
toProcess.addLast(firstClass);
annotations.put(firstClass, firstModule);
while (!toProcess.isEmpty()) {
Class<?> c = toProcess.removeFirst();
SkylarkModule annotation = annotations.get(c);
done.add(c);
if (!modules.containsKey(annotation.name())) {
modules.put(annotation.name(), new SkylarkModuleDoc(annotation, c));
}
SkylarkModuleDoc module = modules.get(annotation.name());
if (module.javaMethodsNotCollected()) {
ImmutableMap<Method, SkylarkCallable> methods = FuncallExpression.collectSkylarkMethodsWithAnnotation(c);
for (Map.Entry<Method, SkylarkCallable> entry : methods.entrySet()) {
module.addMethod(new SkylarkJavaMethodDoc(module, entry.getKey(), entry.getValue()));
}
for (Map.Entry<Method, SkylarkCallable> method : methods.entrySet()) {
Class<?> returnClass = method.getKey().getReturnType();
if (returnClass.isAnnotationPresent(SkylarkModule.class) && !done.contains(returnClass)) {
toProcess.addLast(returnClass);
annotations.put(returnClass, returnClass.getAnnotation(SkylarkModule.class));
}
}
}
}
}
use of com.google.devtools.build.lib.skylarkinterface.SkylarkModule in project bazel by bazelbuild.
the class SkylarkInterfaceUtilsTest method testGetSkylarkModuleNotFound.
@Test
public void testGetSkylarkModuleNotFound() throws Exception {
// Doesn't exist.
SkylarkModule ann = SkylarkInterfaceUtils.getSkylarkModule(MockClassZ.class);
Class<?> cls = SkylarkInterfaceUtils.getParentWithSkylarkModule(MockClassZ.class);
assertThat(ann).isNull();
assertThat(cls).isNull();
}
use of com.google.devtools.build.lib.skylarkinterface.SkylarkModule in project bazel by bazelbuild.
the class SkylarkInterfaceUtilsTest method testGetSkylarkModuleSubclassNoSubannotation.
@Test
public void testGetSkylarkModuleSubclassNoSubannotation() throws Exception {
// Falls back on superclass's annotation.
SkylarkModule ann = SkylarkInterfaceUtils.getSkylarkModule(MockClassD.class);
Class<?> cls = SkylarkInterfaceUtils.getParentWithSkylarkModule(MockClassD.class);
assertThat(ann).isNotNull();
assertThat(ann.doc()).isEqualTo("MockClassC");
assertThat(cls).isNotNull();
assertThat(cls).isEqualTo(MockClassC.class);
}
Aggregations