]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/CompilationUnitTextSelection.java
Making the ExtractAndMoveMethodAnalysisResult lightweight enough for it
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / utils / CompilationUnitTextSelection.java
1 package no.uio.ifi.refaktor.utils;
2
3 import static no.uio.ifi.refaktor.RefaktorAssert.assertThat;
4 import static org.hamcrest.CoreMatchers.instanceOf;
5
6 import org.eclipse.core.resources.IProject;
7 import org.eclipse.jdt.core.ICompilationUnit;
8 import org.eclipse.jdt.core.dom.ASTNode;
9 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
10 import org.eclipse.jdt.core.dom.CompilationUnit;
11 import org.eclipse.jdt.core.dom.NodeFinder;
12 import org.eclipse.jface.text.IDocument;
13 import org.eclipse.jface.text.ITextSelection;
14 import org.eclipse.jface.text.TextSelection;
15
16 /**
17  * A custom TextSelection that enforces the presence of
18  * a compilation unit to back the selection.
19  */
20 public class CompilationUnitTextSelection extends TextSelection implements ITextSelection {
21
22         private class NodeFinderCache {
23                 private NodeFinder nodeFinder;
24
25                 private NodeFinder getNodeFinder() {
26                         if (nodeFinder == null)
27                                 nodeFinder = ParseUtils.createNodeFinder(ParseUtils.parse(compilationUnit), CompilationUnitTextSelection.this);
28                         return nodeFinder;
29                 }
30
31                 public void dropCachedData() {
32                         nodeFinder = null;
33                 }
34         }
35
36         private ICompilationUnit compilationUnit;
37         private final NodeFinderCache nodeFinderCache;
38
39         public CompilationUnitTextSelection(ICompilationUnit compilationUnit, int offset, int length) {
40                 super(offset, length);
41                 if (compilationUnit == null)
42                         throw new NullPointerException(this.getClass().getCanonicalName() + ": given compilation unit cannot be null");
43                 this.compilationUnit = compilationUnit;
44                 nodeFinderCache = new NodeFinderCache();
45         }
46
47         public CompilationUnitTextSelection(ICompilationUnit compilationUnit, ITextSelection selection) {
48                 this(compilationUnit, selection.getOffset(), selection.getLength());
49         }
50
51         @Override
52         public IDocument getDocument() {
53                 return DocumentUtils.getDocumentFromCompilationUnit(compilationUnit);
54         }
55
56         public int getEnd() {
57                 return getOffset() + getLength();
58         }
59
60         public IProject getProject() {
61                 return compilationUnit.getJavaProject().getProject();
62         }
63
64         public ICompilationUnit getCompilationUnit() {
65                 return compilationUnit;
66         }
67
68         public ASTNode getCoveringNode() {
69                 return getNodeFinder().getCoveringNode();
70         }
71
72         public ASTNode getCoveredNode() {
73                 return getNodeFinder().getCoveredNode();
74         }
75
76         private NodeFinder getNodeFinder() {
77                 return nodeFinderCache.getNodeFinder();
78         }
79
80         public boolean surroundsNode(ASTNode node) {
81                 return getOffset() <= node.getStartPosition() && endOfNode(node) <= getEnd();
82         }
83
84         private int endOfNode(ASTNode node) {
85                 return node.getStartPosition() + node.getLength();
86         }
87
88         public String getPackageName() {
89                 return getConcreteCompilationUnit().getPackage().getName().getFullyQualifiedName();
90         }
91
92         public String getNameOfSurroundingType() {
93                 for (Object type: getConcreteCompilationUnit().types()) {
94                         assertThat(type, instanceOf(AbstractTypeDeclaration.class));
95                         AbstractTypeDeclaration absTypeDecl = (AbstractTypeDeclaration) type;
96                         if (isSurroundedBy(absTypeDecl))
97                                 return absTypeDecl.getName().getFullyQualifiedName();
98                 }
99                 return null;
100         }
101
102         private boolean isSurroundedBy(AbstractTypeDeclaration absTypeDecl) {
103                 return absTypeDecl.getStartPosition() <= getOffset() && getLength() <= absTypeDecl.getLength();
104         }
105
106         private CompilationUnit getConcreteCompilationUnit() {
107                 ASTNode root = getCoveringNode().getRoot();
108                 assertThat(root, instanceOf(CompilationUnit.class));
109                 return (CompilationUnit) root;
110         }
111
112         @Override
113         public boolean equals(Object obj) {
114                 if (obj instanceof CompilationUnitTextSelection)
115                         return super.equals(obj) && compilationUnit.equals(((CompilationUnitTextSelection)obj).compilationUnit);
116                 return false;
117         }
118
119         @Override
120         public int hashCode() {
121                 return (getOffset() << 24) | (getLength() << 16) | compilationUnit.hashCode();
122         }
123
124         @Override
125         public String toString() {
126                 return "Offset: " + getOffset() + "; Length: " + getLength() + "; End: " + getEnd() + "; CU handle identifier: " + compilationUnit.getHandleIdentifier();
127         }
128
129         @Override
130         public int getStartLine() {
131                 return createTextSelectionWithDocument().getStartLine();
132         }
133
134         @Override
135         public int getEndLine() {
136                 return createTextSelectionWithDocument().getEndLine();
137         }
138
139         @Override
140         public String getText() {
141                 return createTextSelectionWithDocument().getText();
142         }
143
144         private TextSelection createTextSelectionWithDocument() {
145                 return new TextSelection(getDocument(), getOffset(), getLength());
146         }
147
148         public void dropCachedData() {
149                 nodeFinderCache.dropCachedData();
150         }
151 }