1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.refactoring.code;
13 import org.eclipse.jdt.core.dom.ASTNode;
14 import org.eclipse.jdt.core.dom.ASTVisitor;
15 import org.eclipse.jdt.core.dom.Assignment;
16 import org.eclipse.jdt.core.dom.IVariableBinding;
17 import org.eclipse.jdt.core.dom.Name;
18 import org.eclipse.jdt.core.dom.PostfixExpression;
19 import org.eclipse.jdt.core.dom.PrefixExpression;
20 import org.eclipse.jdt.core.dom.SimpleName;
21 import org.eclipse.jdt.core.dom.VariableDeclaration;
22 import org.eclipse.jdt.core.dom.PrefixExpression.Operator;
24 public class TempAssignmentFinder extends ASTVisitor{
25 private ASTNode fFirstAssignment;
26 private IVariableBinding fTempBinding;
28 TempAssignmentFinder(VariableDeclaration tempDeclaration){
29 fTempBinding= tempDeclaration.resolveBinding();
32 private boolean isNameReferenceToTemp(Name name){
33 return fTempBinding == name.resolveBinding();
36 private boolean isAssignmentToTemp(Assignment assignment){
37 if (fTempBinding == null)
40 if (! (assignment.getLeftHandSide() instanceof Name))
42 Name ref= (Name)assignment.getLeftHandSide();
43 return isNameReferenceToTemp(ref);
46 boolean hasAssignments(){
47 return fFirstAssignment != null;
50 ASTNode getFirstAssignment(){
51 return fFirstAssignment;
57 public boolean visit(Assignment assignment) {
58 if (! isAssignmentToTemp(assignment))
61 fFirstAssignment= assignment;
66 public boolean visit(PostfixExpression postfixExpression) {
67 if (postfixExpression.getOperand() == null)
69 if (! (postfixExpression.getOperand() instanceof SimpleName))
71 SimpleName simpleName= (SimpleName)postfixExpression.getOperand();
72 if (! isNameReferenceToTemp(simpleName))
75 fFirstAssignment= postfixExpression;
80 public boolean visit(PrefixExpression prefixExpression) {
81 if (prefixExpression.getOperand() == null)
83 if (! (prefixExpression.getOperand() instanceof SimpleName))
85 if (! prefixExpression.getOperator().equals(Operator.DECREMENT) &&
86 ! prefixExpression.getOperator().equals(Operator.INCREMENT))
88 SimpleName simpleName= (SimpleName)prefixExpression.getOperand();
89 if (! isNameReferenceToTemp(simpleName))
92 fFirstAssignment= prefixExpression;