use of org.eclipse.jdt.internal.corext.refactoring.ParameterInfo in project che 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.internal.corext.refactoring.ParameterInfo in project che by eclipse.
the class ExtractMethodRefactoring method computeLocalTypeVariables.
private ITypeBinding[] computeLocalTypeVariables(int modifier) {
List<ITypeBinding> result = new ArrayList<ITypeBinding>(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.internal.corext.refactoring.ParameterInfo in project che 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.internal.corext.refactoring.ParameterInfo in project che 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 (//$NON-NLS-1$
!"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));
}
return result;
}
use of org.eclipse.jdt.internal.corext.refactoring.ParameterInfo in project che by eclipse.
the class ExtractMethodRefactoring method initializeParameterInfos.
//---- Helper methods ------------------------------------------------------------------------
private void initializeParameterInfos() {
IVariableBinding[] arguments = fAnalyzer.getArguments();
fParameterInfos = new ArrayList<ParameterInfo>(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