use of com.google.devtools.j2objc.ast.FunctionInvocation in project j2objc by google.
the class Functionizer method functionizeInvocation.
private void functionizeInvocation(Expression node, ExecutableElement element, Expression receiver, List<Expression> methodArgs) {
FunctionInvocation functionInvocation = new FunctionInvocation(newFunctionElement(element), node.getTypeMirror());
List<Expression> funcArgs = functionInvocation.getArguments();
TreeUtil.moveList(methodArgs, funcArgs);
if (!ElementUtil.isStatic(element)) {
if (receiver == null) {
receiver = new ThisExpression(TreeUtil.getEnclosingTypeElement(node).asType());
}
funcArgs.add(0, TreeUtil.remove(receiver));
}
node.replaceWith(functionInvocation);
}
use of com.google.devtools.j2objc.ast.FunctionInvocation in project j2objc by google.
the class SuperMethodInvocationRewriter method endVisit.
@Override
public void endVisit(SuperMethodInvocation node) {
Expression receiver = node.getReceiver();
ExecutableElement method = node.getExecutableElement();
TypeMirror exprType = node.getTypeMirror();
assert !ElementUtil.isDefault(method) : "Default methods are handled in Functionizer.java";
if (receiver == null) {
return;
}
VariableElement var = TreeUtil.getVariableElement(receiver);
assert var != null : "Expected receiver to be a variable";
TypeMirror receiverType = var.asType();
TypeElement receiverElem = TypeUtil.asTypeElement(receiverType);
SuperMethodElementPair superMethod = new SuperMethodElementPair(receiverElem, method);
superMethods.add(superMethod);
FunctionElement element = new FunctionElement(getSuperFunctionName(superMethod), exprType, receiverElem).addParameters(receiverType, TypeUtil.ID_TYPE).addParameters(ElementUtil.asTypes(method.getParameters()));
FunctionInvocation invocation = new FunctionInvocation(element, exprType);
List<Expression> args = invocation.getArguments();
args.add(TreeUtil.remove(receiver));
String selectorExpr = UnicodeUtils.format("@selector(%s)", nameTable.getMethodSelector(method));
args.add(new NativeExpression(selectorExpr, TypeUtil.ID_TYPE));
TreeUtil.copyList(node.getArguments(), args);
node.replaceWith(invocation);
}
use of com.google.devtools.j2objc.ast.FunctionInvocation in project j2objc by google.
the class MetadataWriter method createContainerAnnotation.
// Creates the container annotation which value is an array with the repeated annotations.
private Expression createContainerAnnotation(List<Annotation> annotations) {
DeclaredType annotationType = annotations.get(0).getAnnotationMirror().getAnnotationType();
ArrayType arrayType = typeUtil.getArrayType(annotationType);
DeclaredType containerType = (DeclaredType) ElementUtil.getAnnotationValue(ElementUtil.getAnnotation(annotationType.asElement(), Repeatable.class), "value");
TypeElement containerElement = (TypeElement) containerType.asElement();
FunctionElement element = new FunctionElement("create_" + nameTable.getFullName(containerElement), containerType, containerElement);
FunctionInvocation invocation = new FunctionInvocation(element, containerType);
element.addParameters(arrayType);
List<Expression> array = annotations.stream().map(Annotation::getAnnotationMirror).map(translationUtil::createAnnotation).collect(Collectors.toList());
invocation.addArgument(translationUtil.createObjectArray(array, arrayType));
return invocation;
}
use of com.google.devtools.j2objc.ast.FunctionInvocation in project j2objc by google.
the class CastResolver method createCastCheck.
private FunctionInvocation createCastCheck(TypeMirror type, Expression expr) {
type = typeUtil.erasure(type);
TypeMirror idType = TypeUtil.ID_TYPE;
if (TypeUtil.isInterface(type) || isObjectArray(type)) {
// Interfaces and object arrays require an isInstance call.
FunctionElement element = new FunctionElement("cast_check", idType, null).addParameters(idType, TypeUtil.IOS_CLASS.asType());
FunctionInvocation invocation = new FunctionInvocation(element, idType);
invocation.addArgument(TreeUtil.remove(expr));
invocation.addArgument(new TypeLiteral(type, typeUtil));
return invocation;
} else if (TypeUtil.isArray(type) || (TypeUtil.isDeclaredType(type) && needsCastChk(expr, type))) {
// Primitive array and non-interface type casts are checked using Objective-C's
// isKindOfClass:.
TypeElement objcClass = typeUtil.getObjcClass(type);
FunctionElement checkFunction = new FunctionElement("cast_chk", idType, null).addParameters(idType, idType);
FunctionInvocation invocation = new FunctionInvocation(checkFunction, idType);
invocation.addArgument(TreeUtil.remove(expr));
ExecutableElement classElement = GeneratedExecutableElement.newMethodWithSelector("class", idType, objcClass).addModifiers(Modifier.STATIC);
MethodInvocation classInvocation = new MethodInvocation(new ExecutablePair(classElement), new SimpleName(objcClass));
invocation.addArgument(classInvocation);
return invocation;
}
return null;
}
use of com.google.devtools.j2objc.ast.FunctionInvocation in project j2objc by google.
the class SwitchRewriter method fixStringValue.
private void fixStringValue(SwitchStatement node) {
Expression expr = node.getExpression();
TypeMirror type = expr.getTypeMirror();
if (!typeUtil.isString(type)) {
return;
}
ArrayType arrayType = typeUtil.getArrayType(type);
ArrayInitializer arrayInit = new ArrayInitializer(arrayType);
int idx = 0;
for (Statement stmt : node.getStatements()) {
if (stmt instanceof SwitchCase) {
SwitchCase caseStmt = (SwitchCase) stmt;
if (!caseStmt.isDefault()) {
arrayInit.addExpression(TreeUtil.remove(caseStmt.getExpression()));
caseStmt.setExpression(NumberLiteral.newIntLiteral(idx++, typeUtil));
}
}
}
TypeMirror intType = typeUtil.getInt();
FunctionElement indexOfFunc = new FunctionElement("JreIndexOfStr", intType, null).addParameters(type, arrayType, intType);
FunctionInvocation invocation = new FunctionInvocation(indexOfFunc, intType);
invocation.addArgument(TreeUtil.remove(expr)).addArgument(arrayInit).addArgument(NumberLiteral.newIntLiteral(idx, typeUtil));
node.setExpression(invocation);
}
Aggregations