]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/refaktor-after/src/no/uio/ifi/refaktor/analyze/analyzers/SelectionValidator.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / refaktor-after / src / no / uio / ifi / refaktor / analyze / analyzers / SelectionValidator.java
CommitLineData
1b2798f6
EK
1package no.uio.ifi.refaktor.analyze.analyzers;
2
3import java.util.Collections;
4import java.util.Comparator;
5import java.util.LinkedList;
6
7import no.uio.ifi.refaktor.analyze.exceptions.SelectionInvalidException;
8import no.uio.ifi.refaktor.textselection.CompilationUnitTextSelection;
9
10import org.eclipse.jdt.core.dom.ASTNode;
11import org.eclipse.jdt.core.dom.ASTVisitor;
12import org.eclipse.jdt.core.dom.Block;
13import org.eclipse.jdt.core.dom.MethodDeclaration;
14import org.eclipse.jdt.core.dom.Statement;
15
16public class SelectionValidator extends ASTVisitor {
17
18 public final CompilationUnitTextSelection selection;
19 public final LinkedList<ASTNode> selectedStatements;
20
21 public SelectionValidator(CompilationUnitTextSelection selection) {
22 super();
23 this.selection = selection;
24 this.selectedStatements = new LinkedList<ASTNode>();
25 findSelectedStatements();
26 }
27
28 private void findSelectedStatements() {
29 getCoveringNode().accept(this);
30 }
31
32 private ASTNode getCoveringNode() {
33 return selection.getCoveringNode();
34 }
35
36 private ASTNode getCoveredNode() {
37 return selection.getCoveredNode();
38 }
39
40 @Override
41 public boolean preVisit2(ASTNode node) {
42 if (node instanceof Statement && selection.surroundsNode(node)) {
43 selectedStatements.add(node);
44 // Only visit top-level statements
45 return false;
46 }
47
48 if (node instanceof MethodDeclaration || node instanceof Block )
49 return true;
50
51 return false;
52 }
53
54 public static void checkIfSelectionIsValid(CompilationUnitTextSelection selection) {
55 if (!selectionIsValid(selection))
56 throw new SelectionInvalidException();
57 }
58
59 private static boolean selectionIsValid(CompilationUnitTextSelection selection) {
60 return new SelectionValidator(selection).selectionIsValid();
61 }
62
63 private boolean selectionIsValid() {
64 if (noStatementsSelected())
65 return false;
66
67 if (selectedNodeHasBlockStructure())
68 return true;
69
70 return selectionMatchesStatementsStartAndEnd();
71 }
72
73 private boolean noStatementsSelected() {
74 return selectedStatements.isEmpty();
75 }
76
77 private boolean selectedNodeHasBlockStructure() {
78 return isSingleNodeSelection() &&
79 (getSelectedNode() instanceof MethodDeclaration || getSelectedNode() instanceof Block);
80 }
81
82 private boolean selectionMatchesStatementsStartAndEnd() {
83 sortSelectedStatements();
84 return selection.generated_6610636579653443493(this);
85 }
86
87 private void sortSelectedStatements() {
88 Collections.sort(selectedStatements, new Comparator<ASTNode>(){
89 @Override
90 public int compare(ASTNode node1, ASTNode node2) {
91 if (node1.equals(node2))
92 return 0;
93 else if (node1.getStartPosition() < node2.getStartPosition())
94 return -1;
95 else if (node1.getStartPosition() > node2.getStartPosition())
96 return 1;
97 else
98 return 0;
99 }});
100 }
101
102 private boolean isSingleNodeSelection() {
103 return getCoveringNode() == getCoveredNode();
104 }
105
106 private ASTNode getSelectedNode() {
107 if (!isSingleNodeSelection())
108 return null;
109 return getCoveredNode();
110 }
111
112 public int nodeEnd(ASTNode astNode) {
113 return astNode.getStartPosition() + astNode.getLength();
114 }
115}