use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.
the class LinkedNodeFinder method findByProblems.
public static SimpleName[] findByProblems(ASTNode parent, SimpleName nameNode) {
ArrayList<SimpleName> res = new ArrayList<SimpleName>();
ASTNode astRoot = parent.getRoot();
if (!(astRoot instanceof CompilationUnit)) {
return null;
}
IProblem[] problems = ((CompilationUnit) astRoot).getProblems();
int nameNodeKind = getNameNodeProblemKind(problems, nameNode);
if (nameNodeKind == 0) {
// no problem on node
return null;
}
int bodyStart = parent.getStartPosition();
int bodyEnd = bodyStart + parent.getLength();
String name = nameNode.getIdentifier();
for (int i = 0; i < problems.length; i++) {
IProblem curr = problems[i];
int probStart = curr.getSourceStart();
int probEnd = curr.getSourceEnd() + 1;
if (probStart > bodyStart && probEnd < bodyEnd) {
int currKind = getProblemKind(curr);
if ((nameNodeKind & currKind) != 0) {
ASTNode node = NodeFinder.perform(parent, probStart, (probEnd - probStart));
if (node instanceof SimpleName && name.equals(((SimpleName) node).getIdentifier())) {
res.add((SimpleName) node);
}
}
}
}
return res.toArray(new SimpleName[res.size()]);
}
use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.
the class RenameAnalyzeUtil method analyzeLocalRenames.
/**
* This method analyzes a set of local variable renames inside one cu. It checks whether
* any new compile errors have been introduced by the rename(s) and whether the correct
* node(s) has/have been renamed.
*
* @param analyzePackages the LocalAnalyzePackages containing the information about the local renames
* @param cuChange the TextChange containing all local variable changes to be applied.
* @param oldCUNode the fully (incl. bindings) resolved AST node of the original compilation unit
* @param recovery whether statements and bindings recovery should be performed when parsing the changed CU
* @return a RefactoringStatus containing errors if compile errors or wrongly renamed nodes are found
* @throws CoreException thrown if there was an error greating the preview content of the change
*/
public static RefactoringStatus analyzeLocalRenames(LocalAnalyzePackage[] analyzePackages, TextChange cuChange, CompilationUnit oldCUNode, boolean recovery) throws CoreException {
RefactoringStatus result = new RefactoringStatus();
ICompilationUnit compilationUnit = (ICompilationUnit) oldCUNode.getJavaElement();
String newCuSource = cuChange.getPreviewContent(new NullProgressMonitor());
CompilationUnit newCUNode = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(newCuSource, compilationUnit, true, recovery, null);
result.merge(analyzeCompileErrors(newCuSource, newCUNode, oldCUNode));
if (result.hasError())
return result;
for (int i = 0; i < analyzePackages.length; i++) {
ASTNode enclosing = getEnclosingBlockOrMethodOrLambda(analyzePackages[i].fDeclarationEdit, cuChange, newCUNode);
// get new declaration
IRegion newRegion = RefactoringAnalyzeUtil.getNewTextRange(analyzePackages[i].fDeclarationEdit, cuChange);
ASTNode newDeclaration = NodeFinder.perform(newCUNode, newRegion.getOffset(), newRegion.getLength());
Assert.isTrue(newDeclaration instanceof Name);
VariableDeclaration declaration = getVariableDeclaration((Name) newDeclaration);
Assert.isNotNull(declaration);
SimpleName[] problemNodes = ProblemNodeFinder.getProblemNodes(enclosing, declaration, analyzePackages[i].fOccurenceEdits, cuChange);
result.merge(RefactoringAnalyzeUtil.reportProblemNodes(newCuSource, problemNodes));
}
return result;
}
use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.
the class UnresolvedElementsSubProcessor method getExpressionBaseName.
private static String getExpressionBaseName(Expression expr) {
IBinding argBinding = Bindings.resolveExpressionBinding(expr, true);
if (argBinding instanceof IVariableBinding) {
IJavaProject project = null;
ASTNode root = expr.getRoot();
if (root instanceof CompilationUnit) {
ITypeRoot typeRoot = ((CompilationUnit) root).getTypeRoot();
if (typeRoot != null)
project = typeRoot.getJavaProject();
}
return StubUtility.getBaseName((IVariableBinding) argBinding, project);
}
if (expr instanceof SimpleName)
return ((SimpleName) expr).getIdentifier();
return null;
}
use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.
the class UnresolvedElementsSubProcessor method getArrayAccessProposals.
public static void getArrayAccessProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
CompilationUnit root = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(root);
if (!(selectedNode instanceof MethodInvocation)) {
return;
}
MethodInvocation decl = (MethodInvocation) selectedNode;
SimpleName nameNode = decl.getName();
String methodName = nameNode.getIdentifier();
IBinding[] bindings = (new ScopeAnalyzer(root)).getDeclarationsInScope(nameNode, ScopeAnalyzer.METHODS);
for (int i = 0; i < bindings.length; i++) {
String currName = bindings[i].getName();
if (NameMatcher.isSimilarName(methodName, currName)) {
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_arraychangetomethod_description, BasicElementLabels.getJavaElementName(currName));
proposals.add(new RenameNodeCorrectionProposal(label, context.getCompilationUnit(), nameNode.getStartPosition(), nameNode.getLength(), currName, IProposalRelevance.ARRAY_CHANGE_TO_METHOD));
}
}
// always suggest 'length'
//$NON-NLS-1$
String lengthId = "length";
String label = CorrectionMessages.UnresolvedElementsSubProcessor_arraychangetolength_description;
int offset = nameNode.getStartPosition();
int length = decl.getStartPosition() + decl.getLength() - offset;
proposals.add(new RenameNodeCorrectionProposal(label, context.getCompilationUnit(), offset, length, lengthId, IProposalRelevance.ARRAY_CHANGE_TO_LENGTH));
}
use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.
the class UnresolvedElementsSubProcessor method addEnhancedForWithoutTypeProposals.
private static void addEnhancedForWithoutTypeProposals(ICompilationUnit cu, ASTNode selectedNode, Collection<ICommandAccess> proposals) {
if (selectedNode instanceof SimpleName && (selectedNode.getLocationInParent() == SimpleType.NAME_PROPERTY || selectedNode.getLocationInParent() == NameQualifiedType.NAME_PROPERTY)) {
ASTNode type = selectedNode.getParent();
if (type.getLocationInParent() == SingleVariableDeclaration.TYPE_PROPERTY) {
SingleVariableDeclaration svd = (SingleVariableDeclaration) type.getParent();
if (svd.getLocationInParent() == EnhancedForStatement.PARAMETER_PROPERTY) {
if (svd.getName().getLength() == 0) {
SimpleName simpleName = (SimpleName) selectedNode;
String name = simpleName.getIdentifier();
int relevance = StubUtility.hasLocalVariableName(cu.getJavaProject(), name) ? 10 : 7;
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_create_loop_variable_description, BasicElementLabels.getJavaElementName(name));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
proposals.add(new NewVariableCorrectionProposal(label, cu, NewVariableCorrectionProposal.LOCAL, simpleName, null, relevance, image));
}
}
}
}
}
Aggregations