Search in sources :

Example 26 with CompilationUnit

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

the class SignatureGeneratorTest method testGenericInterface.

public void testGenericInterface() throws IOException {
    CompilationUnit unit = translateType("A", "interface A<E> extends java.util.Collection<E> {}");
    SignatureGenerator signatureGenerator = unit.getEnv().signatureGenerator();
    List<AbstractTypeDeclaration> decls = unit.getTypes();
    assertEquals(1, decls.size());
    assertEquals("<E:Ljava/lang/Object;>Ljava/lang/Object;Ljava/util/Collection<TE;>;", signatureGenerator.createClassSignature(decls.get(0).getTypeElement()));
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 27 with CompilationUnit

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

the class InnerClassExtractorTest method translateClassBody.

protected List<AbstractTypeDeclaration> translateClassBody(String testSource) {
    String source = "public class Test { " + testSource + " }";
    CompilationUnit unit = translateType("Test", source);
    return unit.getTypes();
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit)

Example 28 with CompilationUnit

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

the class TreeConverter method convertCompilationUnit.

public static CompilationUnit convertCompilationUnit(TranslationEnvironment env, org.eclipse.jdt.core.dom.CompilationUnit jdtUnit, String sourceFilePath, String mainTypeName, String source) {
    CompilationUnit unit = new CompilationUnit(env, sourceFilePath, mainTypeName, source);
    if (jdtUnit.getPackage() == null) {
        unit.setPackage(new PackageDeclaration());
    } else {
        unit.setPackage((PackageDeclaration) TreeConverter.convert(jdtUnit.getPackage()));
    }
    for (Object comment : jdtUnit.getCommentList()) {
        // Comments are not normally parented in the JDT AST. Javadoc nodes are
        // normally parented by the BodyDeclaration they apply to, so here we only
        // keep the unparented comments to avoid duplicate comment nodes.
        ASTNode commentParent = ((ASTNode) comment).getParent();
        if (commentParent == null || commentParent.equals(jdtUnit)) {
            Comment newComment = (Comment) TreeConverter.convert(comment);
            // Since the comment is unparented, it's constructor is unable to get
            // the root CompilationUnit to determine the line number.
            newComment.setLineNumber(jdtUnit.getLineNumber(newComment.getStartPosition()));
            unit.addComment(newComment);
        }
    }
    for (Object type : jdtUnit.types()) {
        unit.addType((AbstractTypeDeclaration) TreeConverter.convert(type));
    }
    return unit;
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) LineComment(com.google.devtools.j2objc.ast.LineComment) BlockComment(com.google.devtools.j2objc.ast.BlockComment) Comment(com.google.devtools.j2objc.ast.Comment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) PackageDeclaration(com.google.devtools.j2objc.ast.PackageDeclaration)

Example 29 with CompilationUnit

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

the class FileProcessor method processInput.

private void processInput(ProcessingContext input) {
    try {
        InputFile file = input.getFile();
        if (isBatchable(file)) {
            batchInputs.add(input);
            if (batchInputs.size() == batchSize) {
                processBatch();
            }
            return;
        }
        logger.finest("parsing " + file);
        CompilationUnit compilationUnit = parser.parse(file);
        if (compilationUnit == null) {
            handleError(input);
            return;
        }
        processCompiledSource(input, compilationUnit);
    } catch (RuntimeException | Error e) {
        ErrorUtil.fatalError(e, input.getOriginalSourcePath());
    }
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) InputFile(com.google.devtools.j2objc.file.InputFile)

Example 30 with CompilationUnit

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

the class InputFilePreprocessor method processPackageInfoSource.

private void processPackageInfoSource(ProcessingContext input) throws IOException {
    InputFile file = input.getFile();
    String source = options.fileUtil().readFile(file);
    CompilationUnit compilationUnit = parser.parse(FileUtil.getMainTypeName(file), file.getUnitName(), source);
    if (compilationUnit != null) {
        extractPackagePrefix(file, compilationUnit);
    }
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) InputFile(com.google.devtools.j2objc.file.InputFile) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile)

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