use of com.google.devtools.j2objc.ast.TypeDeclaration in project j2objc by google.
the class Functionizer method endVisit.
@Override
public void endVisit(SuperConstructorInvocation node) {
ExecutableElement element = node.getExecutableElement();
AbstractTypeDeclaration typeDecl = TreeUtil.getEnclosingType(node);
TypeElement type = typeDecl.getTypeElement();
FunctionElement funcElement = newFunctionElement(element);
FunctionInvocation invocation = new FunctionInvocation(funcElement, typeUtil.getVoid());
List<Expression> args = invocation.getArguments();
args.add(new ThisExpression(ElementUtil.getDeclaringClass(element).asType()));
if (typeDecl instanceof TypeDeclaration) {
TypeDeclaration typeDeclaration = (TypeDeclaration) typeDecl;
if (captureInfo.needsOuterParam(ElementUtil.getSuperclass(type))) {
Expression outerArg = TreeUtil.remove(node.getExpression());
args.add(outerArg != null ? outerArg : typeDeclaration.getSuperOuter().copy());
}
TreeUtil.moveList(typeDeclaration.getSuperCaptureArgs(), args);
}
TreeUtil.moveList(node.getArguments(), args);
if (ElementUtil.isEnum(type)) {
for (VariableElement param : captureInfo.getImplicitEnumParams()) {
args.add(new SimpleName(param));
}
}
node.replaceWith(new ExpressionStatement(invocation));
assert funcElement.getParameterTypes().size() == args.size();
}
use of com.google.devtools.j2objc.ast.TypeDeclaration 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.TypeDeclaration 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.TypeDeclaration in project j2objc by google.
the class OuterReferenceResolverTest method testCapturedWeakLocalVariable.
public void testCapturedWeakLocalVariable() {
resolveSource("Test", "import com.google.j2objc.annotations.Weak;" + "class Test { void test(@Weak final int i) { Runnable r = new Runnable() { " + "public void run() { int i2 = i + 1; } }; } }");
TypeDeclaration runnableNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(1);
assertTrue(ElementUtil.isAnonymous(runnableNode.getTypeElement()));
List<VariableElement> innerFields = Lists.newArrayList(captureInfo.getCaptureFields(runnableNode.getTypeElement()));
assertEquals(1, innerFields.size());
assertTrue(ElementUtil.isWeakReference(innerFields.get(0)));
}
use of com.google.devtools.j2objc.ast.TypeDeclaration in project j2objc by google.
the class TreeConverter method convertClassDeclaration.
private TreeNode convertClassDeclaration(JCTree.JCClassDecl node) {
// to support our different declaration nodes.
if (node.sym.getKind() == ElementKind.ANNOTATION_TYPE) {
throw new AssertionError("Annotation type declaration tree conversion not implemented");
}
TypeDeclaration newNode = (TypeDeclaration) convertAbstractTypeDeclaration(node, new TypeDeclaration());
newNode.setInterface(node.getKind() == Kind.INTERFACE || node.getKind() == Kind.ANNOTATION_TYPE);
if (node.sym.isAnonymous()) {
newUnit.getEnv().elementUtil().mapElementType(node.sym, node.type);
}
return newNode;
}
Aggregations