]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/TempAssignmentFinder.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / code / TempAssignmentFinder.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
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         //-- visit methods
55
56         @Override
57         public boolean visit(Assignment assignment) {
58                 if (! isAssignmentToTemp(assignment))
59                         return true;
60
61                 fFirstAssignment= assignment;
62                 return false;
63         }
64
65         @Override
66         public boolean visit(PostfixExpression postfixExpression) {
67                 if (postfixExpression.getOperand() == null)
68                         return true;
69                 if (! (postfixExpression.getOperand() instanceof SimpleName))
70                         return true;
71                 SimpleName simpleName= (SimpleName)postfixExpression.getOperand();
72                 if (! isNameReferenceToTemp(simpleName))
73                         return true;
74
75                 fFirstAssignment= postfixExpression;
76                 return false;
77         }
78
79         @Override
80         public boolean visit(PrefixExpression prefixExpression) {
81                 if (prefixExpression.getOperand() == null)
82                         return true;
83                 if (! (prefixExpression.getOperand() instanceof SimpleName))
84                         return true;
85                 if (! prefixExpression.getOperator().equals(Operator.DECREMENT) &&
86                         ! prefixExpression.getOperator().equals(Operator.INCREMENT))
87                         return true;
88                 SimpleName simpleName= (SimpleName)prefixExpression.getOperand();
89                 if (! isNameReferenceToTemp(simpleName))
90                         return true;
91
92                 fFirstAssignment= prefixExpression;
93                 return false;
94         }
95 }