Search in sources :

Example 46 with CompilationUnit

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

the class JavadocGenerator method getSourceIndent.

/**
   * Fetch the leading whitespace from the comment line. Since the JDT
   * strips leading and trailing whitespace from lines, the original
   * source is fetched and is walked backwards from the fragment's start
   * until the previous new line, then moved forward if there is a leading
   * "* ".
   */
private String getSourceIndent(TreeNode fragment) {
    int index = fragment.getStartPosition();
    if (index < 1) {
        return "";
    }
    TreeNode node = fragment.getParent();
    while (node != null && node.getKind() != TreeNode.Kind.COMPILATION_UNIT) {
        node = node.getParent();
    }
    if (node instanceof CompilationUnit) {
        String source = ((CompilationUnit) node).getSource();
        int i = index - 1;
        char c;
        while (i >= 0 && (c = source.charAt(i)) != '\n') {
            if (c != '*' && !Character.isWhitespace(c)) {
                // Pre tag embedded in other text, so no indent.
                return "";
            }
            --i;
        }
        String lineStart = source.substring(i + 1, index);
        i = lineStart.indexOf('*');
        if (i == -1) {
            return lineStart;
        }
        // Indent could end with '*' instead of "* ", if there's no text after it.
        return (++i + 1) < lineStart.length() ? lineStart.substring(i + 1) : lineStart.substring(i);
    }
    return "";
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TreeNode(com.google.devtools.j2objc.ast.TreeNode)

Example 47 with CompilationUnit

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

the class SwitchRewriterTest method testVariableDeclarationsInSwitchStatement2.

public void testVariableDeclarationsInSwitchStatement2() throws IOException {
    CompilationUnit unit = translateType("A", "public class A { public void doSomething(int i) { switch (i) { " + "case 1: int j = i * 2; log(j); break; " + "case 2: log(i); break; " + "case 3: log(i); int k = i, l = 42; break; }}" + "private void log(int i) {}}");
    TypeDeclaration testType = (TypeDeclaration) unit.getTypes().get(0);
    // First MethodDeclaration is the implicit default constructor.
    MethodDeclaration method = TreeUtil.getMethodDeclarationsList(testType).get(1);
    List<Statement> stmts = method.getBody().getStatements();
    assertEquals(1, stmts.size());
    Block block = (Block) stmts.get(0);
    stmts = block.getStatements();
    if (options.isJDT()) {
        assertEquals(3, stmts.size());
        assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
        assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
        assertTrue(stmts.get(2) instanceof SwitchStatement);
    } else {
        assertEquals(4, stmts.size());
        assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
        assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
        assertTrue(stmts.get(2) instanceof VariableDeclarationStatement);
        assertTrue(stmts.get(3) instanceof SwitchStatement);
    }
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) Block(com.google.devtools.j2objc.ast.Block) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Example 48 with CompilationUnit

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

the class CompoundTypeTest method testIsCompound.

// Test TypeUtil.isIntersection(TypeMirror).
public void testIsCompound() throws Exception {
    String source = "interface Test<T> extends java.util.Comparator<T> {" + "  default Test<T> thenTesting(Test<? super T> other) { " + "    return (Test<T> & java.io.Serializable) (c1, c2) -> { " + "    int res = compare(c1, c2); " + "    return (res != 0) ? res : other.compare(c1, c2); }; }}";
    CompilationUnit unit = compileType("Test", source);
    AbstractTypeDeclaration decl = unit.getTypes().get(0);
    int methodsFound = 0;
    for (BodyDeclaration body : decl.getBodyDeclarations()) {
        if (body instanceof MethodDeclaration) {
            MethodDeclaration method = (MethodDeclaration) body;
            if (ElementUtil.getName(method.getExecutableElement()).equals("thenTesting")) {
                // Verify a normal type isn't marked as compound.
                TypeMirror returnType = method.getReturnTypeMirror();
                assertFalse(TypeUtil.isIntersection(returnType));
                // The method's return type isn't compound, but the cast expression in
                // its return statement is.
                ReturnStatement stmt = (ReturnStatement) method.getBody().getStatements().get(0);
                assertTrue(TypeUtil.isIntersection(stmt.getExpression().getTypeMirror()));
                methodsFound++;
            }
        }
    }
    assertEquals(1, methodsFound);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TypeMirror(javax.lang.model.type.TypeMirror) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 49 with CompilationUnit

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

the class CompoundTypeTest method testCompoundTypeFullName.

// Test NameTable.getObjCType(TypeMirror).
public void testCompoundTypeFullName() throws IOException {
    String source = "package foo.bar; interface Test<T> extends java.util.Comparator<T> {" + "  default Test<T> thenTesting(Test<? super T> other) { " + "    return (Test<T> & java.io.Serializable) (c1, c2) -> { " + "    int res = compare(c1, c2); " + "    return (res != 0) ? res : other.compare(c1, c2); }; }}";
    CompilationUnit unit = compileType("Test", source);
    AbstractTypeDeclaration decl = unit.getTypes().get(0);
    for (BodyDeclaration body : decl.getBodyDeclarations()) {
        if (body instanceof MethodDeclaration) {
            MethodDeclaration method = (MethodDeclaration) body;
            if (ElementUtil.getName(method.getExecutableElement()).equals("thenTesting")) {
                // The method's return type isn't compound, but the cast expression in
                // its return statement is.
                ReturnStatement stmt = (ReturnStatement) method.getBody().getStatements().get(0);
                TypeMirror mirror = stmt.getExpression().getTypeMirror();
                String typeName = unit.getEnv().nameTable().getObjCType(mirror);
                assertEquals("id<FooBarTest, JavaIoSerializable>", typeName);
                return;
            }
        }
    }
    fail("thenTesting method not found");
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TypeMirror(javax.lang.model.type.TypeMirror) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 50 with CompilationUnit

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

the class ElementUtilTest method testGetAnnotation.

public void testGetAnnotation() throws IOException {
    CompilationUnit unit = translateType("Example", "@com.google.j2objc.annotations.ObjectiveCName(\"E\") class Example {}");
    AbstractTypeDeclaration decl = unit.getTypes().get(0);
    TypeElement element = decl.getTypeElement();
    AnnotationMirror annotation = ElementUtil.getAnnotation(element, ObjectiveCName.class);
    assertEquals("com.google.j2objc.annotations.ObjectiveCName", annotation.getAnnotationType().toString());
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Aggregations

CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)71 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)24 HashSet (java.util.HashSet)24 ReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.ReferenceNode)23 HashMap (java.util.HashMap)23 Set (java.util.Set)23 CodeReferenceMap (com.google.devtools.j2objc.util.CodeReferenceMap)9 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)7 MethodReferenceNode (com.google.devtools.treeshaker.ElementReferenceMapper.MethodReferenceNode)7 TypeElement (javax.lang.model.element.TypeElement)5 TreeVisitor (com.google.devtools.j2objc.ast.TreeVisitor)4 NameTable (com.google.devtools.j2objc.util.NameTable)4 TreeNode (com.google.devtools.j2objc.ast.TreeNode)3 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)3 RegularInputFile (com.google.devtools.j2objc.file.RegularInputFile)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)2 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)2