use of com.google.devtools.j2objc.ast.Statement in project j2objc by google.
the class GwtConverter method visit.
@Override
public boolean visit(IfStatement node) {
if (isGwtTest(node.getExpression())) {
if (node.getElseStatement() != null) {
// Replace this node with the else statement.
Statement replacement = TreeUtil.remove(node.getElseStatement());
node.replaceWith(replacement);
replacement.accept(this);
} else {
// with an empty statement.
if (node.getParent() instanceof Block) {
node.remove();
} else {
node.replaceWith(new EmptyStatement());
}
}
return false;
}
return true;
}
use of com.google.devtools.j2objc.ast.Statement 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.Statement in project j2objc by google.
the class JavaCloneWriter method endVisit.
@Override
public void endVisit(TypeDeclaration node) {
TypeElement type = node.getTypeElement();
VariableElement originalVar = GeneratedVariableElement.newParameter("original", type.asType(), null);
List<Statement> adjustments = getFieldAdjustments(node, originalVar);
if (adjustments.isEmpty()) {
return;
}
TypeMirror voidType = typeUtil.getVoid();
ExecutableElement javaCloneElement = GeneratedExecutableElement.newMethodWithSelector(JAVA_CLONE_METHOD, voidType, type).addParameter(originalVar);
MethodDeclaration declaration = new MethodDeclaration(javaCloneElement);
declaration.setHasDeclaration(false);
node.addBodyDeclaration(declaration);
declaration.addParameter(new SingleVariableDeclaration(originalVar));
Block body = new Block();
declaration.setBody(body);
List<Statement> statements = body.getStatements();
ExecutableElement javaCloneSuperElement = GeneratedExecutableElement.newMethodWithSelector(JAVA_CLONE_METHOD, voidType, typeUtil.getJavaObject());
SuperMethodInvocation superCall = new SuperMethodInvocation(new ExecutablePair(javaCloneSuperElement));
superCall.addArgument(new SimpleName(originalVar));
statements.add(new ExpressionStatement(superCall));
statements.addAll(adjustments);
}
use of com.google.devtools.j2objc.ast.Statement in project j2objc by google.
the class ComplexExpressionExtractor method handleNode.
private void handleNode(Expression node, Collection<Expression> children) {
if (node.getParent() instanceof Statement) {
return;
}
int depth = 0;
for (Expression child : children) {
Integer childDepth = depths.get(child);
depth = Math.max(depth, childDepth != null ? childDepth : 1);
}
if (depth >= maxDepth) {
VariableElement newVar = GeneratedVariableElement.newLocalVar("complex$" + count++, node.getTypeMirror(), currentMethod);
Statement newStmt = new VariableDeclarationStatement(newVar, node.copy());
assert currentStatement != null;
TreeUtil.insertBefore(currentStatement, newStmt);
node.replaceWith(new SimpleName(newVar));
} else {
depths.put(node, depth + 1);
}
}
use of com.google.devtools.j2objc.ast.Statement in project j2objc by google.
the class ConstantBranchPruner method endVisit.
@Override
public void endVisit(IfStatement node) {
Expression expr = node.getExpression();
Boolean value = getKnownValue(expr);
if (value != null) {
Statement sideEffects = getSideEffects(expr);
if (sideEffects != null) {
TreeUtil.insertBefore(node, sideEffects);
}
if (value) {
node.replaceWith(TreeUtil.remove(node.getThenStatement()));
} else if (node.getElseStatement() != null) {
node.replaceWith(TreeUtil.remove(node.getElseStatement()));
} else {
node.remove();
}
}
}
Aggregations