Search in sources :

Example 1 with VariableDeclarationFragment

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

the class DeadCodeEliminator method isInlinableConstant.

private boolean isInlinableConstant(BodyDeclaration decl) {
    if (!(decl instanceof FieldDeclaration)) {
        return false;
    }
    int modifiers = decl.getModifiers();
    if (!Modifier.isStatic(modifiers) || !Modifier.isFinal(modifiers) || Modifier.isPrivate(modifiers)) {
        return false;
    }
    TypeMirror type = ((FieldDeclaration) decl).getTypeMirror();
    if (!(type.getKind().isPrimitive() || typeUtil.isString(type))) {
        return false;
    }
    // Only when every fragment has constant value do we say this is inlinable.
    for (VariableDeclarationFragment fragment : ((FieldDeclaration) decl).getFragments()) {
        if (fragment.getVariableElement().getConstantValue() == null) {
            return false;
        }
    }
    return true;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 2 with VariableDeclarationFragment

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

the class DeadCodeEliminator method removeDeadFields.

/**
   * Deletes non-constant dead fields from a type's body declarations list.
   */
private void removeDeadFields(String clazz, List<BodyDeclaration> declarations) {
    Iterator<BodyDeclaration> declarationsIter = declarations.iterator();
    while (declarationsIter.hasNext()) {
        BodyDeclaration declaration = declarationsIter.next();
        if (declaration instanceof FieldDeclaration) {
            FieldDeclaration field = (FieldDeclaration) declaration;
            Iterator<VariableDeclarationFragment> fragmentsIter = field.getFragments().iterator();
            while (fragmentsIter.hasNext()) {
                VariableDeclarationFragment fragment = fragmentsIter.next();
                // Don't delete any constants because we can't detect their use.
                VariableElement var = fragment.getVariableElement();
                if (var.getConstantValue() == null && deadCodeMap.containsField(clazz, ElementUtil.getName(var))) {
                    fragmentsIter.remove();
                }
            }
            if (field.getFragments().isEmpty()) {
                declarationsIter.remove();
            }
        }
    }
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) BodyDeclaration(com.google.devtools.j2objc.ast.BodyDeclaration) VariableElement(javax.lang.model.element.VariableElement) FieldDeclaration(com.google.devtools.j2objc.ast.FieldDeclaration)

Example 3 with VariableDeclarationFragment

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

the class SwitchRewriter method fixVariableDeclarations.

/**
   * Moves all variable declarations above the first case statement.
   */
private void fixVariableDeclarations(SwitchStatement node) {
    List<Statement> statements = node.getStatements();
    Block block = new Block();
    List<Statement> blockStmts = block.getStatements();
    for (int i = 0; i < statements.size(); i++) {
        Statement stmt = statements.get(i);
        if (stmt instanceof VariableDeclarationStatement) {
            VariableDeclarationStatement declStmt = (VariableDeclarationStatement) stmt;
            statements.remove(i--);
            List<VariableDeclarationFragment> fragments = declStmt.getFragments();
            for (VariableDeclarationFragment decl : fragments) {
                Expression initializer = decl.getInitializer();
                if (initializer != null) {
                    Assignment assignment = new Assignment(new SimpleName(decl.getVariableElement()), TreeUtil.remove(initializer));
                    statements.add(++i, new ExpressionStatement(assignment));
                }
            }
            blockStmts.add(declStmt);
        }
    }
    if (blockStmts.size() > 0) {
        // There is at least one variable declaration, so copy this switch
        // statement into the new block and replace it in the parent list.
        node.replaceWith(block);
        blockStmts.add(node);
    }
}
Also used : Assignment(com.google.devtools.j2objc.ast.Assignment) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Block(com.google.devtools.j2objc.ast.Block) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement)

Example 4 with VariableDeclarationFragment

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

the class JavaCloneWriter method getFieldAdjustments.

private List<Statement> getFieldAdjustments(TypeDeclaration node, VariableElement originalVar) {
    List<Statement> adjustments = Lists.newArrayList();
    for (VariableDeclarationFragment decl : TreeUtil.getAllFields(node)) {
        VariableElement var = decl.getVariableElement();
        if (ElementUtil.isStatic(var) || var.asType().getKind().isPrimitive()) {
            continue;
        }
        boolean isWeak = ElementUtil.isWeakReference(var);
        boolean isVolatile = ElementUtil.isVolatile(var);
        if (isVolatile) {
            adjustments.add(createVolatileCloneStatement(var, originalVar, isWeak));
        } else if (isWeak) {
            adjustments.add(createReleaseStatement(var));
        }
    }
    return adjustments;
}
Also used : ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Statement(com.google.devtools.j2objc.ast.Statement) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 5 with VariableDeclarationFragment

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

the class Rewriter method endVisit.

@Override
public void endVisit(ForStatement node) {
    // nodes in the initializers.
    if (node.getInitializers().size() == 1) {
        Object initializer = node.getInitializer(0);
        if (initializer instanceof VariableDeclarationExpression) {
            List<VariableDeclarationFragment> fragments = ((VariableDeclarationExpression) initializer).getFragments();
            for (VariableDeclarationFragment fragment : fragments) {
                if (ElementUtil.hasAnnotation(fragment.getVariableElement(), AutoreleasePool.class)) {
                    Statement loopBody = node.getBody();
                    if (!(loopBody instanceof Block)) {
                        Block block = new Block();
                        node.setBody(block);
                        block.addStatement(loopBody);
                    }
                    ((Block) node.getBody()).setHasAutoreleasePool(true);
                }
            }
        }
    }
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) ForStatement(com.google.devtools.j2objc.ast.ForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) Block(com.google.devtools.j2objc.ast.Block)

Aggregations

VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)24 VariableElement (javax.lang.model.element.VariableElement)11 FieldDeclaration (com.google.devtools.j2objc.ast.FieldDeclaration)6 Statement (com.google.devtools.j2objc.ast.Statement)5 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)5 TypeMirror (javax.lang.model.type.TypeMirror)5 Block (com.google.devtools.j2objc.ast.Block)4 Expression (com.google.devtools.j2objc.ast.Expression)4 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)4 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)3 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)3 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)3 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)3 ForStatement (com.google.devtools.j2objc.ast.ForStatement)3 NameTable (com.google.devtools.j2objc.util.NameTable)3 BodyDeclaration (com.google.devtools.j2objc.ast.BodyDeclaration)2 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 EnumDeclaration (com.google.devtools.j2objc.ast.EnumDeclaration)2 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)2 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)2