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;
}
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();
}
}
}
}
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);
}
}
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;
}
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);
}
}
}
}
}
Aggregations