use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class ExtractMethodAnalyzer method endVisit.
@Override
public void endVisit(CompilationUnit node) {
RefactoringStatus status = getStatus();
superCall: {
if (status.hasFatalError())
break superCall;
if (!hasSelectedNodes()) {
ASTNode coveringNode = getLastCoveringNode();
if (coveringNode instanceof Block && coveringNode.getParent() instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) coveringNode.getParent();
Message[] messages = ASTNodes.getMessages(methodDecl, ASTNodes.NODE_ONLY);
if (messages.length > 0) {
status.addFatalError(Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors, BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier())), JavaStatusContext.create(fCUnit, methodDecl));
break superCall;
}
}
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
break superCall;
}
fEnclosingBodyDeclaration = (BodyDeclaration) ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
if (fEnclosingBodyDeclaration == null || (fEnclosingBodyDeclaration.getNodeType() != ASTNode.METHOD_DECLARATION && fEnclosingBodyDeclaration.getNodeType() != ASTNode.FIELD_DECLARATION && fEnclosingBodyDeclaration.getNodeType() != ASTNode.INITIALIZER)) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
break superCall;
} else if (ASTNodes.getEnclosingType(fEnclosingBodyDeclaration) == null) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors_no_parent_binding);
break superCall;
} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
fEnclosingMethodBinding = ((MethodDeclaration) fEnclosingBodyDeclaration).resolveBinding();
}
if (!isSingleExpressionOrStatementSet()) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_single_expression_or_set);
break superCall;
}
if (isExpressionSelected()) {
ASTNode expression = getFirstSelectedNode();
if (expression instanceof Name) {
Name name = (Name) expression;
if (name.resolveBinding() instanceof ITypeBinding) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_type_reference);
break superCall;
}
if (name.resolveBinding() instanceof IMethodBinding) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_method_name_reference);
break superCall;
}
if (name.resolveBinding() instanceof IVariableBinding) {
StructuralPropertyDescriptor locationInParent = name.getLocationInParent();
if (locationInParent == QualifiedName.NAME_PROPERTY || (locationInParent == FieldAccess.NAME_PROPERTY && !(((FieldAccess) name.getParent()).getExpression() instanceof ThisExpression))) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name);
break superCall;
}
}
if (name.isSimpleName() && ((SimpleName) name).isDeclaration()) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_name_in_declaration);
break superCall;
}
}
fForceStatic = ASTNodes.getParent(expression, ASTNode.SUPER_CONSTRUCTOR_INVOCATION) != null || ASTNodes.getParent(expression, ASTNode.CONSTRUCTOR_INVOCATION) != null;
}
status.merge(LocalTypeAnalyzer.perform(fEnclosingBodyDeclaration, getSelection()));
computeLastStatementSelected();
}
super.endVisit(node);
}
use of org.eclipse.jdt.core.dom.IVariableBinding 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.core.dom.IVariableBinding 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.core.dom.IVariableBinding 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);
}
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class ExtractMethodAnalyzer method removeSelectedDeclarations.
private IVariableBinding[] removeSelectedDeclarations(IVariableBinding[] bindings) {
List<IVariableBinding> result = new ArrayList<IVariableBinding>(bindings.length);
Selection selection = getSelection();
for (int i = 0; i < bindings.length; i++) {
ASTNode decl = ((CompilationUnit) fEnclosingBodyDeclaration.getRoot()).findDeclaringNode(bindings[i]);
if (!selection.covers(decl))
result.add(bindings[i]);
}
return result.toArray(new IVariableBinding[result.size()]);
}
Aggregations