Search in sources :

Example 1 with SkylarkModule

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()));
            }
        }
    }
}
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 2 with SkylarkModule

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);
    }
}
Also used : SkylarkModule(com.google.devtools.build.lib.skylarkinterface.SkylarkModule)

Example 3 with SkylarkModule

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));
                }
            }
        }
    }
}
Also used : SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) LinkedList(java.util.LinkedList) SkylarkJavaMethodDoc(com.google.devtools.build.docgen.skylark.SkylarkJavaMethodDoc) SkylarkModule(com.google.devtools.build.lib.skylarkinterface.SkylarkModule) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SkylarkModuleDoc(com.google.devtools.build.docgen.skylark.SkylarkModuleDoc) HashSet(java.util.HashSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with SkylarkModule

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();
}
Also used : SkylarkModule(com.google.devtools.build.lib.skylarkinterface.SkylarkModule) Test(org.junit.Test)

Example 5 with SkylarkModule

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);
}
Also used : SkylarkModule(com.google.devtools.build.lib.skylarkinterface.SkylarkModule) Test(org.junit.Test)

Aggregations

SkylarkModule (com.google.devtools.build.lib.skylarkinterface.SkylarkModule)8 Test (org.junit.Test)4 SkylarkModuleDoc (com.google.devtools.build.docgen.skylark.SkylarkModuleDoc)3 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 TreeMap (java.util.TreeMap)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 SkylarkBuiltinMethodDoc (com.google.devtools.build.docgen.skylark.SkylarkBuiltinMethodDoc)1 SkylarkJavaMethodDoc (com.google.devtools.build.docgen.skylark.SkylarkJavaMethodDoc)1 SkylarkCallable (com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)1 SkylarkSignature (com.google.devtools.build.lib.skylarkinterface.SkylarkSignature)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1