1 11 package org.eclipse.jdt.internal.corext.refactoring.code; 12 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; 23 24 public class TempAssignmentFinder extends ASTVisitor{ 25 private ASTNode fFirstAssignment; 26 private IVariableBinding fTempBinding; 27 28 TempAssignmentFinder(VariableDeclaration tempDeclaration){ 29 fTempBinding= tempDeclaration.resolveBinding(); 30 } 31 32 private boolean isNameReferenceToTemp(Name name){ 33 return fTempBinding == name.resolveBinding(); 34 } 35 36 private boolean isAssignmentToTemp(Assignment assignment){ 37 if (fTempBinding == null) 38 return false; 39 40 if (! (assignment.getLeftHandSide() instanceof Name)) 41 return false; 42 Name ref= (Name)assignment.getLeftHandSide(); 43 return isNameReferenceToTemp(ref); 44 } 45 46 boolean hasAssignments(){ 47 return fFirstAssignment != null; 48 } 49 50 ASTNode getFirstAssignment(){ 51 return fFirstAssignment; 52 } 53 54 56 public boolean visit(Assignment assignment) { 57 if (! isAssignmentToTemp(assignment)) 58 return true; 59 60 fFirstAssignment= assignment; 61 return false; 62 } 63 64 public boolean visit(PostfixExpression postfixExpression) { 65 if (postfixExpression.getOperand() == null) 66 return true; 67 if (! (postfixExpression.getOperand() instanceof SimpleName)) 68 return true; 69 SimpleName simpleName= (SimpleName)postfixExpression.getOperand(); 70 if (! isNameReferenceToTemp(simpleName)) 71 return true; 72 73 fFirstAssignment= postfixExpression; 74 return false; 75 } 76 77 public boolean visit(PrefixExpression prefixExpression) { 78 if (prefixExpression.getOperand() == null) 79 return true; 80 if (! (prefixExpression.getOperand() instanceof SimpleName)) 81 return true; 82 if (! prefixExpression.getOperator().equals(Operator.DECREMENT) && 83 ! prefixExpression.getOperator().equals(Operator.INCREMENT)) 84 return true; 85 SimpleName simpleName= (SimpleName)prefixExpression.getOperand(); 86 if (! isNameReferenceToTemp(simpleName)) 87 return true; 88 89 fFirstAssignment= prefixExpression; 90 return false; 91 } 92 } 93 | Popular Tags |