use of com.google.devtools.j2objc.ast.VariableDeclarationStatement 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.VariableDeclarationStatement in project j2objc by google.
the class Rewriter method endVisit.
@Override
public void endVisit(VariableDeclarationStatement node) {
if (options.isJDT()) {
ListMultimap<Integer, VariableDeclarationFragment> newDeclarations = rewriteExtraDimensions(node.getFragments());
if (newDeclarations != null) {
List<Statement> statements = ((Block) node.getParent()).getStatements();
int location = 0;
while (location < statements.size() && !node.equals(statements.get(location))) {
location++;
}
for (Integer dimensions : newDeclarations.keySet()) {
List<VariableDeclarationFragment> fragments = newDeclarations.get(dimensions);
VariableDeclarationStatement newDecl = new VariableDeclarationStatement(fragments.get(0));
newDecl.getFragments().addAll(fragments.subList(1, fragments.size()));
statements.add(++location, newDecl);
}
}
}
}
use of com.google.devtools.j2objc.ast.VariableDeclarationStatement in project j2objc by google.
the class TreeConverter method convertVariableDeclaration.
private TreeNode convertVariableDeclaration(VariableTree node, TreePath parent) {
TreePath path = getTreePath(parent, node);
VariableElement element = (VariableElement) getElement(path);
if (element.getKind() == ElementKind.FIELD) {
FieldDeclaration newNode = new FieldDeclaration(element, (Expression) convert(node.getInitializer(), path));
convertBodyDeclaration(node, parent, node.getModifiers(), newNode);
return newNode;
}
if (element.getKind() == ElementKind.LOCAL_VARIABLE) {
return new VariableDeclarationStatement(element, (Expression) convert(node.getInitializer(), path));
}
if (element.getKind() == ElementKind.ENUM_CONSTANT) {
EnumConstantDeclaration newNode = new EnumConstantDeclaration().setVariableElement(element);
convertBodyDeclaration(node, parent, node.getModifiers(), newNode);
ClassInstanceCreation init = (ClassInstanceCreation) convert(node.getInitializer(), path);
TreeUtil.moveList(init.getArguments(), newNode.getArguments());
if (init.getAnonymousClassDeclaration() != null) {
newNode.setAnonymousClassDeclaration(TreeUtil.remove(init.getAnonymousClassDeclaration()));
}
return newNode.setExecutablePair(init.getExecutablePair()).setVarargsType(init.getVarargsType());
}
return convertSingleVariable(node, parent);
}
use of com.google.devtools.j2objc.ast.VariableDeclarationStatement in project j2objc by google.
the class MethodTranslator method visitForStatement.
@Override
public TreeNode visitForStatement(com.strobel.decompiler.languages.java.ast.ForStatement node, Void data) {
ForStatement forStatement = new ForStatement();
for (com.strobel.decompiler.languages.java.ast.Statement init : node.getInitializers()) {
TreeNode stmt = init.acceptVisitor(this, null);
if (stmt instanceof VariableDeclarationStatement) {
VariableDeclarationStatement varDeclStmt = (VariableDeclarationStatement) stmt;
VariableDeclarationExpression varDeclExpr = new VariableDeclarationExpression().setType(Type.newType(varDeclStmt.getTypeMirror()));
for (VariableDeclarationFragment varFrag : varDeclStmt.getFragments()) {
varDeclExpr.addFragment(varFrag.copy());
}
forStatement.addInitializer(varDeclExpr);
} else {
forStatement.addInitializer(((ExpressionStatement) stmt).getExpression().copy());
}
}
forStatement.setExpression((Expression) node.getCondition().acceptVisitor(this, null));
for (com.strobel.decompiler.languages.java.ast.Statement updater : node.getIterators()) {
TreeNode stmt = updater.acceptVisitor(this, null);
forStatement.addUpdater(((ExpressionStatement) stmt).getExpression().copy());
}
forStatement.setBody((Statement) node.getEmbeddedStatement().acceptVisitor(this, null));
return forStatement;
}
use of com.google.devtools.j2objc.ast.VariableDeclarationStatement in project j2objc by google.
the class Rewriter method visit.
@Override
public boolean visit(TryStatement node) {
// This visit rewrites try-with-resources constructs into regular try statements according to
// JLS 14.20.3. The rewriting is done in a visit instead of endVisit because the mutations may
// result in more try-with-resources constructs that need to be rewritten recursively.
List<TreeNode> resources = node.getResources();
if (resources.isEmpty()) {
return true;
}
if (!node.getCatchClauses().isEmpty() || node.getFinally() != null) {
// Extended try-with-resources. (JLS 14.20.3.2)
// The new innerTry statement will be a "Basic try-with-resources" and will be rewritten by
// the code below when it is visited.
TryStatement innerTry = new TryStatement().setBody(TreeUtil.remove(node.getBody()));
;
TreeUtil.moveList(resources, innerTry.getResources());
node.setBody(new Block().addStatement(innerTry));
return true;
}
// Basic try-with-resources. (JLS 14.20.3.1)
DeclaredType throwableType = (DeclaredType) typeUtil.getJavaThrowable().asType();
VariableElement primaryException = GeneratedVariableElement.newLocalVar("__primaryException" + resources.size(), throwableType, null);
TreeNode resource = resources.remove(0);
VariableElement resourceVar = null;
VariableDeclarationFragment resourceFrag = null;
if (resource.getKind() == Kind.VARIABLE_DECLARATION_EXPRESSION) {
List<VariableDeclarationFragment> resourceFrags = ((VariableDeclarationExpression) resource).getFragments();
assert resourceFrags.size() == 1;
resourceFrag = resourceFrags.get(0);
resourceVar = resourceFrag.getVariableElement();
} else {
resourceVar = TreeUtil.getVariableElement((Expression) resource);
}
assert resourceVar != null;
DeclaredType closeableType = typeUtil.findSupertype(resourceVar.asType(), "java.lang.AutoCloseable");
ExecutablePair closeMethod = typeUtil.findMethod(closeableType, "close");
ExecutablePair addSuppressedMethod = typeUtil.findMethod(throwableType, "addSuppressed", "java.lang.Throwable");
Block block = new Block();
if (resourceFrag != null) {
block.addStatement(new VariableDeclarationStatement(resourceVar, TreeUtil.remove(resourceFrag.getInitializer())));
}
block.addStatement(new VariableDeclarationStatement(primaryException, new NullLiteral(typeUtil.getNull())));
// If the current try node is the only statement in its parent block then replace the parent
// block instead of the try node to avoid extra nesting of braces.
TreeNode parent = node.getParent();
if (parent instanceof Block && ((Block) parent).getStatements().size() == 1) {
parent.replaceWith(block);
} else {
node.replaceWith(block);
}
block.addStatement(TreeUtil.remove(node));
VariableElement caughtException = GeneratedVariableElement.newLocalVar("e", throwableType, null);
Block catchBlock = new Block().addStatement(new ExpressionStatement(new Assignment(new SimpleName(primaryException), new SimpleName(caughtException)))).addStatement(new ThrowStatement(new SimpleName(caughtException)));
node.addCatchClause(new CatchClause().setException(new SingleVariableDeclaration(caughtException)).setBody(catchBlock));
Statement closeResource = new ExpressionStatement(new MethodInvocation(closeMethod, typeUtil.getVoid(), new SimpleName(resourceVar)));
VariableElement suppressedException = GeneratedVariableElement.newLocalVar("e", throwableType, null);
node.setFinally(new Block().addStatement(new IfStatement().setExpression(new InfixExpression(typeUtil.getBoolean(), InfixExpression.Operator.NOT_EQUALS, new SimpleName(resourceVar), new NullLiteral(typeUtil.getNull()))).setThenStatement(new Block().addStatement(new IfStatement().setExpression(new InfixExpression(typeUtil.getBoolean(), InfixExpression.Operator.NOT_EQUALS, new SimpleName(primaryException), new NullLiteral(typeUtil.getNull()))).setThenStatement(new Block().addStatement(new TryStatement().setBody(new Block().addStatement(closeResource)).addCatchClause(new CatchClause().setException(new SingleVariableDeclaration(suppressedException)).setBody(new Block().addStatement(new ExpressionStatement(new MethodInvocation(addSuppressedMethod, typeUtil.getVoid(), new SimpleName(primaryException)).addArgument(new SimpleName(suppressedException)))))))).setElseStatement(new Block().addStatement(closeResource.copy()))))));
// Visit the new block instead of the current node because some of content of the node (eg. the
// resource initializer) has been moved outside of the try node.
block.accept(this);
return false;
}
Aggregations