Search in sources :

Example 1 with PackageDeclaration

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

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

the class FileUtil method getQualifiedMainTypeName.

public static String getQualifiedMainTypeName(InputFile file, CompilationUnit unit) {
    String qualifiedName = getMainTypeName(file);
    PackageDeclaration packageDecl = unit.getPackage();
    if (packageDecl != null) {
        String packageName = packageDecl.getName().getFullyQualifiedName();
        qualifiedName = packageName + "." + qualifiedName;
    }
    return qualifiedName;
}
Also used : PackageDeclaration(com.google.devtools.j2objc.ast.PackageDeclaration)

Example 3 with PackageDeclaration

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

the class TreeConverter method convertPackage.

private PackageDeclaration convertPackage(PackageElement pkg, Trees trees) {
    JCTree node = (JCTree) trees.getTree(pkg);
    PackageDeclaration newNode = new PackageDeclaration().setPackageElement(pkg);
    for (JCTree.JCAnnotation pkgAnnotation : unit.getPackageAnnotations()) {
        newNode.addAnnotation((Annotation) convert(pkgAnnotation));
    }
    if (unit.sourcefile.toUri().getPath().endsWith("package-info.java")) {
        newNode.setJavadoc((Javadoc) getAssociatedJavaDoc(node, pkg));
    }
    return (PackageDeclaration) newNode.setName(convertName((PackageSymbol) pkg, getPosition(node))).setPosition(SourcePosition.NO_POSITION);
}
Also used : PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) JCTree(com.sun.tools.javac.tree.JCTree) PackageDeclaration(com.google.devtools.j2objc.ast.PackageDeclaration)

Example 4 with PackageDeclaration

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

the class TreeConverter method convertPackage.

private PackageDeclaration convertPackage(TreePath parent) {
    ExpressionTree pkgName = unit.getPackageName();
    PackageElement pkg = pkgName != null ? env.elementUtilities().getPackageElement(pkgName.toString()) : env.defaultPackage();
    PackageDeclaration newNode = null;
    if (pkg == null) {
        // Synthetic package, create from name.
        pkg = new GeneratedPackageElement(pkgName != null ? pkgName.toString() : "");
        newNode = new PackageDeclaration().setPackageElement(pkg);
        newNode.setName(new SimpleName(pkg, null));
    } else {
        Tree node = trees.getTree(pkg);
        newNode = new PackageDeclaration().setPackageElement(pkg);
        for (AnnotationTree pkgAnnotation : unit.getPackageAnnotations()) {
            newNode.addAnnotation((Annotation) convert(pkgAnnotation, parent));
        }
        if (unit.getSourceFile().toUri().getPath().endsWith("package-info.java")) {
            if (node == null) {
                // Java 8 javac bug, fixed in Java 9. Doc-comments in package-info.java
                // sources are keyed to their compilation unit, not their package node.
                node = unit;
            }
            newNode.setJavadoc((Javadoc) getAssociatedJavaDoc(node, getTreePath(parent, node)));
        }
        newNode.setName(newUnit.getEnv().elementUtil().getPackageName(pkg));
    }
    newNode.setPosition(SourcePosition.NO_POSITION);
    return newNode;
}
Also used : GeneratedPackageElement(com.google.devtools.j2objc.types.GeneratedPackageElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ForLoopTree(com.sun.source.tree.ForLoopTree) InstanceOfTree(com.sun.source.tree.InstanceOfTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ThrowTree(com.sun.source.tree.ThrowTree) BlockTree(com.sun.source.tree.BlockTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) ReturnTree(com.sun.source.tree.ReturnTree) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) LabeledStatementTree(com.sun.source.tree.LabeledStatementTree) UnaryTree(com.sun.source.tree.UnaryTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) BreakTree(com.sun.source.tree.BreakTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) NewArrayTree(com.sun.source.tree.NewArrayTree) ContinueTree(com.sun.source.tree.ContinueTree) CaseTree(com.sun.source.tree.CaseTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) SwitchTree(com.sun.source.tree.SwitchTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) AssertTree(com.sun.source.tree.AssertTree) StatementTree(com.sun.source.tree.StatementTree) ModifiersTree(com.sun.source.tree.ModifiersTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) ClassTree(com.sun.source.tree.ClassTree) IfTree(com.sun.source.tree.IfTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) JCTree(com.sun.tools.javac.tree.JCTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree) TryTree(com.sun.source.tree.TryTree) AnnotationTree(com.sun.source.tree.AnnotationTree) PackageElement(javax.lang.model.element.PackageElement) GeneratedPackageElement(com.google.devtools.j2objc.types.GeneratedPackageElement) PackageDeclaration(com.google.devtools.j2objc.ast.PackageDeclaration)

Example 5 with PackageDeclaration

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

the class PackageInfoRewriter method run.

private void run() {
    PackageDeclaration pkg = unit.getPackage();
    List<Annotation> annotations = pkg.getAnnotations();
    List<Annotation> runtimeAnnotations = TreeUtil.getRuntimeAnnotationsList(annotations);
    String prefix = getPackagePrefix(pkg);
    boolean needsReflection = translationUtil.needsReflection(pkg);
    // Remove compile time annotations.
    annotations.retainAll(runtimeAnnotations);
    if ((annotations.isEmpty() && prefix == null) || !needsReflection) {
        return;
    }
    TypeElement typeElement = GeneratedTypeElement.newPackageInfoClass(pkg.getPackageElement(), typeUtil);
    TypeDeclaration typeDecl = new TypeDeclaration(typeElement);
    TreeUtil.moveList(pkg.getAnnotations(), typeDecl.getAnnotations());
    if (prefix != null) {
        typeDecl.addBodyDeclaration(createPrefixMethod(prefix, typeElement));
    }
    unit.addType(0, typeDecl);
}
Also used : GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration) PackageDeclaration(com.google.devtools.j2objc.ast.PackageDeclaration) Annotation(com.google.devtools.j2objc.ast.Annotation)

Aggregations

PackageDeclaration (com.google.devtools.j2objc.ast.PackageDeclaration)5 JCTree (com.sun.tools.javac.tree.JCTree)2 Annotation (com.google.devtools.j2objc.ast.Annotation)1 BlockComment (com.google.devtools.j2objc.ast.BlockComment)1 Comment (com.google.devtools.j2objc.ast.Comment)1 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)1 LineComment (com.google.devtools.j2objc.ast.LineComment)1 SimpleName (com.google.devtools.j2objc.ast.SimpleName)1 TypeDeclaration (com.google.devtools.j2objc.ast.TypeDeclaration)1 GeneratedPackageElement (com.google.devtools.j2objc.types.GeneratedPackageElement)1 GeneratedTypeElement (com.google.devtools.j2objc.types.GeneratedTypeElement)1 AnnotatedTypeTree (com.sun.source.tree.AnnotatedTypeTree)1 AnnotationTree (com.sun.source.tree.AnnotationTree)1 ArrayAccessTree (com.sun.source.tree.ArrayAccessTree)1 ArrayTypeTree (com.sun.source.tree.ArrayTypeTree)1 AssertTree (com.sun.source.tree.AssertTree)1 AssignmentTree (com.sun.source.tree.AssignmentTree)1 BinaryTree (com.sun.source.tree.BinaryTree)1 BlockTree (com.sun.source.tree.BlockTree)1 BreakTree (com.sun.source.tree.BreakTree)1