use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class ExtractMethodRefactoring method replaceBranches.
private void replaceBranches(final CompilationUnitChange result) {
ASTNode[] selectedNodes = fAnalyzer.getSelectedNodes();
for (int i = 0; i < selectedNodes.length; i++) {
ASTNode astNode = selectedNodes[i];
astNode.accept(new ASTVisitor() {
private LinkedList<String> fOpenLoopLabels = new LinkedList<String>();
private void registerLoopLabel(Statement node) {
String identifier;
if (node.getParent() instanceof LabeledStatement) {
LabeledStatement labeledStatement = (LabeledStatement) node.getParent();
identifier = labeledStatement.getLabel().getIdentifier();
} else {
identifier = null;
}
fOpenLoopLabels.add(identifier);
}
@Override
public boolean visit(ForStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(ForStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(WhileStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(WhileStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(EnhancedForStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(EnhancedForStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public boolean visit(DoStatement node) {
registerLoopLabel(node);
return super.visit(node);
}
@Override
public void endVisit(DoStatement node) {
fOpenLoopLabels.removeLast();
}
@Override
public void endVisit(ContinueStatement node) {
final SimpleName label = node.getLabel();
if (fOpenLoopLabels.isEmpty() || (label != null && !fOpenLoopLabels.contains(label.getIdentifier()))) {
TextEditGroup description = new TextEditGroup(RefactoringCoreMessages.ExtractMethodRefactoring_replace_continue);
result.addTextEditGroup(description);
ReturnStatement rs = fAST.newReturnStatement();
IVariableBinding returnValue = fAnalyzer.getReturnValue();
if (returnValue != null) {
rs.setExpression(fAST.newSimpleName(getName(returnValue)));
}
fRewriter.replace(node, rs, description);
}
}
});
}
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class ExtractMethodRefactoring method createCallNodes.
//---- Code generation -----------------------------------------------------------------------
private ASTNode[] createCallNodes(SnippetFinder.Match duplicate, int modifiers) {
List<ASTNode> result = new ArrayList<ASTNode>(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.core.dom.IVariableBinding in project che by eclipse.
the class ExtractTempRefactoring method isReferringToLocalVariableFromFor.
private static boolean isReferringToLocalVariableFromFor(Expression expression) {
ASTNode current = expression;
ASTNode parent = current.getParent();
while (parent != null && !(parent instanceof BodyDeclaration)) {
if (parent instanceof ForStatement) {
ForStatement forStmt = (ForStatement) parent;
if (forStmt.initializers().contains(current) || forStmt.updaters().contains(current) || forStmt.getExpression() == current) {
List<Expression> initializers = forStmt.initializers();
if (initializers.size() == 1 && initializers.get(0) instanceof VariableDeclarationExpression) {
List<IVariableBinding> forInitializerVariables = getForInitializedVariables((VariableDeclarationExpression) initializers.get(0));
ForStatementChecker checker = new ForStatementChecker(forInitializerVariables);
expression.accept(checker);
if (checker.isReferringToForVariable())
return true;
}
}
}
current = parent;
parent = current.getParent();
}
return false;
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class ExtractTempRefactoring method getForInitializedVariables.
// return List<IVariableBinding>
private static List<IVariableBinding> getForInitializedVariables(VariableDeclarationExpression variableDeclarations) {
List<IVariableBinding> forInitializerVariables = new ArrayList<IVariableBinding>(1);
for (Iterator<VariableDeclarationFragment> iter = variableDeclarations.fragments().iterator(); iter.hasNext(); ) {
VariableDeclarationFragment fragment = iter.next();
IVariableBinding binding = fragment.resolveBinding();
if (binding != null)
forInitializerVariables.add(binding);
}
return forInitializerVariables;
}
use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.
the class SourceAnalyzer method checkActivation.
public RefactoringStatus checkActivation() throws JavaModelException {
RefactoringStatus result = new RefactoringStatus();
if (!fTypeRoot.isStructureKnown()) {
result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_syntax_errors, JavaStatusContext.create(fTypeRoot));
return result;
}
IProblem[] problems = ASTNodes.getProblems(fDeclaration, ASTNodes.NODE_ONLY, ASTNodes.ERROR);
if (problems.length > 0) {
result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_declaration_has_errors, JavaStatusContext.create(fTypeRoot, fDeclaration));
return result;
}
final IMethodBinding declarationBinding = fDeclaration.resolveBinding();
if (declarationBinding != null) {
final int modifiers = declarationBinding.getModifiers();
if (Modifier.isAbstract(modifiers)) {
result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_abstract_methods, JavaStatusContext.create(fTypeRoot, fDeclaration));
return result;
} else if (Modifier.isNative(modifiers)) {
result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_native_methods, JavaStatusContext.create(fTypeRoot, fDeclaration));
return result;
}
} else {
result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_methoddeclaration_has_errors, JavaStatusContext.create(fTypeRoot));
return result;
}
ActivationAnalyzer analyzer = new ActivationAnalyzer();
fDeclaration.accept(analyzer);
result.merge(analyzer.status);
if (!result.hasFatalError()) {
List<SingleVariableDeclaration> parameters = fDeclaration.parameters();
fParameters = new HashMap<IVariableBinding, ParameterData>(parameters.size() * 2);
for (Iterator<SingleVariableDeclaration> iter = parameters.iterator(); iter.hasNext(); ) {
SingleVariableDeclaration element = iter.next();
IVariableBinding binding = element.resolveBinding();
if (binding == null) {
result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_declaration_has_errors, JavaStatusContext.create(fTypeRoot, fDeclaration));
return result;
}
fParameters.put(binding, (ParameterData) element.getProperty(ParameterData.PROPERTY));
}
fNames = new HashMap<IBinding, NameData>();
fImplicitReceivers = new ArrayList<Expression>(2);
fTypeParameterReferences = new ArrayList<NameData>(0);
fTypeParameterMapping = new HashMap<ITypeBinding, NameData>();
ITypeBinding declaringType = declarationBinding.getDeclaringClass();
if (declaringType == null) {
result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_typedeclaration_has_errors, JavaStatusContext.create(fTypeRoot));
return result;
}
ITypeBinding[] typeParameters = declaringType.getTypeParameters();
for (int i = 0; i < typeParameters.length; i++) {
NameData data = new NameData(typeParameters[i].getName());
fTypeParameterReferences.add(data);
fTypeParameterMapping.put(typeParameters[i], data);
}
fMethodTypeParameterReferences = new ArrayList<NameData>(0);
fMethodTypeParameterMapping = new HashMap<ITypeBinding, NameData>();
IMethodBinding method = declarationBinding;
typeParameters = method.getTypeParameters();
for (int i = 0; i < typeParameters.length; i++) {
NameData data = new NameData(typeParameters[i].getName());
fMethodTypeParameterReferences.add(data);
fMethodTypeParameterMapping.put(typeParameters[i], data);
}
}
if (fDeclaration.isVarargs()) {
List<SingleVariableDeclaration> parameters = fDeclaration.parameters();
VarargAnalyzer vAnalyzer = new VarargAnalyzer(parameters.get(parameters.size() - 1).getName().resolveBinding());
fDeclaration.getBody().accept(vAnalyzer);
}
return result;
}
Aggregations