Search in sources :

Example 1 with ParamType

use of net.starlark.java.annot.ParamType in project copybara by google.

the class ModuleLoader method processStarlarkMethod.

private DocFunction processStarlarkMethod(Method method, StarlarkMethod annotation, @Nullable String prefix) {
    Type[] genericParameterTypes = method.getGenericParameterTypes();
    Param[] starlarkParams = annotation.parameters();
    if (genericParameterTypes.length < starlarkParams.length) {
        throw new IllegalStateException(String.format("Missing java parameters for: %s\n" + "%s\n" + "%s", method, Arrays.toString(genericParameterTypes), Arrays.toString(starlarkParams)));
    }
    ImmutableList.Builder<DocParam> params = ImmutableList.builder();
    Map<String, DocDefault> docDefaultsMap = stream(method.getAnnotationsByType(DocDefault.class)).collect(Collectors.toMap(DocDefault::field, identity(), (f, v) -> v));
    for (int i = 0; i < starlarkParams.length; i++) {
        Type parameterType = genericParameterTypes[i];
        Param starlarkParam = starlarkParams[i];
        // Compute the list of names of allowed types (e.g. string or bool or NoneType).
        List<String> allowedTypeNames = new ArrayList<>();
        if (starlarkParam.allowedTypes().length > 0) {
            for (ParamType param : starlarkParam.allowedTypes()) {
                allowedTypeNames.add(skylarkTypeName(param.type()) + (param.generic1() != Object.class ? " of " + skylarkTypeName(param.generic1()) : ""));
            }
        } else {
            // Otherwise use the type of the parameter variable itself.
            allowedTypeNames.add(skylarkTypeName(parameterType));
        }
        DocDefault fieldInfo = docDefaultsMap.get(starlarkParam.name());
        if (fieldInfo != null && fieldInfo.allowedTypes().length > 0) {
            allowedTypeNames = Arrays.asList(fieldInfo.allowedTypes());
        }
        params.add(new DocParam(starlarkParam.name(), fieldInfo != null ? fieldInfo.value() : emptyToNull(starlarkParam.defaultValue()), allowedTypeNames, starlarkParam.doc()));
    }
    String returnType = method.getGenericReturnType().equals(NoneType.class) || method.getGenericReturnType().equals(void.class) ? null : skylarkTypeName(method.getGenericReturnType());
    return new DocFunction(prefix != null ? prefix + "." + annotation.name() : annotation.name(), annotation.doc(), returnType, params.build(), generateFlagsInfo(method), stream(method.getAnnotationsByType(Example.class)).map(DocExample::new).collect(toImmutableList()));
}
Also used : UsesFlags(com.google.copybara.doc.annotations.UsesFlags) DocExample(com.google.copybara.doc.DocBase.DocExample) Arrays(java.util.Arrays) DocFlag(com.google.copybara.doc.DocBase.DocFlag) HtmlEscapers(com.google.common.html.HtmlEscapers) DurationConverter(com.google.copybara.jcommander.DurationConverter) DocDefault(com.google.copybara.doc.annotations.DocDefault) CharStreams(com.google.common.io.CharStreams) Map(java.util.Map) ZipFile(java.util.zip.ZipFile) Splitter(com.google.common.base.Splitter) Method(java.lang.reflect.Method) ZipEntry(java.util.zip.ZipEntry) Param(net.starlark.java.annot.Param) DocModule(com.google.copybara.doc.DocBase.DocModule) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) List(java.util.List) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) Type(java.lang.reflect.Type) NoneType(net.starlark.java.eval.NoneType) Function.identity(java.util.function.Function.identity) Pattern(com.google.re2j.Pattern) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) DocFunction(com.google.copybara.doc.DocBase.DocFunction) DocSignaturePrefix(com.google.copybara.doc.annotations.DocSignaturePrefix) DocField(com.google.copybara.doc.DocBase.DocField) Arrays.stream(java.util.Arrays.stream) SortedMap(java.util.SortedMap) Joiner(com.google.common.base.Joiner) AnnotatedElement(java.lang.reflect.AnnotatedElement) Library(com.google.copybara.doc.annotations.Library) Matcher(com.google.re2j.Matcher) Parameter(com.beust.jcommander.Parameter) WildcardType(java.lang.reflect.WildcardType) StarlarkBuiltin(net.starlark.java.annot.StarlarkBuiltin) ArrayList(java.util.ArrayList) Example(com.google.copybara.doc.annotations.Example) ImmutableList(com.google.common.collect.ImmutableList) DocParam(com.google.copybara.doc.DocBase.DocParam) ParamType(net.starlark.java.annot.ParamType) Starlark(net.starlark.java.eval.Starlark) Nullable(javax.annotation.Nullable) UTF_8(java.nio.charset.StandardCharsets.UTF_8) IOException(java.io.IOException) Field(java.lang.reflect.Field) InputStreamReader(java.io.InputStreamReader) Strings.emptyToNull(com.google.common.base.Strings.emptyToNull) ParameterizedType(java.lang.reflect.ParameterizedType) TreeMap(java.util.TreeMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) DocDefault(com.google.copybara.doc.annotations.DocDefault) ArrayList(java.util.ArrayList) DocFunction(com.google.copybara.doc.DocBase.DocFunction) ParamType(net.starlark.java.annot.ParamType) Type(java.lang.reflect.Type) NoneType(net.starlark.java.eval.NoneType) WildcardType(java.lang.reflect.WildcardType) ParamType(net.starlark.java.annot.ParamType) ParameterizedType(java.lang.reflect.ParameterizedType) DocExample(com.google.copybara.doc.DocBase.DocExample) Param(net.starlark.java.annot.Param) DocParam(com.google.copybara.doc.DocBase.DocParam) DocParam(com.google.copybara.doc.DocBase.DocParam)

Aggregations

Parameter (com.beust.jcommander.Parameter)1 Joiner (com.google.common.base.Joiner)1 Splitter (com.google.common.base.Splitter)1 Strings.emptyToNull (com.google.common.base.Strings.emptyToNull)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 HtmlEscapers (com.google.common.html.HtmlEscapers)1 CharStreams (com.google.common.io.CharStreams)1 DocExample (com.google.copybara.doc.DocBase.DocExample)1 DocField (com.google.copybara.doc.DocBase.DocField)1 DocFlag (com.google.copybara.doc.DocBase.DocFlag)1 DocFunction (com.google.copybara.doc.DocBase.DocFunction)1 DocModule (com.google.copybara.doc.DocBase.DocModule)1 DocParam (com.google.copybara.doc.DocBase.DocParam)1 DocDefault (com.google.copybara.doc.annotations.DocDefault)1 DocSignaturePrefix (com.google.copybara.doc.annotations.DocSignaturePrefix)1 Example (com.google.copybara.doc.annotations.Example)1 Library (com.google.copybara.doc.annotations.Library)1 UsesFlags (com.google.copybara.doc.annotations.UsesFlags)1 DurationConverter (com.google.copybara.jcommander.DurationConverter)1