use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class OuterReferenceResolverTest method testAnonymousClassCreatesLocalClassWithCaptures.
public void testAnonymousClassCreatesLocalClassWithCaptures() {
resolveSource("Test", "class Test { Runnable test(final Object o) { " + "class Local { public void foo() { o.toString(); } } " + "return new Runnable() { public void run() { new Local(); } }; } }");
TypeDeclaration runnableNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(2);
assertTrue(ElementUtil.isAnonymous(runnableNode.getTypeElement()));
List<VariableElement> innerFields = Lists.newArrayList(captureInfo.getCaptureFields(runnableNode.getTypeElement()));
assertEquals(1, innerFields.size());
assertEquals("val$o", ElementUtil.getName(innerFields.get(0)));
ClassInstanceCreation creationNode = (ClassInstanceCreation) nodesByType.get(Kind.CLASS_INSTANCE_CREATION).get(1);
List<Expression> captureArgs = creationNode.getCaptureArgs();
assertEquals(1, captureArgs.size());
Expression captureArg = captureArgs.get(0);
assertTrue(captureArg instanceof SimpleName);
VariableElement captureVar = TreeUtil.getVariableElement(captureArg);
assertNotNull(captureVar);
assertEquals("val$o", ElementUtil.getName(captureVar));
}
use of com.google.devtools.j2objc.ast.Expression 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.Expression in project j2objc by google.
the class OuterReferenceResolverTest method testNestedLocalClassesWithNestedCreations.
public void testNestedLocalClassesWithNestedCreations() {
// This test is particularly tricky for OuterReferenceResolver because A captures variable i,
// but that is not known until after A's creation. A's creation occurs within B, which requires
// B to have an outer field in order to access A's capturing field for i. B's creation therefore
// requires the outer field to be passed as an outer argument.
// Because of the cascading effects of the statements in this test and the order in which they
// occur, we would need to do three passes over the code to resolve B's creation successfuly.
resolveSource("Test", "class Test { void test(int i) { class A { " + "void foo() { class B { void bar() { new B(); new A(); } } } " + "int other() { return i; } } } }");
ClassInstanceCreation bCreate = (ClassInstanceCreation) nodesByType.get(Kind.CLASS_INSTANCE_CREATION).get(0);
Expression outerArg = bCreate.getExpression();
assertTrue(outerArg instanceof SimpleName);
VariableElement var = TreeUtil.getVariableElement(outerArg);
assertNotNull(var);
assertEquals("this$0", ElementUtil.getName(var));
}
use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class TreeConverter method convertExpressionMethodReference.
private static TreeNode convertExpressionMethodReference(org.eclipse.jdt.core.dom.ExpressionMethodReference node) {
IMethodBinding methodBinding = node.resolveMethodBinding();
ExpressionMethodReference newNode = new ExpressionMethodReference();
Expression expression = (Expression) convert(node.getExpression());
boolean consumesFirstParam = !BindingUtil.isStatic(methodBinding) && expression instanceof Name && !ElementUtil.isVariable(((Name) expression).getElement());
convertMethodReference(node, newNode, methodBinding, consumesFirstParam);
return newNode.setName((SimpleName) TreeConverter.convert(node.getName())).setExpression(expression);
}
use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.
the class Autoboxer method endVisit.
@Override
public void endVisit(ConditionalExpression node) {
Expression expr = node.getExpression();
if (!expr.getTypeMirror().getKind().isPrimitive()) {
unbox(expr);
}
boolean nodeIsPrimitive = node.getTypeMirror().getKind().isPrimitive();
Expression thenExpr = node.getThenExpression();
boolean thenIsPrimitive = thenExpr.getTypeMirror().getKind().isPrimitive();
Expression elseExpr = node.getElseExpression();
boolean elseIsPrimitive = elseExpr.getTypeMirror().getKind().isPrimitive();
if (thenIsPrimitive && !nodeIsPrimitive) {
box(thenExpr);
} else if (!thenIsPrimitive && nodeIsPrimitive) {
unbox(thenExpr);
}
if (elseIsPrimitive && !nodeIsPrimitive) {
box(elseExpr);
} else if (!elseIsPrimitive && nodeIsPrimitive) {
unbox(elseExpr);
}
}
Aggregations