use of org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method initializeUsedNames.
private void initializeUsedNames() {
fUsedNames = UsedNamesCollector.perform(fAnalyzer.getSelectedNodes());
for (Iterator<ParameterInfo> iter = fParameterInfos.iterator(); iter.hasNext(); ) {
ParameterInfo parameter = iter.next();
fUsedNames.remove(parameter.getOldName());
}
}
use of org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method createCallNodes.
// ---- Code generation -----------------------------------------------------------------------
private ASTNode[] createCallNodes(SnippetFinder.Match duplicate, int modifiers) {
List<ASTNode> result = new ArrayList<>(2);
IVariableBinding[] locals = fAnalyzer.getCallerLocals();
for (int i = 0; i < locals.length; i++) {
result.add(createDeclaration(locals[i], null));
}
MethodInvocation invocation = fAST.newMethodInvocation();
invocation.setName(fAST.newSimpleName(fMethodName));
ASTNode typeNode = ASTResolving.findParentType(fAnalyzer.getEnclosingBodyDeclaration());
RefactoringStatus status = new RefactoringStatus();
while (fDestination != typeNode) {
fAnalyzer.checkInput(status, fMethodName, typeNode);
if (!status.isOK()) {
SimpleName destinationTypeName = fAST.newSimpleName(ASTNodes.getEnclosingType(fDestination).getName());
if ((modifiers & Modifier.STATIC) == 0) {
ThisExpression thisExpression = fAST.newThisExpression();
thisExpression.setQualifier(destinationTypeName);
invocation.setExpression(thisExpression);
} else {
invocation.setExpression(destinationTypeName);
}
break;
}
typeNode = typeNode.getParent();
}
List<Expression> arguments = invocation.arguments();
for (int i = 0; i < fParameterInfos.size(); i++) {
ParameterInfo parameter = fParameterInfos.get(i);
arguments.add(ASTNodeFactory.newName(fAST, getMappedName(duplicate, parameter)));
}
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
nameGroup.addPosition(fRewriter.track(invocation.getName()), false);
}
ASTNode call;
int returnKind = fAnalyzer.getReturnKind();
switch(returnKind) {
case ExtractMethodAnalyzer.ACCESS_TO_LOCAL:
IVariableBinding binding = fAnalyzer.getReturnLocal();
if (binding != null) {
VariableDeclarationStatement decl = createDeclaration(getMappedBinding(duplicate, binding), invocation);
call = decl;
} else {
Assignment assignment = fAST.newAssignment();
assignment.setLeftHandSide(ASTNodeFactory.newName(fAST, getMappedBinding(duplicate, fAnalyzer.getReturnValue()).getName()));
assignment.setRightHandSide(invocation);
call = assignment;
}
break;
case ExtractMethodAnalyzer.RETURN_STATEMENT_VALUE:
ReturnStatement rs = fAST.newReturnStatement();
rs.setExpression(invocation);
call = rs;
break;
default:
call = invocation;
}
if (call instanceof Expression && !fAnalyzer.isExpressionSelected()) {
call = fAST.newExpressionStatement((Expression) call);
}
result.add(call);
// return;
if (returnKind == ExtractMethodAnalyzer.RETURN_STATEMENT_VOID && !fAnalyzer.isLastStatementSelected()) {
result.add(fAST.newReturnStatement());
}
return result.toArray(new ASTNode[result.size()]);
}
use of org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method computeLocalTypeVariables.
private ITypeBinding[] computeLocalTypeVariables(int modifier) {
List<ITypeBinding> result = new ArrayList<>(Arrays.asList(fAnalyzer.getTypeVariables()));
for (int i = 0; i < fParameterInfos.size(); i++) {
ParameterInfo info = fParameterInfos.get(i);
processVariable(result, info.getOldBinding(), modifier);
}
IVariableBinding[] methodLocals = fAnalyzer.getMethodLocals();
for (int i = 0; i < methodLocals.length; i++) {
processVariable(result, methodLocals[i], modifier);
}
return result.toArray(new ITypeBinding[result.size()]);
}
use of org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo in project eclipse.jdt.ls by eclipse.
the class ExtractMethodRefactoring method createMethodBody.
private Block createMethodBody(ASTNode[] selectedNodes, TextEditGroup substitute, int modifiers) {
Block result = fAST.newBlock();
ListRewrite statements = fRewriter.getListRewrite(result, Block.STATEMENTS_PROPERTY);
// Locals that are not passed as an arguments since the extracted method only
// writes to them
IVariableBinding[] methodLocals = fAnalyzer.getMethodLocals();
for (int i = 0; i < methodLocals.length; i++) {
if (methodLocals[i] != null) {
result.statements().add(createDeclaration(methodLocals[i], null));
}
}
for (Iterator<ParameterInfo> iter = fParameterInfos.iterator(); iter.hasNext(); ) {
ParameterInfo parameter = iter.next();
if (parameter.isRenamed()) {
for (int n = 0; n < selectedNodes.length; n++) {
SimpleName[] oldNames = LinkedNodeFinder.findByBinding(selectedNodes[n], parameter.getOldBinding());
for (int i = 0; i < oldNames.length; i++) {
fRewriter.replace(oldNames[i], fAST.newSimpleName(parameter.getNewName()), null);
}
}
}
}
boolean extractsExpression = fAnalyzer.isExpressionSelected();
ASTNode[] callNodes = createCallNodes(null, modifiers);
ASTNode replacementNode;
if (callNodes.length == 1) {
replacementNode = callNodes[0];
} else {
replacementNode = fRewriter.createGroupNode(callNodes);
}
if (extractsExpression) {
// if we have an expression then only one node is selected.
ITypeBinding binding = fAnalyzer.getExpressionBinding();
if (binding != null && (!binding.isPrimitive() || !"void".equals(binding.getName()))) {
// $NON-NLS-1$
ReturnStatement rs = fAST.newReturnStatement();
rs.setExpression((Expression) fRewriter.createMoveTarget(selectedNodes[0] instanceof ParenthesizedExpression ? ((ParenthesizedExpression) selectedNodes[0]).getExpression() : selectedNodes[0]));
statements.insertLast(rs, null);
} else {
ExpressionStatement st = fAST.newExpressionStatement((Expression) fRewriter.createMoveTarget(selectedNodes[0]));
statements.insertLast(st, null);
}
fRewriter.replace(selectedNodes[0].getParent() instanceof ParenthesizedExpression ? selectedNodes[0].getParent() : selectedNodes[0], replacementNode, substitute);
} else {
if (selectedNodes.length == 1) {
statements.insertLast(fRewriter.createMoveTarget(selectedNodes[0]), substitute);
fRewriter.replace(selectedNodes[0], replacementNode, substitute);
} else {
ListRewrite source = fRewriter.getListRewrite(selectedNodes[0].getParent(), (ChildListPropertyDescriptor) selectedNodes[0].getLocationInParent());
ASTNode toMove = source.createMoveTarget(selectedNodes[0], selectedNodes[selectedNodes.length - 1], replacementNode, substitute);
statements.insertLast(toMove, substitute);
}
IVariableBinding returnValue = fAnalyzer.getReturnValue();
if (returnValue != null) {
ReturnStatement rs = fAST.newReturnStatement();
rs.setExpression(fAST.newSimpleName(getName(returnValue)));
statements.insertLast(rs, null);
}
}
return result;
}
use of org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo 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);
}
}
Aggregations