use of org.eclipse.jdt.core.dom.VariableDeclaration 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.core.dom.VariableDeclaration in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method initializeParameterInfos.
// ---- Helper methods ------------------------------------------------------------------------
private void initializeParameterInfos() {
IVariableBinding[] arguments = fAnalyzer.getArguments();
fParameterInfos = new ArrayList<>(arguments.length);
ASTNode root = fAnalyzer.getEnclosingBodyDeclaration();
ParameterInfo vararg = null;
for (int i = 0; i < arguments.length; i++) {
IVariableBinding argument = arguments[i];
if (argument == null) {
continue;
}
VariableDeclaration declaration = ASTNodes.findVariableDeclaration(argument, root);
boolean isVarargs = declaration instanceof SingleVariableDeclaration ? ((SingleVariableDeclaration) declaration).isVarargs() : false;
ParameterInfo info = new ParameterInfo(argument, getType(declaration, isVarargs), argument.getName(), i);
if (isVarargs) {
vararg = info;
} else {
fParameterInfos.add(info);
}
}
if (vararg != null) {
fParameterInfos.add(vararg);
}
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method createDeclaration.
private VariableDeclarationStatement createDeclaration(IVariableBinding binding, Expression intilizer) {
VariableDeclaration original = ASTNodes.findVariableDeclaration(binding, fAnalyzer.getEnclosingBodyDeclaration());
VariableDeclarationFragment fragment = fAST.newVariableDeclarationFragment();
fragment.setName((SimpleName) ASTNode.copySubtree(fAST, original.getName()));
fragment.setInitializer(intilizer);
VariableDeclarationStatement result = fAST.newVariableDeclarationStatement(fragment);
result.modifiers().addAll(ASTNode.copySubtrees(fAST, ASTNodes.getModifiers(original)));
result.setType(ASTNodeFactory.newType(fAST, original, fImportRewriter, new ContextSensitiveImportRewriteContext(original, fImportRewriter)));
return result;
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method createNewMethodDeclaration.
private MethodDeclaration createNewMethodDeclaration() {
MethodDeclaration result = fAST.newMethodDeclaration();
int modifiers = fVisibility;
BodyDeclaration enclosingBodyDeclaration = fAnalyzer.getEnclosingBodyDeclaration();
boolean isDestinationInterface = isDestinationInterface();
if (isDestinationInterface && !(enclosingBodyDeclaration instanceof MethodDeclaration && enclosingBodyDeclaration.getParent() == fDestination && Modifier.isPublic(enclosingBodyDeclaration.getModifiers()))) {
modifiers = Modifier.NONE;
}
boolean shouldBeStatic = false;
ASTNode currentParent = enclosingBodyDeclaration;
do {
if (currentParent instanceof BodyDeclaration) {
shouldBeStatic = shouldBeStatic || JdtFlags.isStatic((BodyDeclaration) currentParent);
}
currentParent = currentParent.getParent();
} while (!shouldBeStatic && currentParent != null && currentParent != fDestination);
if (shouldBeStatic || fAnalyzer.getForceStatic() || forceStatic()) {
modifiers |= Modifier.STATIC;
} else if (isDestinationInterface) {
modifiers |= Modifier.DEFAULT;
}
ITypeBinding[] typeVariables = computeLocalTypeVariables(modifiers);
List<TypeParameter> typeParameters = result.typeParameters();
for (int i = 0; i < typeVariables.length; i++) {
TypeParameter parameter = fAST.newTypeParameter();
parameter.setName(fAST.newSimpleName(typeVariables[i].getName()));
ITypeBinding[] bounds = typeVariables[i].getTypeBounds();
for (int j = 0; j < bounds.length; j++) {
if (!"java.lang.Object".equals(bounds[j].getQualifiedName())) {
parameter.typeBounds().add(fImportRewriter.addImport(bounds[j], fAST));
}
}
typeParameters.add(parameter);
}
result.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, modifiers));
result.setReturnType2((Type) ASTNode.copySubtree(fAST, fAnalyzer.getReturnType()));
result.setName(fAST.newSimpleName(fMethodName));
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(enclosingBodyDeclaration, fImportRewriter);
List<SingleVariableDeclaration> parameters = result.parameters();
for (int i = 0; i < fParameterInfos.size(); i++) {
ParameterInfo info = fParameterInfos.get(i);
VariableDeclaration infoDecl = getVariableDeclaration(info);
SingleVariableDeclaration parameter = fAST.newSingleVariableDeclaration();
parameter.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, ASTNodes.getModifiers(infoDecl)));
parameter.setType(ASTNodeFactory.newType(fAST, infoDecl, fImportRewriter, context));
parameter.setName(fAST.newSimpleName(info.getNewName()));
parameter.setVarargs(info.isNewVarargs());
parameters.add(parameter);
}
List<Type> exceptions = result.thrownExceptionTypes();
ITypeBinding[] exceptionTypes = fAnalyzer.getExceptions(fThrowRuntimeExceptions);
for (int i = 0; i < exceptionTypes.length; i++) {
ITypeBinding exceptionType = exceptionTypes[i];
exceptions.add(fImportRewriter.addImport(exceptionType, fAST, context, TypeLocation.EXCEPTION));
}
return result;
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project eclipse.jdt.ls by eclipse.
the class SurroundWithTryCatchRefactoring method getSpecialVariableDeclarationStatements.
private List<ASTNode> getSpecialVariableDeclarationStatements() {
List<ASTNode> result = new ArrayList<>(3);
VariableDeclaration[] locals = fAnalyzer.getAffectedLocals();
for (int i = 0; i < locals.length; i++) {
ASTNode parent = locals[i].getParent();
if (parent instanceof VariableDeclarationStatement && !result.contains(parent)) {
result.add(parent);
}
}
return result;
}
Aggregations