Search in sources :

Example 1 with TreeVisitor

use of com.google.devtools.j2objc.ast.TreeVisitor 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 TreeVisitor

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

the class NameTableTest method testTypeVariableWithTypeVariableBounds.

public void testTypeVariableWithTypeVariableBounds() {
    String source = "class A<T> { <E extends T> void foo(E e) {} }";
    CompilationUnit unit = translateType("A", source);
    NameTable nameTable = unit.getEnv().nameTable();
    final ExecutableElement[] methodElement = new ExecutableElement[1];
    unit.accept(new TreeVisitor() {

        @Override
        public void endVisit(MethodDeclaration node) {
            ExecutableElement element = node.getExecutableElement();
            if (ElementUtil.getName(element).equals("foo")) {
                methodElement[0] = element;
            }
        }
    });
    assertNotNull(methodElement[0]);
    TypeMirror paramType = methodElement[0].getParameters().get(0).asType();
    assertEquals("id", nameTable.getObjCType(paramType));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) TypeMirror(javax.lang.model.type.TypeMirror) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 3 with TreeVisitor

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

the class GenerationTest method translateStatements.

/**
 * Translate a string of Java statement(s) into a list of
 * JDT DOM Statements.  Although JDT has support for statement
 * parsing, it doesn't resolve them.  The statements are therefore
 * wrapped in a type declaration so they having bindings.
 */
protected List<Statement> translateStatements(String stmts) {
    // Wrap statements in test class, so type resolution works.
    String source = "public class Test { void test() { " + stmts + "}}";
    CompilationUnit unit = translateType("Test", source);
    final List<Statement> statements = Lists.newArrayList();
    unit.accept(new TreeVisitor() {

        @Override
        public boolean visit(MethodDeclaration node) {
            if (ElementUtil.getName(node.getExecutableElement()).equals("test")) {
                statements.addAll(node.getBody().getStatements());
            }
            return false;
        }
    });
    return statements;
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) Statement(com.google.devtools.j2objc.ast.Statement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration)

Example 4 with TreeVisitor

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

the class StatementGenerator method needsParenthesesForMacro.

private boolean needsParenthesesForMacro(Expression expr) {
    boolean[] hasComma = { false };
    expr.accept(new TreeVisitor() {

        @Override
        public boolean visit(ArrayInitializer node) {
            hasComma[0] = true;
            return false;
        }

        @Override
        public boolean visit(CommaExpression node) {
            // Adds parentheses around children.
            return false;
        }

        @Override
        public boolean visit(FunctionInvocation node) {
            // Adds parentheses around children.
            return false;
        }

        @Override
        public boolean visit(StringLiteral node) {
            if (!UnicodeUtils.hasValidCppCharacters(node.getLiteralValue())) {
                // LiteralGenerator will emit the string using [NSString stringWithCharacters:].
                hasComma[0] = true;
                return false;
            }
            return true;
        }
    });
    return hasComma[0];
}
Also used : TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) UnitTreeVisitor(com.google.devtools.j2objc.ast.UnitTreeVisitor) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) CStringLiteral(com.google.devtools.j2objc.ast.CStringLiteral) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer)

Example 5 with TreeVisitor

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

the class Functionizer method hasSuperMethodInvocation.

private static boolean hasSuperMethodInvocation(MethodDeclaration node) {
    final boolean[] result = new boolean[1];
    result[0] = false;
    node.accept(new TreeVisitor() {

        @Override
        public void endVisit(SuperMethodInvocation node) {
            result[0] = true;
        }
    });
    return result[0];
}
Also used : TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) UnitTreeVisitor(com.google.devtools.j2objc.ast.UnitTreeVisitor) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation)

Aggregations

TreeVisitor (com.google.devtools.j2objc.ast.TreeVisitor)9 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)5 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)4 UnitTreeVisitor (com.google.devtools.j2objc.ast.UnitTreeVisitor)3 BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)2 ContinueStatement (com.google.devtools.j2objc.ast.ContinueStatement)2 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)2 SimpleName (com.google.devtools.j2objc.ast.SimpleName)2 Statement (com.google.devtools.j2objc.ast.Statement)2 SuperMethodInvocation (com.google.devtools.j2objc.ast.SuperMethodInvocation)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)1 Block (com.google.devtools.j2objc.ast.Block)1 CStringLiteral (com.google.devtools.j2objc.ast.CStringLiteral)1 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)1 DoStatement (com.google.devtools.j2objc.ast.DoStatement)1 EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)1 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)1 ForStatement (com.google.devtools.j2objc.ast.ForStatement)1 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)1