use of org.eclipse.jdt.core.dom.EnhancedForStatement in project eclipse.jdt.ls by eclipse.
the class NewVariableCorrectionProposal method isEnhancedForStatementVariable.
private boolean isEnhancedForStatementVariable(Statement statement, SimpleName name) {
if (statement instanceof EnhancedForStatement) {
EnhancedForStatement forStatement = (EnhancedForStatement) statement;
SingleVariableDeclaration param = forStatement.getParameter();
// strange recovery, see https://bugs.eclipse.org/180456
return param.getType() == name.getParent();
}
return false;
}
use of org.eclipse.jdt.core.dom.EnhancedForStatement in project eclipse.jdt.ls by eclipse.
the class TypeMismatchSubProcessor method addTypeMismatchInForEachProposals.
public static void addTypeMismatchInForEachProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(astRoot);
if (selectedNode == null || selectedNode.getLocationInParent() != EnhancedForStatement.EXPRESSION_PROPERTY) {
return;
}
EnhancedForStatement forStatement = (EnhancedForStatement) selectedNode.getParent();
ITypeBinding expressionBinding = forStatement.getExpression().resolveTypeBinding();
if (expressionBinding == null) {
return;
}
ITypeBinding expectedBinding;
if (expressionBinding.isArray()) {
expectedBinding = expressionBinding.getComponentType();
} else {
// $NON-NLS-1$
IMethodBinding iteratorMethod = Bindings.findMethodInHierarchy(expressionBinding, "iterator", new String[0]);
if (iteratorMethod == null) {
return;
}
ITypeBinding[] typeArguments = iteratorMethod.getReturnType().getTypeArguments();
if (typeArguments.length != 1) {
return;
}
expectedBinding = typeArguments[0];
}
AST ast = astRoot.getAST();
expectedBinding = Bindings.normalizeForDeclarationUse(expectedBinding, ast);
SingleVariableDeclaration parameter = forStatement.getParameter();
ICompilationUnit cu = context.getCompilationUnit();
if (parameter.getName().getLength() == 0) {
SimpleName simpleName = null;
if (parameter.getType() instanceof SimpleType) {
SimpleType type = (SimpleType) parameter.getType();
if (type.getName() instanceof SimpleName) {
simpleName = (SimpleName) type.getName();
}
} else if (parameter.getType() instanceof NameQualifiedType) {
simpleName = ((NameQualifiedType) parameter.getType()).getName();
}
if (simpleName != null) {
String name = simpleName.getIdentifier();
int relevance = StubUtility.hasLocalVariableName(cu.getJavaProject(), name) ? 10 : 7;
String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_create_loop_variable_description, BasicElementLabels.getJavaElementName(name));
proposals.add(new NewVariableCorrectionProposal(label, cu, NewVariableCorrectionProposal.LOCAL, simpleName, null, relevance));
return;
}
}
String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_incompatible_for_each_type_description, new String[] { BasicElementLabels.getJavaElementName(parameter.getName().getIdentifier()), BindingLabelProvider.getBindingLabel(expectedBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS) });
ASTRewrite rewrite = ASTRewrite.create(ast);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.INCOMPATIBLE_FOREACH_TYPE);
ImportRewrite importRewrite = proposal.createImportRewrite(astRoot);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(ASTResolving.findParentBodyDeclaration(selectedNode), importRewrite);
Type newType = importRewrite.addImport(expectedBinding, ast, importRewriteContext, TypeLocation.LOCAL_VARIABLE);
rewrite.replace(parameter.getType(), newType, null);
proposals.add(proposal);
}
use of org.eclipse.jdt.core.dom.EnhancedForStatement in project eclipse.jdt.ls by eclipse.
the class ExtractMethodAnalyzer method getParentLoopBody.
private Statement getParentLoopBody(ASTNode node) {
Statement stmt = null;
ASTNode start = node;
while (start != null && !(start instanceof ForStatement) && !(start instanceof DoStatement) && !(start instanceof WhileStatement) && !(start instanceof EnhancedForStatement) && !(start instanceof SwitchStatement)) {
start = start.getParent();
}
if (start instanceof ForStatement) {
stmt = ((ForStatement) start).getBody();
} else if (start instanceof DoStatement) {
stmt = ((DoStatement) start).getBody();
} else if (start instanceof WhileStatement) {
stmt = ((WhileStatement) start).getBody();
} else if (start instanceof EnhancedForStatement) {
stmt = ((EnhancedForStatement) start).getBody();
}
if (start != null && start.getParent() instanceof LabeledStatement) {
LabeledStatement labeledStatement = (LabeledStatement) start.getParent();
fEnclosingLoopLabel = labeledStatement.getLabel();
}
return stmt;
}
use of org.eclipse.jdt.core.dom.EnhancedForStatement in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method replaceBranches.
private void replaceBranches(final CompilationUnitChange result) {
ASTNode[] selectedNodes = fAnalyzer.getSelectedNodes();
for (int i = 0; i < selectedNodes.length; i++) {
ASTNode astNode = selectedNodes[i];
astNode.accept(new ASTVisitor() {
private LinkedList<String> fOpenLoopLabels = new LinkedList<>();
private void registerLoopLabel(Statement node) {
String identifier;
if (node.getParent() instanceof LabeledStatement) {
LabeledStatement labeledStatement = (LabeledStatement) node.getParent();
identifier = labeledStatement.getLabel().getIdentifier();
} else {
identifier = null;
}
fOpenLoopLabels.add(identifier);
}
@Override
public boolean visit(ForStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(ForStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(WhileStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(WhileStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(EnhancedForStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(EnhancedForStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(DoStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(DoStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public void endVisit(ContinueStatement node) {
final SimpleName label = node.getLabel();
if (fOpenLoopLabels.isEmpty() || (label != null && !fOpenLoopLabels.contains(label.getIdentifier()))) {
TextEditGroup description = new TextEditGroup(RefactoringCoreMessages.ExtractMethodRefactoring_replace_continue);
result.addTextEditGroup(description);
ReturnStatement rs = fAST.newReturnStatement();
IVariableBinding returnValue = fAnalyzer.getReturnValue();
if (returnValue != null) {
rs.setExpression(fAST.newSimpleName(getName(returnValue)));
}
fRewriter.replace(node, rs, description);
}
}
});
}
}
use of org.eclipse.jdt.core.dom.EnhancedForStatement in project whole by wholeplatform.
the class CompilationUnitBuilder method newEnhancedForStatement.
public EnhancedForStatement newEnhancedForStatement(SingleVariableDeclaration parameter, Expression exp, Statement body) {
EnhancedForStatement stm = ast.newEnhancedForStatement();
stm.setParameter(parameter);
stm.setExpression(exp);
stm.setBody(body);
return stm;
}
Aggregations