Search in sources :

Example 1 with MethodInvocation

use of com.google.devtools.j2objc.ast.MethodInvocation in project j2objc by google.

the class Functionizer method determineFunctionizableMethods.

/**
   * Determines the set of methods to functionize. In addition to a method being
   * final we must also find an invocation for that method. Static methods, though,
   * are always functionized since there are no dynamic dispatch issues.
   */
private Set<ExecutableElement> determineFunctionizableMethods(final CompilationUnit unit) {
    final Set<ExecutableElement> functionizableDeclarations = Sets.newHashSet();
    final Set<ExecutableElement> invocations = Sets.newHashSet();
    unit.accept(new TreeVisitor() {

        @Override
        public void endVisit(MethodDeclaration node) {
            if (canFunctionize(node)) {
                functionizableDeclarations.add(node.getExecutableElement());
            }
        }

        @Override
        public void endVisit(MethodInvocation node) {
            invocations.add(node.getExecutableElement());
        }
    });
    return Sets.intersection(functionizableDeclarations, invocations);
}
Also used : TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) UnitTreeVisitor(com.google.devtools.j2objc.ast.UnitTreeVisitor) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation)

Example 2 with MethodInvocation

use of com.google.devtools.j2objc.ast.MethodInvocation in project j2objc by google.

the class JavaToIOSMethodTranslator method addCopyWithZoneMethod.

private void addCopyWithZoneMethod(TypeDeclaration node) {
    // Create copyWithZone: method.
    GeneratedExecutableElement copyElement = GeneratedExecutableElement.newMethodWithSelector("copyWithZone:", TypeUtil.ID_TYPE, node.getTypeElement());
    MethodDeclaration copyDecl = new MethodDeclaration(copyElement);
    copyDecl.setHasDeclaration(false);
    // Add NSZone *zone parameter.
    VariableElement zoneParam = GeneratedVariableElement.newParameter("zone", NSZONE_TYPE, copyElement);
    copyElement.addParameter(zoneParam);
    copyDecl.addParameter(new SingleVariableDeclaration(zoneParam));
    Block block = new Block();
    copyDecl.setBody(block);
    ExecutableElement cloneElement = ElementUtil.findMethod(typeUtil.getJavaObject(), "clone");
    MethodInvocation invocation = new MethodInvocation(new ExecutablePair(cloneElement), null);
    if (options.useReferenceCounting()) {
        invocation = new MethodInvocation(new ExecutablePair(RETAIN_METHOD), invocation);
    }
    block.addStatement(new ReturnStatement(invocation));
    node.addBodyDeclaration(copyDecl);
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) Block(com.google.devtools.j2objc.ast.Block) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 3 with MethodInvocation

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());
}
Also used : PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) VariableElement(javax.lang.model.element.VariableElement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Example 4 with MethodInvocation

use of com.google.devtools.j2objc.ast.MethodInvocation in project j2objc by google.

the class LogSiteInjector method injectEnclosingClass.

private MethodInvocation injectEnclosingClass(MethodInvocation node, TypeElement cls) {
    ExecutableElement injectedMethod = ElementUtil.findMethod(cls, "forInjectedClassName", "java.lang.String");
    MethodInvocation injectedInvocation = new MethodInvocation(new ExecutablePair(injectedMethod), node.getExpression().copy());
    injectedInvocation.addArgument(enclosingClassLiteral(node));
    return injectedInvocation;
}
Also used : ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) ExecutableElement(javax.lang.model.element.ExecutableElement) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Example 5 with MethodInvocation

use of com.google.devtools.j2objc.ast.MethodInvocation in project j2objc by google.

the class LogSiteInjector method endVisit.

@Override
public void endVisit(MethodInvocation node) {
    if (node.getExpression() == null) {
        // Local and statically imported methods are never injected.
        return;
    }
    ExecutableElement method = node.getExecutableElement();
    String methodName = ElementUtil.getName(method);
    TypeElement cls = ElementUtil.getDeclaringClass(method);
    if (methodName.equals("forEnclosingClass") && isLoggingSubtype(cls, googleLoggerClass)) {
        node.replaceWith(injectEnclosingClass(node, cls));
        return;
    }
    if (methodName.equals(LOG_METHOD) && isLoggingSubtype(cls, javaUtilLoggingLoggerClass)) {
        node.replaceWith(injectLogMethod(node));
        return;
    }
    if ((methodName.equals(LOG_METHOD) || methodName.equals(LOG_VARARGS_METHOD)) && isLoggingSubtype(cls, loggingApiClass)) {
        Expression methodExpr = node.getExpression();
        // Check that injectedLogSite() wasn't already injected or in original source.
        if (methodExpr.getKind() == TreeNode.Kind.METHOD_INVOCATION && !ElementUtil.getName(((MethodInvocation) methodExpr).getExecutableElement()).equals(WITH_INJECTED_LOG_SITE_METHOD)) {
            node.replaceWith(injectLogSite(node));
        }
        return;
    }
    if (CONVENIENCE_METHODS.contains(methodName) && isLoggingSubtype(cls, javaUtilLoggingLoggerClass)) {
        node.replaceWith(injectConvenienceMethod(methodName, node));
        return;
    }
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Aggregations

MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)27 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)23 ExecutableElement (javax.lang.model.element.ExecutableElement)16 Expression (com.google.devtools.j2objc.ast.Expression)14 TypeElement (javax.lang.model.element.TypeElement)12 SimpleName (com.google.devtools.j2objc.ast.SimpleName)11 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)11 TypeMirror (javax.lang.model.type.TypeMirror)11 SuperMethodInvocation (com.google.devtools.j2objc.ast.SuperMethodInvocation)10 Block (com.google.devtools.j2objc.ast.Block)6 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)6 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)6 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)5 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)5 DeclaredType (javax.lang.model.type.DeclaredType)5 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)4 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)4 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)4 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)4 Statement (com.google.devtools.j2objc.ast.Statement)4