use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.SnippetFinder.Match in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method findValidDuplicates.
private List<SnippetFinder.Match> findValidDuplicates(ASTNode startNode) {
List<Match> duplicates = SnippetFinder.perform(startNode, fAnalyzer.getSelectedNodes());
List<SnippetFinder.Match> validDuplicates = new ArrayList<>();
for (Match duplicate : duplicates) {
if (duplicate != null && !duplicate.isInvalidNode()) {
try {
ASTNode[] nodes = duplicate.getNodes();
int duplicateStart = nodes[0].getStartPosition();
ASTNode lastNode = nodes[nodes.length - 1];
int duplicateEnd = lastNode.getStartPosition() + lastNode.getLength();
int duplicateLength = duplicateEnd - duplicateStart;
ExtractMethodAnalyzer analyzer = new ExtractMethodAnalyzer(fCUnit, Selection.createFromStartLength(duplicateStart, duplicateLength));
fRoot.accept(analyzer);
RefactoringStatus result = new RefactoringStatus();
result.merge(analyzer.checkInitialConditions(fImportRewriter));
if (!result.hasFatalError()) {
ITypeBinding originalReturnTypeBinding = fAnalyzer.getReturnTypeBinding();
ITypeBinding duplicateReturnTypeBinding = analyzer.getReturnTypeBinding();
if (originalReturnTypeBinding == null && duplicateReturnTypeBinding == null) {
validDuplicates.add(duplicate);
} else if (originalReturnTypeBinding != null && duplicateReturnTypeBinding != null) {
if (!originalReturnTypeBinding.equals(duplicateReturnTypeBinding)) {
if (duplicateReturnTypeBinding.equals(startNode.getAST().resolveWellKnownType("void"))) {
// $NON-NLS-1$
// extracted snippet returns non-void and duplicate snippet returns void => OK
validDuplicates.add(duplicate);
}
} else {
IVariableBinding originalReturnValBinding = fAnalyzer.getReturnValue();
IVariableBinding duplicateReturnValBinding = analyzer.getReturnValue();
if (originalReturnValBinding == null && duplicateReturnValBinding == null) {
validDuplicates.add(duplicate);
} else if (originalReturnValBinding != null && duplicateReturnValBinding != null) {
BodyDeclaration originalEnclosingBodyDeclaration = fAnalyzer.getEnclosingBodyDeclaration();
BodyDeclaration duplicateEnclosingBodyDeclaration = analyzer.getEnclosingBodyDeclaration();
VariableDeclaration originalReturnNode = ASTNodes.findVariableDeclaration(originalReturnValBinding, originalEnclosingBodyDeclaration);
VariableDeclaration duplicateReturnNode = ASTNodes.findVariableDeclaration(duplicateReturnValBinding, duplicateEnclosingBodyDeclaration);
if (originalReturnNode != null && duplicateReturnNode != null) {
boolean matches;
if (!fAnalyzer.getSelection().covers(originalReturnNode) && !analyzer.getSelection().covers(duplicateReturnNode)) {
// returned variables are defined outside of the selection => always OK
matches = true;
} else {
matches = matchesLocationInEnclosingBodyDecl(originalEnclosingBodyDeclaration, duplicateEnclosingBodyDeclaration, originalReturnNode, duplicateReturnNode);
}
if (matches) {
validDuplicates.add(duplicate);
}
}
}
}
}
}
} catch (CoreException e) {
// consider as invalid duplicate
}
}
}
return validDuplicates;
}
use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.SnippetFinder.Match in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method replaceDuplicates.
private void replaceDuplicates(CompilationUnitChange result, int modifiers) {
int numberOf = getNumberOfDuplicates();
if (numberOf == 0 || !fReplaceDuplicates) {
return;
}
String label = null;
if (numberOf == 1) {
label = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_duplicates_single, BasicElementLabels.getJavaElementName(fMethodName));
} else {
label = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_duplicates_multi, BasicElementLabels.getJavaElementName(fMethodName));
}
TextEditGroup description = new TextEditGroup(label);
result.addTextEditGroup(description);
for (Match duplicate : fDuplicates) {
if (!duplicate.isInvalidNode()) {
if (isDestinationReachable(duplicate.getEnclosingMethod())) {
ASTNode[] callNodes = createCallNodes(duplicate, modifiers);
ASTNode[] duplicateNodes = duplicate.getNodes();
for (int i = 0; i < duplicateNodes.length; i++) {
ASTNode parent = duplicateNodes[i].getParent();
if (parent instanceof ParenthesizedExpression) {
duplicateNodes[i] = parent;
}
}
new StatementRewrite(fRewriter, duplicateNodes).replace(callNodes, description);
}
}
}
}
Aggregations