List of usage examples for org.eclipse.jdt.core.dom IfStatement setThenStatement
public void setThenStatement(Statement statement)
From source file:java5totext.input.JDTVisitor.java
License:Open Source License
@Override public void endVisit(org.eclipse.jdt.core.dom.IfStatement node) { IfStatement element = (IfStatement) this.binding.get(node); this.initializeNode(element, node); if (this.binding.get(node.getExpression()) != null) element.setExpression((Expression) this.binding.get(node.getExpression())); if (this.binding.get(node.getThenStatement()) != null) element.setThenStatement((Statement) this.binding.get(node.getThenStatement())); if (this.binding.get(node.getElseStatement()) != null) element.setElseStatement((Statement) this.binding.get(node.getElseStatement())); }
From source file:net.sf.eclipsecs.ui.quickfixes.blocks.NeedBracesQuickfix.java
License:Open Source License
/** * {@inheritDoc}//from w ww.ja v a2 s. c om */ protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) { return new ASTVisitor() { public boolean visit(IfStatement node) { int nodePos = node.getStartPosition(); int nodeEnd = nodePos + node.getLength(); if ((nodePos >= lineInfo.getOffset() && nodePos <= (lineInfo.getOffset() + lineInfo.getLength())) || (nodePos <= lineInfo.getOffset() && nodeEnd >= lineInfo.getOffset() + lineInfo.getLength())) { bracifyIfStatement(node); } return true; } /** * Helper method to recursivly bracify a if-statement. * * @param ifStatement the if statement */ private void bracifyIfStatement(IfStatement ifStatement) { // change the then statement to a block if necessary if (!(ifStatement.getThenStatement() instanceof Block)) { if (ifStatement.getThenStatement() instanceof IfStatement) { bracifyIfStatement((IfStatement) ifStatement.getThenStatement()); } Block block = createBracifiedCopy(ifStatement.getAST(), ifStatement.getThenStatement()); ifStatement.setThenStatement(block); } // check the else statement if it is a block Statement elseStatement = ifStatement.getElseStatement(); if (elseStatement != null && !(elseStatement instanceof Block)) { // in case the else statement is an further if statement // (else if) // do the recursion if (elseStatement instanceof IfStatement) { bracifyIfStatement((IfStatement) elseStatement); } else { // change the else statement to a block // Block block = ifStatement.getAST().newBlock(); // block.statements().add(ASTNode.copySubtree(block.getAST(), // elseStatement)); Block block = createBracifiedCopy(ifStatement.getAST(), ifStatement.getElseStatement()); ifStatement.setElseStatement(block); } } } public boolean visit(ForStatement node) { if (containsPosition(lineInfo, node.getStartPosition())) { Block block = createBracifiedCopy(node.getAST(), node.getBody()); node.setBody(block); } return true; } public boolean visit(DoStatement node) { if (containsPosition(lineInfo, node.getStartPosition())) { Block block = createBracifiedCopy(node.getAST(), node.getBody()); node.setBody(block); } return true; } public boolean visit(WhileStatement node) { if (containsPosition(lineInfo, node.getStartPosition())) { Block block = createBracifiedCopy(node.getAST(), node.getBody()); node.setBody(block); } return true; } private Block createBracifiedCopy(AST ast, Statement body) { Block block = ast.newBlock(); block.statements().add(ASTNode.copySubtree(block.getAST(), body)); return block; } }; }
From source file:org.asup.dk.compiler.rpj.writer.JDTStatementWriter.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/* w ww. j a v a 2 s . c o m*/ public boolean visit(QIf statement) { Block block = blocks.peek(); IfStatement ifSt = ast.newIfStatement(); QPredicateExpression condition = expressionParser.parsePredicate(statement.getCondition()); Expression expression = null; if (CompilationContextHelper.isPrimitive(compilationUnit, condition)) expression = buildExpression(ast, condition, Boolean.class); else { expression = buildExpression(ast, condition, Boolean.class); } // expression = buildExpression(ast, condition, null); ifSt.setExpression(expression); block.statements().add(ifSt); // then if (statement.getThen() != null) { Block thenBlock = null; if (ifSt.getThenStatement() instanceof Block) thenBlock = (Block) ifSt.getThenStatement(); else { thenBlock = ast.newBlock(); ifSt.setThenStatement(thenBlock); } blocks.push(thenBlock); statement.getThen().accept(this); blocks.pop(); } // else if (statement.getElse() != null) { Block elseBlock = null; if (ifSt.getElseStatement() instanceof Block) elseBlock = (Block) ifSt.getElseStatement(); else { elseBlock = ast.newBlock(); ifSt.setElseStatement(elseBlock); } // walk else blocks.push(elseBlock); statement.getElse().accept(this); blocks.pop(); } // interrupt navigation return false; }
From source file:org.asup.dk.compiler.rpj.writer.JDTStatementWriter.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w w w . ja va 2 s. c om public boolean visit(QJump statement) { Block block = blocks.peek(); // dummy break if (isParentFor(statement)) { IfStatement ifSt = ast.newIfStatement(); ifSt.setExpression(ast.newBooleanLiteral(false)); ifSt.setThenStatement(ast.newBreakStatement()); block.statements().add(ifSt); } MethodInvocation methodInvocation = ast.newMethodInvocation(); methodInvocation.setExpression(ast.newSimpleName("qRPJ")); methodInvocation.setName(ast.newSimpleName("qJump")); Name labelName = ast.newName(new String[] { "TAG", statement.getLabel() }); methodInvocation.arguments().add(0, labelName); ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation); block.statements().add(expressionStatement); return super.visit(statement); }
From source file:org.asup.dk.compiler.rpj.writer.JDTStatementWriter.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from ww w . j a v a 2s . c o m public boolean visit(QReturn statement) { Block block = blocks.peek(); ReturnStatement returnSt = ast.newReturnStatement(); if (isParentProcedure(statement)) { block.statements().add(returnSt); } else { // dummy condition IfStatement ifSt = ast.newIfStatement(); ifSt.setExpression(ast.newBooleanLiteral(true)); ifSt.setThenStatement(returnSt); block.statements().add(ifSt); } return false; }
From source file:org.autorefactor.refactoring.ASTBuilder.java
License:Open Source License
/** * Builds a new {@link IfStatement} instance. * * @param condition the if condition/*from ww w.ja va 2 s. c om*/ * @param thenStatement the statement of the then clause * @param elseStatement the statement of the else clause * @return a new if statement */ public IfStatement if0(Expression condition, Statement thenStatement, Statement elseStatement) { final IfStatement is = ast.newIfStatement(); is.setExpression(condition); is.setThenStatement(thenStatement); is.setElseStatement(elseStatement); return is; }
From source file:org.autorefactor.refactoring.rules.ReduceVariableScopeRefactoring.java
License:Open Source License
private void replace(VariableAccess varDecl, VariableAccess varAccess) { final ASTBuilder b = this.ctx.getASTBuilder(); final AST ast = b.getAST(); final ASTNode scope = varAccess.getScope(); final Name varName = varAccess.getVariableName(); final Type varType = getType(varDecl.getVariableName().getParent()); if (scope instanceof Block) { final List<Statement> stmts = statements((Block) scope); for (int i = 0; i < stmts.size(); i++) { final Statement stmt = stmts.get(i); final Expression parentExpr = getAncestor(varName, Expression.class); // FIXME i=0 final Statement parentStmt = getAncestor(parentExpr, Statement.class); // FIXME i=0 if (stmt.equals(parentStmt)) { final VariableDeclarationFragment vdf = getVariableDeclarationFragment(parentExpr, varName); final VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf); vds.setType(varType);/*from w w w .ja va 2s. c o m*/ this.ctx.getRefactorings().replace(stmt, vds); break; } } } else if (scope instanceof EnhancedForStatement) { final EnhancedForStatement efs = (EnhancedForStatement) scope; final EnhancedForStatement newEfs = b.copy(efs); newEfs.setParameter(b.copy(efs.getParameter())); newEfs.setExpression(b.copy(efs.getExpression())); final Statement parentStmt = getAncestor(varName, Statement.class); if (equalNotNull(efs.getBody(), parentStmt)) { newEfs.setBody(copy(efs.getBody(), varName)); } this.ctx.getRefactorings().replace(efs, newEfs); } else if (scope instanceof ForStatement) { final ForStatement fs = (ForStatement) scope; final ForStatement newFs = b.copy(fs); final List<Expression> initializers = initializers(newFs); if (initializers.size() == 1) { final Expression init = initializers.remove(0); final VariableDeclarationFragment vdf = getVariableDeclarationFragment(init, varName); final VariableDeclarationExpression vde = ast.newVariableDeclarationExpression(vdf); vde.setType(varType); initializers.add(vde); this.ctx.getRefactorings().replace(fs, newFs); // TODO JNR // if (equalNotNull(fs.getBody(), parentStmt)) { // newFs.setBody(copy(fs.getBody())); // } } else { throw new NotImplementedException(scope, "for more than one initializer in for loop."); } } else if (scope instanceof WhileStatement) { final WhileStatement ws = (WhileStatement) scope; final WhileStatement newWs = ast.newWhileStatement(); newWs.setExpression(b.copy(ws.getExpression())); final Statement parentStmt = getAncestor(varName, Statement.class); if (equalNotNull(ws.getBody(), parentStmt)) { newWs.setBody(copy(ws.getBody(), varName)); } this.ctx.getRefactorings().replace(ws, newWs); } else if (scope instanceof IfStatement) { final IfStatement is = (IfStatement) scope; final IfStatement newIs = ast.newIfStatement(); newIs.setExpression(b.copy(is.getExpression())); final Statement parentStmt = getAncestor(varName, Statement.class); if (equalNotNull(is.getThenStatement(), parentStmt)) { newIs.setThenStatement(copy(is.getThenStatement(), varName)); if (is.getElseStatement() != null) { newIs.setElseStatement(b.copy(is.getElseStatement())); } this.ctx.getRefactorings().replace(is, newIs); } else if (equalNotNull(is.getElseStatement(), parentStmt)) { if (is.getThenStatement() != null) { newIs.setThenStatement(b.copy(is.getThenStatement())); } newIs.setElseStatement(copy(is.getElseStatement(), varName)); this.ctx.getRefactorings().replace(is, newIs); } else { throw new IllegalStateException(is, "Parent statement should be inside the then or else statement of this if statement: " + is); } } else { throw new NotImplementedException(scope); } }
From source file:org.autorefactor.refactoring.rules.ViewHolderRefactoring.java
License:Open Source License
@Override public boolean visit(MethodDeclaration node) { final ASTBuilder b = this.ctx.getASTBuilder(); final Refactorings r = this.ctx.getRefactorings(); IMethodBinding methodBinding = node.resolveBinding(); if (methodBinding != null && isMethod(methodBinding, "android.widget.Adapter", "getView", "int", "android.view.View", "android.view.ViewGroup")) { GetViewVisitor visitor = new GetViewVisitor(); Block body = node.getBody();/* ww w . j ava 2s . co m*/ if (body != null) { body.accept(visitor); if (!visitor.usesConvertView && visitor.viewVariable != null && !visitor.isInflateInsideIf()) { // Transform tree //Create If statement IfStatement ifStatement = b.getAST().newIfStatement(); // test-clause InfixExpression infixExpression = b.getAST().newInfixExpression(); infixExpression.setOperator(InfixExpression.Operator.EQUALS); infixExpression.setLeftOperand(b.simpleName("convertView")); infixExpression.setRightOperand(b.getAST().newNullLiteral()); ifStatement.setExpression(infixExpression); //then Assignment assignment = b.assign(b.simpleName("convertView"), Assignment.Operator.ASSIGN, b.copy(visitor.getInflateExpression())); Block thenBlock = b.block(b.getAST().newExpressionStatement(assignment)); ifStatement.setThenStatement(thenBlock); r.insertBefore(ifStatement, visitor.viewAssignmentStatement); // assign to local view variable when necessary if (!"convertView".equals(visitor.viewVariable.getIdentifier())) { Statement assignConvertViewToView = null; if (visitor.viewVariableDeclarationFragment != null) { assignConvertViewToView = b.declare(visitor.viewVariable.resolveTypeBinding().getName(), b.copy(visitor.viewVariable), b.simpleName("convertView")); } else if (visitor.viewVariableAssignment != null) { assignConvertViewToView = b.getAST() .newExpressionStatement(b.assign(b.copy(visitor.viewVariable), Assignment.Operator.ASSIGN, b.simpleName("convertView"))); } if (assignConvertViewToView != null) { r.insertBefore(assignConvertViewToView, visitor.viewAssignmentStatement); } } // make sure method returns the view to be reused DELETEME if (visitor.returnStatement != null) { r.insertAfter(b.return0(b.copy(visitor.viewVariable)), visitor.returnStatement); r.remove(visitor.returnStatement); } //Optimize findViewById calls FindViewByIdVisitor findViewByIdVisitor = new FindViewByIdVisitor(); body.accept(findViewByIdVisitor); if (findViewByIdVisitor.items.size() > 0) { //create ViewHolderItem class TypeDeclaration viewHolderItemDeclaration = b.getAST().newTypeDeclaration(); viewHolderItemDeclaration.setName(b.simpleName("ViewHolderItem")); List<ASTNode> viewItemsDeclarations = viewHolderItemDeclaration.bodyDeclarations(); for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) { VariableDeclarationFragment declarationFragment = b.getAST() .newVariableDeclarationFragment(); SimpleName simpleName = b.simpleName(item.variable.getIdentifier()); declarationFragment.setName(simpleName); FieldDeclaration fieldDeclaration = b.getAST().newFieldDeclaration(declarationFragment); fieldDeclaration.setType(b.getAST() .newSimpleType(b.simpleName(item.variable.resolveTypeBinding().getName()))); viewItemsDeclarations.add(fieldDeclaration); } viewHolderItemDeclaration.modifiers() .add(b.getAST().newModifier(ModifierKeyword.STATIC_KEYWORD)); r.insertBefore(viewHolderItemDeclaration, node); // create viewhHolderItem object VariableDeclarationStatement viewHolderItemVariableDeclaration = b.declare("ViewHolderItem", b.simpleName("viewHolderItem"), null); r.insertAt(viewHolderItemVariableDeclaration, 0, Block.STATEMENTS_PROPERTY, body); //initialize viewHolderItem Assignment viewHolderItemInitialization = b.assign(b.simpleName("viewHolderItem"), Assignment.Operator.ASSIGN, b.new0("ViewHolderItem")); thenBlock.statements().add(b.getAST().newExpressionStatement(viewHolderItemInitialization)); // Assign findViewById to ViewHolderItem for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) { //ensure we are accessing to convertView object QualifiedName qualifiedName = b.getAST().newQualifiedName(b.name("viewHolderItem"), b.simpleName(item.variable.getIdentifier())); item.findViewByIdInvocation.setExpression(b.simpleName("convertView")); Assignment itemAssignment = b.assign(qualifiedName, Assignment.Operator.ASSIGN, (Expression) ASTNode.copySubtree(b.getAST(), item.findViewByIdExpression)); thenBlock.statements().add(b.getAST().newExpressionStatement(itemAssignment)); //replace previous fidnviewbyid with accesses to viewHolderItem QualifiedName viewHolderItemFieldAccessQualifiedName = b.getAST().newQualifiedName( b.name("viewHolderItem"), b.simpleName(item.variable.getIdentifier())); r.replace(item.findViewByIdExpression, b.copy(qualifiedName)); } //store viewHolderItem in convertView MethodInvocation setTagInvocation = b.invoke("convertView", "setTag", b.simpleName("viewHolderItem")); thenBlock.statements().add(b.getAST().newExpressionStatement(setTagInvocation)); //retrieve viewHolderItem from convertView ifStatement .setElseStatement(b.block(b.getAST() .newExpressionStatement(b.assign(b.simpleName("viewHolderItem"), Assignment.Operator.ASSIGN, b.cast("ViewHolderItem", b.invoke("convertView", "getTag")))))); } r.remove(visitor.viewAssignmentStatement); return DO_NOT_VISIT_SUBTREE; } } } return VISIT_SUBTREE; }
From source file:org.autorefactor.refactoring.rules.WakeLockRefactoring.java
License:Open Source License
private Statement createWakelockReleaseNode(MethodInvocation methodInvocation) { final ASTBuilder b = this.ctx.getASTBuilder(); IfStatement ifStatement = b.getAST().newIfStatement(); // test-clause PrefixExpression prefixExpression = b.getAST().newPrefixExpression(); prefixExpression.setOperator(PrefixExpression.Operator.NOT); MethodInvocation isHeldInvocation = b.getAST().newMethodInvocation(); isHeldInvocation.setName(b.simpleName("isHeld")); isHeldInvocation.setExpression(b.copyExpression(methodInvocation)); prefixExpression.setOperand(isHeldInvocation); ifStatement.setExpression(prefixExpression); // then/*from ww w.j a va 2 s .c o m*/ MethodInvocation releaseInvocation = b.getAST().newMethodInvocation(); releaseInvocation.setName(b.simpleName("release")); releaseInvocation.setExpression(b.copyExpression(methodInvocation)); ExpressionStatement releaseExpressionStatement = b.getAST().newExpressionStatement(releaseInvocation); ifStatement.setThenStatement(b.block(releaseExpressionStatement)); return ifStatement; }
From source file:org.decojer.cavaj.transformers.TrControlFlowStmts.java
License:Open Source License
@Nullable private Statement transformCond(@Nonnull final Cond cond) { final BB head = cond.getHead(); final IfStatement ifStatement = (IfStatement) head.getFinalStmt(); if (ifStatement == null) { assert false; return null; }//from w w w .j a v a 2 s .c om final Expression condExpression = ifStatement.getExpression(); if (condExpression == null) { assert false; return null; } final E falseOut = head.getFalseOut(); assert falseOut != null; final E trueOut = head.getTrueOut(); assert trueOut != null; boolean negate = false; switch (cond.getKind()) { case IFNOT: ifStatement.setExpression(wrap(not(condExpression))); negate = true; // fall through case IF: { final AssertStatement assertStmt = transformAssert(negate ? falseOut : trueOut, condExpression); if (assertStmt != null) { return assertStmt; } ifStatement.setThenStatement(transformSequence(cond, negate ? falseOut : trueOut)); return ifStatement; } case IFNOT_ELSE: ifStatement.setExpression(wrap(not(condExpression))); negate = true; // fall through case IF_ELSE: ifStatement.setThenStatement(transformSequence(cond, negate ? falseOut : trueOut)); ifStatement.setElseStatement(transformSequence(cond, negate ? trueOut : falseOut)); return ifStatement; default: log.warn(getM() + ": Unknown cond type '" + cond.getKind() + "'!"); return null; } }