use of org.eclipse.jdt.core.dom.ForStatement in project che by eclipse.
the class ConvertForLoopOperation method convert.
@Override
protected Statement convert(CompilationUnitRewrite cuRewrite, TextEditGroup group, LinkedProposalModel positionGroups) throws CoreException {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ImportRewrite importRewrite = cuRewrite.getImportRewrite();
ForStatement forStatement = getForStatement();
IJavaProject javaProject = ((CompilationUnit) forStatement.getRoot()).getJavaElement().getJavaProject();
String[] proposals = getVariableNameProposals(fArrayAccess.resolveTypeBinding(), javaProject);
String parameterName;
if (fElementDeclaration != null) {
parameterName = fElementDeclaration.getName().getIdentifier();
} else {
parameterName = proposals[0];
}
LinkedProposalPositionGroup pg = positionGroups.getPositionGroup(parameterName, true);
if (fElementDeclaration != null)
pg.addProposal(parameterName, null, 10);
for (int i = 0; i < proposals.length; i++) {
pg.addProposal(proposals[i], null, 10);
}
AST ast = forStatement.getAST();
EnhancedForStatement result = ast.newEnhancedForStatement();
SingleVariableDeclaration parameterDeclaration = createParameterDeclaration(parameterName, fElementDeclaration, fArrayAccess, forStatement, importRewrite, rewrite, group, pg, fMakeFinal);
result.setParameter(parameterDeclaration);
result.setExpression((Expression) rewrite.createCopyTarget(fArrayAccess));
convertBody(forStatement.getBody(), fIndexBinding, fArrayBinding, parameterName, rewrite, group, pg);
result.setBody(getBody(cuRewrite, group, positionGroups));
positionGroups.setEndPosition(rewrite.track(result));
return result;
}
use of org.eclipse.jdt.core.dom.ForStatement in project che by eclipse.
the class SourceProvider method isSingleControlStatementWithoutBlock.
private boolean isSingleControlStatementWithoutBlock() {
List<Statement> statements = fDeclaration.getBody().statements();
int size = statements.size();
if (size != 1)
return false;
Statement statement = statements.get(size - 1);
int nodeType = statement.getNodeType();
if (nodeType == ASTNode.IF_STATEMENT) {
IfStatement ifStatement = (IfStatement) statement;
return !(ifStatement.getThenStatement() instanceof Block) && !(ifStatement.getElseStatement() instanceof Block);
} else if (nodeType == ASTNode.FOR_STATEMENT) {
return !(((ForStatement) statement).getBody() instanceof Block);
} else if (nodeType == ASTNode.WHILE_STATEMENT) {
return !(((WhileStatement) statement).getBody() instanceof Block);
}
return false;
}
use of org.eclipse.jdt.core.dom.ForStatement in project AutoRefactor by JnRouvignac.
the class AllInOneMethodRatherThanLoopRefactoring method visit.
@Override
public boolean visit(ForStatement node) {
final ForLoopContent loopContent = iterateOverContainer(node);
final List<Statement> stmts = asList(node.getBody());
if (loopContent != null && loopContent.getLoopVariable() != null && stmts.size() == 1) {
final SimpleName loopVariable = (SimpleName) loopContent.getLoopVariable();
final IVariableBinding loopVariableName = (IVariableBinding) loopVariable.resolveBinding();
// As we replace only one, there should be no more than one occurrence
if (getVariableUseCount(loopVariableName, node.getBody()) == 1) {
final MethodInvocation mi = asExpression(stmts.get(0), MethodInvocation.class);
switch(loopContent.getContainerType()) {
case COLLECTION:
if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object")) {
return maybeReplaceWithCollectionMethod(node, loopContent, "addAll", mi);
} else if (isMethod(mi, "java.util.Collection", "remove", "java.lang.Object")) {
return maybeReplaceWithCollectionMethod(node, loopContent, "removeAll", mi);
}
break;
case ARRAY:
if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object") && areTypeCompatible(mi.getExpression(), loopContent.getContainerVariable())) {
final Expression addArg0 = arg0(mi);
final ArrayAccess aa = as(addArg0, ArrayAccess.class);
if (isSameVariable(loopContent, aa)) {
replaceWithCollectionsAddAll(node, loopContent.getContainerVariable(), mi);
return DO_NOT_VISIT_SUBTREE;
}
}
break;
}
}
}
return VISIT_SUBTREE;
}
Aggregations