use of org.eclipse.jdt.core.dom.Block in project AutoRefactor by JnRouvignac.
the class ReduceVariableScopeRefactoring method copy.
private Block copy(Statement stmtToCopy, Name varName) {
if (stmtToCopy != null && !(stmtToCopy instanceof Block)) {
final Block b = this.ctx.getAST().newBlock();
final Assignment a = asExpression(stmtToCopy, Assignment.class);
if (a != null) {
final VariableDeclarationFragment vdf = getVariableDeclarationFragment(a, varName);
statements(b).add(this.ctx.getAST().newVariableDeclarationStatement(vdf));
} else {
throw new NotImplementedException(stmtToCopy);
}
return b;
}
// We should never come here if we had a Block statement, see the replace() method
throw new NotImplementedException(stmtToCopy);
}
use of org.eclipse.jdt.core.dom.Block in project AutoRefactor by JnRouvignac.
the class RemoveEmptyStatementRefactoring method visit.
@Override
public boolean visit(EmptyStatement node) {
ASTNode parent = node.getParent();
if (parent instanceof Block) {
this.ctx.getRefactorings().remove(node);
return DO_NOT_VISIT_SUBTREE;
}
parent = getParentIgnoring(node, Block.class);
if (parent instanceof IfStatement) {
final IfStatement is = (IfStatement) parent;
if (isPassive(is.getExpression())) {
final List<Statement> thenStmts = asList(is.getThenStatement());
final List<Statement> elseStmts = asList(is.getElseStatement());
final boolean thenIsEmptyStmt = thenStmts.size() == 1 && is(thenStmts.get(0), EmptyStatement.class);
final boolean elseIsEmptyStmt = elseStmts.size() == 1 && is(elseStmts.get(0), EmptyStatement.class);
if (thenIsEmptyStmt && elseIsEmptyStmt) {
this.ctx.getRefactorings().remove(parent);
return DO_NOT_VISIT_SUBTREE;
} else if (thenIsEmptyStmt && is.getElseStatement() == null) {
this.ctx.getRefactorings().remove(is);
return DO_NOT_VISIT_SUBTREE;
} else if (elseIsEmptyStmt) {
this.ctx.getRefactorings().remove(is.getElseStatement());
return DO_NOT_VISIT_SUBTREE;
}
}
} else if (parent instanceof EnhancedForStatement) {
final EnhancedForStatement efs = (EnhancedForStatement) parent;
if (isPassive(efs.getExpression()) && efs.getExpression().resolveTypeBinding().isArray()) {
return maybeRemoveEmptyStmtBody(node, efs, efs.getBody());
}
} else if (parent instanceof ForStatement) {
final ForStatement fs = (ForStatement) parent;
if (arePassive(fs.initializers()) && isPassive(fs.getExpression())) {
return maybeRemoveEmptyStmtBody(node, fs, fs.getBody());
}
} else if (parent instanceof WhileStatement) {
final WhileStatement ws = (WhileStatement) parent;
if (isPassive(ws.getExpression()) && !Boolean.TRUE.equals(ws.getExpression().resolveConstantExpressionValue())) {
return maybeRemoveEmptyStmtBody(node, ws, ws.getBody());
}
} else if (parent instanceof DoStatement) {
final DoStatement ds = (DoStatement) parent;
if (isPassive(ds.getExpression()) && !Boolean.TRUE.equals(ds.getExpression().resolveConstantExpressionValue())) {
return maybeRemoveEmptyStmtBody(node, ds, ds.getBody());
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.Block in project AutoRefactor by JnRouvignac.
the class ASTBuilder method catch0.
/**
* Builds a new {@link CatchClause} instance.
*
* @param exceptionTypeName the exception type name
* @param caughtExceptionName the local name for the caught exception
* @param stmts the statements to add to the catch clause
* @return a new catch clause
*/
public CatchClause catch0(String exceptionTypeName, String caughtExceptionName, Statement... stmts) {
final CatchClause cc = ast.newCatchClause();
final SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
svd.setType(simpleType(exceptionTypeName));
svd.setName(ast.newSimpleName(caughtExceptionName));
cc.setException(svd);
final Block block = ast.newBlock();
addAll(statements(block), stmts);
cc.setBody(block);
return cc;
}
use of org.eclipse.jdt.core.dom.Block in project eclipse-pmd by acanda.
the class UseUtilityClassQuickFix method addPrivateConstructor.
@SuppressWarnings("unchecked")
private void addPrivateConstructor(final TypeDeclaration typeDeclaration, final ASTRewrite rewrite) {
final AST ast = typeDeclaration.getAST();
final MethodDeclaration constructor = (MethodDeclaration) ast.createInstance(MethodDeclaration.class);
constructor.setConstructor(true);
final Modifier modifier = (Modifier) ast.createInstance(Modifier.class);
modifier.setKeyword(ModifierKeyword.PRIVATE_KEYWORD);
constructor.modifiers().add(modifier);
constructor.setName(ASTUtil.copy(typeDeclaration.getName()));
final Block body = (Block) ast.createInstance(Block.class);
constructor.setBody(body);
final ListRewrite statementRewrite = rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
final ASTNode comment = rewrite.createStringPlaceholder("// hide constructor of utility class", ASTNode.EMPTY_STATEMENT);
statementRewrite.insertFirst(comment, null);
final int position = findConstructorPosition(typeDeclaration);
final ListRewrite bodyDeclarationRewrite = rewrite.getListRewrite(typeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
bodyDeclarationRewrite.insertAt(constructor, position, null);
}
use of org.eclipse.jdt.core.dom.Block in project flux by eclipse.
the class AdvancedQuickAssistProcessor method getExchangeInnerAndOuterIfConditionsProposals.
private static boolean getExchangeInnerAndOuterIfConditionsProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
boolean result = false;
//
if (!(node instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) node;
if (ifStatement.getElseStatement() != null) {
return false;
}
// case when current IfStatement is sole child of another IfStatement
{
IfStatement outerIf = null;
if (ifStatement.getParent() instanceof IfStatement) {
outerIf = (IfStatement) ifStatement.getParent();
} else if (ifStatement.getParent() instanceof Block) {
Block block = (Block) ifStatement.getParent();
if (block.getParent() instanceof IfStatement && block.statements().size() == 1) {
outerIf = (IfStatement) block.getParent();
}
}
if (outerIf != null && outerIf.getElseStatement() == null) {
if (resultingCollections == null) {
return true;
}
//
AST ast = node.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// prepare conditions
Expression outerCondition = (Expression) rewrite.createCopyTarget(outerIf.getExpression());
Expression innerCondition = (Expression) rewrite.createCopyTarget(ifStatement.getExpression());
// exchange conditions
rewrite.replace(outerIf.getExpression(), innerCondition, null);
rewrite.replace(ifStatement.getExpression(), outerCondition, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_exchangeInnerAndOuterIfConditions_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.EXCHANGE_INNER_AND_OUTER_IF_CONDITIONS);
resultingCollections.add(proposal);
result = true;
}
}
// case when current IfStatement has another IfStatement as sole child
{
IfStatement innerIf = null;
if (ifStatement.getThenStatement() instanceof IfStatement) {
innerIf = (IfStatement) ifStatement.getThenStatement();
} else if (ifStatement.getThenStatement() instanceof Block) {
Block block = (Block) ifStatement.getThenStatement();
if (block.statements().size() == 1 && block.statements().get(0) instanceof IfStatement) {
innerIf = (IfStatement) block.statements().get(0);
}
}
if (innerIf != null && innerIf.getElseStatement() == null) {
if (resultingCollections == null) {
return true;
}
//
AST ast = node.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// prepare conditions
Expression innerCondition = (Expression) rewrite.createCopyTarget(innerIf.getExpression());
Expression outerCondition = (Expression) rewrite.createCopyTarget(ifStatement.getExpression());
// exchange conditions
rewrite.replace(innerIf.getExpression(), outerCondition, null);
rewrite.replace(ifStatement.getExpression(), innerCondition, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_exchangeInnerAndOuterIfConditions_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.EXCHANGE_INNER_AND_OUTER_IF_CONDITIONS);
resultingCollections.add(proposal);
result = true;
}
}
return result;
}
Aggregations