NativeScript / android-dts-generator

A tool that generates TypeScript declaration files (.d.ts) from Jars
90 stars 23 forks source link

Recover method parameter names for generated files #73

Closed phudson closed 1 year ago

phudson commented 2 years ago

Replacing getMethodParamSignature with the below (it's a drop-in backwards-compatible replacement) means that most parameter names are recovered (rather than being param0, param1 etc).

I'd do a pull request if I knew how :)

private String getMethodParamSignature(JavaClass clazz, TypeDefinition typeDefinition, Method m) {

    LocalVariableTable table = m.getLocalVariableTable();

    StringBuilder sb = new StringBuilder();
    sb.append("(");
    int idx = 0;
    for (Type type : this.getArgumentTypes(m)) {
        if (idx > 0) {
            sb.append(", ");
        }
        int localVarIndex;
        if (!m.isStatic()) {
            localVarIndex = idx + 1;

        } else {
            localVarIndex = idx;
        }
        if (table != null && table.getLocalVariable(localVarIndex, 0) != null) {
            sb.append(table.getLocalVariable(localVarIndex, 0).getName());
        } else {
            sb.append("param");
            sb.append(idx);
        }
        idx+=1;
        sb.append(": ");

        String paramTypeName = getTypeScriptTypeFromJavaType(type, typeDefinition);

        // TODO: Pete:
        if (paramTypeName.startsWith("java.util.function")) {
            sb.append("any /* " + paramTypeName + "*/");
        } else {
            addReference(type);
            sb.append(paramTypeName);
        }
    }
    sb.append(")");
    String sig = sb.toString();
    return sig;
}