Search in sources :

Example 16 with SingleVariableDeclaration

use of com.google.devtools.j2objc.ast.SingleVariableDeclaration in project j2objc by google.

the class TypeGenerator method getMethodSignature.

/**
   * Create an Objective-C method signature string.
   */
protected String getMethodSignature(MethodDeclaration m) {
    StringBuilder sb = new StringBuilder();
    ExecutableElement element = m.getExecutableElement();
    char prefix = Modifier.isStatic(m.getModifiers()) ? '+' : '-';
    String returnType = nameTable.getObjCType(element.getReturnType());
    String selector = nameTable.getMethodSelector(element);
    if (m.isConstructor()) {
        returnType = "instancetype";
    } else if (selector.equals("hash")) {
        // Explicitly test hashCode() because of NSObject's hash return value.
        returnType = "NSUInteger";
    }
    sb.append(UnicodeUtils.format("%c (%s%s)", prefix, returnType, nullability(element)));
    List<SingleVariableDeclaration> params = m.getParameters();
    String[] selParts = selector.split(":");
    if (params.isEmpty()) {
        assert selParts.length == 1 && !selector.endsWith(":");
        sb.append(selParts[0]);
    } else {
        assert params.size() == selParts.length;
        int baseLength = sb.length() + selParts[0].length();
        for (int i = 0; i < params.size(); i++) {
            if (i != 0) {
                sb.append('\n');
                sb.append(pad(baseLength - selParts[i].length()));
            }
            VariableElement var = params.get(i).getVariableElement();
            String typeName = nameTable.getObjCType(var.asType());
            sb.append(UnicodeUtils.format("%s:(%s%s)%s", selParts[i], typeName, nullability(var), nameTable.getVariableShortName(var)));
        }
    }
    return sb.toString();
}
Also used : SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement)

Example 17 with SingleVariableDeclaration

use of com.google.devtools.j2objc.ast.SingleVariableDeclaration in project j2objc by google.

the class TypeImplementationGenerator method getJniFunctionSignature.

private String getJniFunctionSignature(FunctionDeclaration function) {
    StringBuilder sb = new StringBuilder();
    sb.append(nameTable.getJniType(function.getReturnType().getTypeMirror()));
    sb.append(' ');
    sb.append(function.getJniSignature()).append('(');
    sb.append("JNIEnv *_env_");
    if (Modifier.isStatic(function.getModifiers())) {
        sb.append(", jclass _cls_");
    }
    if (!function.getParameters().isEmpty()) {
        sb.append(", ");
    }
    for (Iterator<SingleVariableDeclaration> iter = function.getParameters().iterator(); iter.hasNext(); ) {
        VariableElement var = iter.next().getVariableElement();
        String paramType = nameTable.getJniType(var.asType());
        sb.append(paramType + ' ' + nameTable.getVariableBaseName(var));
        if (iter.hasNext()) {
            sb.append(", ");
        }
    }
    sb.append(')');
    return sb.toString();
}
Also used : SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) VariableElement(javax.lang.model.element.VariableElement)

Example 18 with SingleVariableDeclaration

use of com.google.devtools.j2objc.ast.SingleVariableDeclaration in project j2objc by google.

the class TypeImplementationGenerator method printJniFunctionAndWrapper.

private void printJniFunctionAndWrapper(FunctionDeclaration function) {
    // Declare the matching JNI function.
    print("JNIEXPORT ");
    print(getJniFunctionSignature(function));
    println(";\n");
    // Generate a wrapper function that calls the matching JNI function.
    print(getFunctionSignature(function));
    println(" {");
    print("  ");
    TypeMirror returnType = function.getReturnType().getTypeMirror();
    if (!TypeUtil.isVoid(returnType)) {
        if (returnType.getKind().isPrimitive()) {
            print("return ");
        } else {
            printf("return (%s) ", nameTable.getObjCType(returnType));
        }
    }
    print(function.getJniSignature());
    print("(&J2ObjC_JNIEnv");
    if (Modifier.isStatic(function.getModifiers())) {
        printf(", %s_class_()", typeName);
    }
    for (SingleVariableDeclaration param : function.getParameters()) {
        printf(", %s", nameTable.getVariableBaseName(param.getVariableElement()));
    }
    println(");");
    println("}");
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration)

Example 19 with SingleVariableDeclaration

use of com.google.devtools.j2objc.ast.SingleVariableDeclaration in project j2objc by google.

the class TypeGenerator method getFunctionSignature.

protected String getFunctionSignature(FunctionDeclaration function) {
    StringBuilder sb = new StringBuilder();
    String returnType = nameTable.getObjCType(function.getReturnType().getTypeMirror());
    returnType += returnType.endsWith("*") ? "" : " ";
    sb.append(returnType).append(function.getName()).append('(');
    for (Iterator<SingleVariableDeclaration> iter = function.getParameters().iterator(); iter.hasNext(); ) {
        VariableElement var = iter.next().getVariableElement();
        String paramType = nameTable.getObjCType(var.asType());
        paramType += (paramType.endsWith("*") ? "" : " ");
        sb.append(paramType + nameTable.getVariableShortName(var));
        if (iter.hasNext()) {
            sb.append(", ");
        }
    }
    sb.append(')');
    return sb.toString();
}
Also used : SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) VariableElement(javax.lang.model.element.VariableElement)

Aggregations

SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)19 VariableElement (javax.lang.model.element.VariableElement)13 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)10 ExecutableElement (javax.lang.model.element.ExecutableElement)8 Block (com.google.devtools.j2objc.ast.Block)7 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)7 TypeElement (javax.lang.model.element.TypeElement)7 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)6 TypeMirror (javax.lang.model.type.TypeMirror)6 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)4 SimpleName (com.google.devtools.j2objc.ast.SimpleName)4 ArrayType (com.google.devtools.j2objc.ast.ArrayType)3 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)3 FunctionDeclaration (com.google.devtools.j2objc.ast.FunctionDeclaration)3 PrimitiveType (com.google.devtools.j2objc.ast.PrimitiveType)3 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)3 SimpleType (com.google.devtools.j2objc.ast.SimpleType)3 Statement (com.google.devtools.j2objc.ast.Statement)3 Type (com.google.devtools.j2objc.ast.Type)3 UnionType (com.google.devtools.j2objc.ast.UnionType)3