use of com.google.devtools.j2objc.ast.MethodInvocation in project j2objc by google.
the class OuterReferenceResolverTest method testInheritedOuterMethod.
public void testInheritedOuterMethod() {
resolveSource("Test", "class Test { class A { void foo() {} } class B extends A { " + "class Inner { void test() { foo(); } } } }");
TypeDeclaration aNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(1);
TypeDeclaration bNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(2);
TypeDeclaration innerNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(3);
assertFalse(captureInfo.needsOuterReference(aNode.getTypeElement()));
assertFalse(captureInfo.needsOuterReference(bNode.getTypeElement()));
assertTrue(captureInfo.needsOuterReference(innerNode.getTypeElement()));
// B will need an outer reference to Test so it can initialize its
// superclass A.
Expression bSuperOuter = bNode.getSuperOuter();
assertTrue(bSuperOuter instanceof SimpleName);
assertEquals("outer$", ElementUtil.getName(TreeUtil.getVariableElement(bSuperOuter)));
// foo() call will need to get to B's scope to call the inherited method.
MethodInvocation fooCall = (MethodInvocation) nodesByType.get(Kind.METHOD_INVOCATION).get(0);
Expression expr = fooCall.getExpression();
assertTrue(expr instanceof SimpleName);
VariableElement fooReceiver = TreeUtil.getVariableElement(expr);
assertNotNull(fooReceiver);
assertEquals("Test.B", fooReceiver.asType().toString());
}
use of com.google.devtools.j2objc.ast.MethodInvocation in project j2objc by google.
the class Autoboxer method unbox.
/**
* Convert a wrapper class instance to a specified primitive equivalent.
*/
private void unbox(Expression expr, PrimitiveType primitiveType) {
TypeElement boxedClass = findBoxedSuperclass(expr.getTypeMirror());
if (primitiveType == null && boxedClass != null) {
primitiveType = typeUtil.unboxedType(boxedClass.asType());
}
if (primitiveType == null) {
return;
}
ExecutableElement valueMethod = ElementUtil.findMethod(boxedClass, TypeUtil.getName(primitiveType) + VALUE_METHOD);
assert valueMethod != null : "could not find value method for " + boxedClass;
MethodInvocation invocation = new MethodInvocation(new ExecutablePair(valueMethod), null);
expr.replaceWith(invocation);
invocation.setExpression(expr);
}
use of com.google.devtools.j2objc.ast.MethodInvocation in project j2objc by google.
the class ArrayRewriter method newSingleDimensionArrayInvocation.
private MethodInvocation newSingleDimensionArrayInvocation(ArrayType arrayType, Expression dimensionExpr, boolean retainedResult) {
TypeMirror componentType = arrayType.getComponentType();
TypeElement iosArrayElement = typeUtil.getIosArray(componentType);
boolean isPrimitive = componentType.getKind().isPrimitive();
String selector = (retainedResult ? "newArray" : "array") + "WithLength:" + (isPrimitive ? "" : "type:");
GeneratedExecutableElement methodElement = GeneratedExecutableElement.newMethodWithSelector(selector, iosArrayElement.asType(), iosArrayElement).addModifiers(Modifier.PUBLIC, Modifier.STATIC);
methodElement.addParameter(GeneratedVariableElement.newParameter("length", typeUtil.getInt(), methodElement));
if (!isPrimitive) {
methodElement.addParameter(GeneratedVariableElement.newParameter("type", TypeUtil.IOS_CLASS.asType(), methodElement));
}
MethodInvocation invocation = new MethodInvocation(new ExecutablePair(methodElement), arrayType, new SimpleName(iosArrayElement));
// Add the array length argument.
invocation.addArgument(dimensionExpr.copy());
// Add the type argument for object arrays.
if (!isPrimitive) {
invocation.addArgument(new TypeLiteral(componentType, typeUtil));
}
return invocation;
}
use of com.google.devtools.j2objc.ast.MethodInvocation in project j2objc by google.
the class Autoboxer method boxWithClass.
private void boxWithClass(Expression expr, TypeElement boxedClass) {
PrimitiveType primitiveType = typeUtil.unboxedType(boxedClass.asType());
assert primitiveType != null;
ExecutableElement wrapperMethod = ElementUtil.findMethod(boxedClass, VALUEOF_METHOD, TypeUtil.getQualifiedName(primitiveType));
assert wrapperMethod != null : "could not find valueOf method for " + boxedClass;
MethodInvocation invocation = new MethodInvocation(new ExecutablePair(wrapperMethod), new SimpleName(boxedClass));
expr.replaceWith(invocation);
invocation.addArgument(expr);
}
use of com.google.devtools.j2objc.ast.MethodInvocation in project j2objc by google.
the class TreeConverter method convertMethodInvocation.
private TreeNode convertMethodInvocation(JCTree.JCMethodInvocation node) {
JCTree.JCExpression method = node.getMethodSelect();
String methodName = getMemberName(method);
ExecutableType type = (ExecutableType) method.type;
Symbol.MethodSymbol sym = (Symbol.MethodSymbol) getMemberSymbol(method);
JCTree.JCExpression target = method.getKind() == Kind.MEMBER_SELECT ? ((JCTree.JCFieldAccess) method).selected : null;
if ("this".equals(methodName)) {
ConstructorInvocation newNode = new ConstructorInvocation().setExecutablePair(new ExecutablePair(sym)).setVarargsType(node.varargsElement);
for (JCTree.JCExpression arg : node.getArguments()) {
newNode.addArgument((Expression) convert(arg));
}
return newNode;
}
if ("super".equals(methodName)) {
SuperConstructorInvocation newNode = new SuperConstructorInvocation().setExecutablePair(new ExecutablePair(sym)).setVarargsType(node.varargsElement);
if (target != null) {
newNode.setExpression((Expression) convert(target));
}
for (JCTree.JCExpression arg : node.getArguments()) {
newNode.addArgument((Expression) convert(arg));
}
return newNode;
}
if (target != null && "super".equals(getMemberName(target))) {
SuperMethodInvocation newNode = new SuperMethodInvocation().setExecutablePair(new ExecutablePair(sym, type)).setVarargsType(node.varargsElement).setName(convertSimpleName(sym, type, getPosition(node)));
if (target.getKind() == Kind.MEMBER_SELECT) {
// foo.bar.MyClass.super.print(...):
// target: foo.bar.MyClass.super
// target.selected: foo.bar.MyClass
newNode.setQualifier((Name) convert(((JCTree.JCFieldAccess) target).selected));
}
for (JCTree.JCExpression arg : node.getArguments()) {
newNode.addArgument((Expression) convert(arg));
}
return newNode;
}
MethodInvocation newNode = new MethodInvocation();
newNode.setName(convertSimpleName(sym, type, getPosition(method)));
if (target != null) {
newNode.setExpression((Expression) convert(target));
}
for (JCTree.JCExpression arg : node.getArguments()) {
newNode.addArgument((Expression) convert(arg));
}
return newNode.setTypeMirror(node.type).setExecutablePair(new ExecutablePair(sym, type)).setVarargsType(node.varargsElement);
}
Aggregations