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();
}
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();
}
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("}");
}
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();
}
Aggregations