use of com.google.devtools.build.docgen.skylark.SkylarkJavaMethodDoc in project bazel by bazelbuild.
the class SkylarkDocumentationProcessor method getCommandLineAPIDoc.
/**
* Returns the API doc for the specified Skylark object in a command line printable format,
* params[0] identifies either a module or a top-level object, the optional params[1] identifies a
* method in the module.<br>
* Returns null if no Skylark object is found.
*/
public static String getCommandLineAPIDoc(String[] params) throws ClassPathException {
Map<String, SkylarkModuleDoc> modules = SkylarkDocumentationCollector.collectModules();
SkylarkModuleDoc toplevelModuleDoc = modules.get(SkylarkDocumentationCollector.getTopLevelModule().name());
if (modules.containsKey(params[0])) {
// Top level module
SkylarkModuleDoc module = modules.get(params[0]);
if (params.length == 1) {
String moduleName = module.getAnnotation().name();
StringBuilder sb = new StringBuilder();
sb.append(moduleName).append("\n\t").append(module.getAnnotation().doc()).append("\n");
// Print the signature of all built-in methods
for (SkylarkBuiltinMethodDoc method : module.getBuiltinMethods().values()) {
printBuiltinFunctionDoc(moduleName, method, sb);
}
// Print all Java methods
for (SkylarkJavaMethodDoc method : module.getJavaMethods()) {
printJavaFunctionDoc(method, sb);
}
return DocgenConsts.toCommandLineFormat(sb.toString());
} else {
return getFunctionDoc(module.getAnnotation().name(), params[1], module);
}
} else if (toplevelModuleDoc.getBuiltinMethods().containsKey(params[0])) {
// Top level object / function
return getFunctionDoc(null, params[0], toplevelModuleDoc);
}
return null;
}
use of com.google.devtools.build.docgen.skylark.SkylarkJavaMethodDoc 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.docgen.skylark.SkylarkJavaMethodDoc in project bazel by bazelbuild.
the class SkylarkDocumentationProcessor method getFunctionDoc.
private static String getFunctionDoc(String moduleName, String methodName, SkylarkModuleDoc module) {
if (module.getBuiltinMethods().containsKey(methodName)) {
// Create the doc for the built-in function
SkylarkBuiltinMethodDoc method = module.getBuiltinMethods().get(methodName);
StringBuilder sb = new StringBuilder();
printBuiltinFunctionDoc(moduleName, method, sb);
sb.append(method.getParams());
return DocgenConsts.removeDuplicatedNewLines(DocgenConsts.toCommandLineFormat(sb.toString()));
} else {
// Search if there are matching Java functions
StringBuilder sb = new StringBuilder();
boolean foundMatchingMethod = false;
for (SkylarkJavaMethodDoc method : module.getJavaMethods()) {
if (method.getName().equals(methodName)) {
printJavaFunctionDoc(method, sb);
foundMatchingMethod = true;
}
}
if (foundMatchingMethod) {
return DocgenConsts.toCommandLineFormat(sb.toString());
}
}
return null;
}
Aggregations