Search in sources :

Example 1 with GeneratedTypeElement

use of com.google.devtools.j2objc.types.GeneratedTypeElement in project j2objc by google.

the class BindingConverter method unwrapGeneratedElement.

private static IBinding unwrapGeneratedElement(GeneratedElement element) {
    IBinding binding = generatedBindingCache.get(element);
    if (binding != null) {
        return binding;
    }
    if (element instanceof GeneratedVariableElement) {
        binding = new GeneratedVariableBinding((GeneratedVariableElement) element);
        generatedBindingCache.put(element, binding);
        return binding;
    }
    if (element instanceof GeneratedExecutableElement) {
        binding = new GeneratedMethodBinding((GeneratedExecutableElement) element);
        generatedBindingCache.put(element, binding);
        return binding;
    }
    if (element instanceof GeneratedTypeElement) {
        throw new AssertionError("not supported");
    }
    if (element instanceof GeneratedPackageElement) {
        binding = new GeneratedPackageBinding(((GeneratedPackageElement) element).getName());
        generatedBindingCache.put(element, binding);
        return binding;
    }
    throw new AssertionError("unknown generated element kind");
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) GeneratedPackageElement(com.google.devtools.j2objc.types.GeneratedPackageElement) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) IBinding(org.eclipse.jdt.core.dom.IBinding)

Example 2 with GeneratedTypeElement

use of com.google.devtools.j2objc.types.GeneratedTypeElement in project j2objc by google.

the class TreeConverter method convertClassDeclaration.

private TreeNode convertClassDeclaration(ClassTree node, TreePath parent) {
    TreePath path = getTreePath(parent, node);
    TypeElement element = (TypeElement) getElement(path);
    // to support our different declaration nodes.
    if (element.getKind() == ElementKind.ANNOTATION_TYPE) {
        throw new AssertionError("Annotation type declaration tree conversion not implemented");
    }
    TypeDeclaration newNode = convertClassDeclarationHelper(node, parent);
    newNode.setInterface(node.getKind() == Kind.INTERFACE || node.getKind() == Kind.ANNOTATION_TYPE);
    if (ElementUtil.isAnonymous(element)) {
        TypeMirror classType = getTypeMirror(path);
        if (!classType.getAnnotationMirrors().isEmpty()) {
            newUnit.getEnv().elementUtil().mapElementType(element, classType);
        } else {
            // With javac 12 and above, annotations are no longer part of the supertype,
            // so check to see if a mutated type that has them is necessary.
            GeneratedTypeElement newElement = GeneratedTypeElement.mutableCopy(element);
            boolean annotationFound = copyAnnotations(node.getExtendsClause(), path, newNode, newElement);
            for (Tree clause : node.getImplementsClause()) {
                annotationFound |= copyAnnotations(clause, path, newNode, newElement);
            }
            if (annotationFound) {
                newUnit.getEnv().elementUtil().mapElementType(element, newElement.asType());
            }
        }
    }
    return newNode;
}
Also used : GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TreePath(com.sun.source.util.TreePath) TypeMirror(javax.lang.model.type.TypeMirror) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) 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) AnnotationTypeDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeDeclaration) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 3 with GeneratedTypeElement

use of com.google.devtools.j2objc.types.GeneratedTypeElement in project j2objc by google.

the class DeadCodeEliminator method removeDeadClasses.

/**
 * Remove empty classes marked as dead. This needs to be done after translation
 * to avoid inner class references in the AST returned by DeadCodeEliminator.
 */
public static void removeDeadClasses(CompilationUnit unit, CodeReferenceMap deadCodeMap) {
    ElementUtil elementUtil = unit.getEnv().elementUtil();
    Iterator<AbstractTypeDeclaration> iter = unit.getTypes().iterator();
    while (iter.hasNext()) {
        AbstractTypeDeclaration type = iter.next();
        TypeElement typeElement = type.getTypeElement();
        if (!ElementUtil.isGeneratedAnnotation(typeElement)) {
            if (deadCodeMap.containsClass(typeElement, elementUtil)) {
                type.setDeadClass(true);
            } else {
                // Keep class, remove dead interfaces.
                if (typeElement.getInterfaces().size() > 0) {
                    GeneratedTypeElement replacement = GeneratedTypeElement.mutableCopy(typeElement);
                    for (TypeElement intrface : ElementUtil.getInterfaces(typeElement)) {
                        if (!deadCodeMap.containsClass(intrface, elementUtil)) {
                            replacement.addInterface(intrface.asType());
                        }
                    }
                    if (typeElement.getInterfaces().size() > replacement.getInterfaces().size()) {
                        type.setTypeElement(replacement);
                    }
                }
            }
        }
    }
}
Also used : GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) ElementUtil(com.google.devtools.j2objc.util.ElementUtil) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Aggregations

GeneratedTypeElement (com.google.devtools.j2objc.types.GeneratedTypeElement)3 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)2 TypeElement (javax.lang.model.element.TypeElement)2 AnnotationTypeDeclaration (com.google.devtools.j2objc.ast.AnnotationTypeDeclaration)1 TypeDeclaration (com.google.devtools.j2objc.ast.TypeDeclaration)1 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)1 GeneratedPackageElement (com.google.devtools.j2objc.types.GeneratedPackageElement)1 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)1 ElementUtil (com.google.devtools.j2objc.util.ElementUtil)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 CaseTree (com.sun.source.tree.CaseTree)1 CatchTree (com.sun.source.tree.CatchTree)1