1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.themis.lethe.analysis;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.themis.exc.ThemisDataException;
21
22 import java.util.ArrayDeque;
23 import java.util.Deque;
24
25
26
27
28 public class ThemisAnalysisMethod
29 implements ThemisAnalysisContainer {
30
31
32
33 private final String theName;
34
35
36
37
38 private final ThemisAnalysisReference theReference;
39
40
41
42
43 private ThemisAnalysisContainer theParent;
44
45
46
47
48 private final ThemisAnalysisProperties theProperties;
49
50
51
52
53 private final Deque<ThemisAnalysisElement> theContents;
54
55
56
57
58 private ThemisAnalysisDataMap theDataMap;
59
60
61
62
63 private final ThemisAnalysisParameters theParameters;
64
65
66
67
68 private final int theNumLines;
69
70
71
72
73 private final boolean isInitializer;
74
75
76
77
78
79
80
81
82
83
84 ThemisAnalysisMethod(final ThemisAnalysisParser pParser,
85 final String pName,
86 final ThemisAnalysisReference pReference,
87 final ThemisAnalysisLine pLine) throws OceanusException {
88
89 isInitializer = pName.length() == 0;
90 theName = isInitializer ? pReference.toString() : pName;
91 theReference = pReference;
92 theProperties = pLine.getProperties();
93
94
95 theParent = pParser.getParent();
96 theDataMap = new ThemisAnalysisDataMap(theParent.getDataMap());
97
98
99 final boolean isInterface = theParent instanceof ThemisAnalysisInterface;
100 final boolean markedDefault = theProperties.hasModifier(ThemisAnalysisModifier.DEFAULT);
101 final boolean markedStatic = theProperties.hasModifier(ThemisAnalysisModifier.STATIC);
102 final boolean markedAbstract = theProperties.hasModifier(ThemisAnalysisModifier.ABSTRACT);
103 final boolean isAbstract = markedAbstract || (isInterface && !markedDefault && !markedStatic);
104
105
106 theContents = new ArrayDeque<>();
107 final Deque<ThemisAnalysisElement> myHeaders = isAbstract
108 ? ThemisAnalysisBuilder.parseTrailers(pParser, pLine)
109 : parseHeaders(pParser, pLine);
110 theNumLines = myHeaders.size() + (isAbstract ? 0 : 1);
111
112
113 final ThemisAnalysisParser myParser = new ThemisAnalysisParser(myHeaders, theContents, this);
114
115
116 theProperties.resolveGeneric(myParser);
117 theReference.resolveGeneric(myParser);
118
119
120 theParameters = new ThemisAnalysisParameters(myParser, myHeaders);
121
122
123 postProcessLines();
124 }
125
126
127
128
129
130
131
132
133
134
135 ThemisAnalysisMethod(final ThemisAnalysisParser pParser,
136 final String pName,
137 final ThemisAnalysisReference pReference,
138 final ThemisAnalysisMethodBody pBody) throws OceanusException {
139
140 final ThemisAnalysisLine myLine = pBody.getHeader();
141 isInitializer = pName.length() == 0;
142 theName = isInitializer ? pReference.toString() : pName;
143 theReference = pReference;
144 theProperties = myLine.getProperties();
145
146
147 theParent = pBody.getParent();
148 theDataMap = pBody.getDataMap();
149
150
151 final Deque<ThemisAnalysisElement> myHeaders = new ArrayDeque<>();
152 myHeaders.add(myLine);
153 theNumLines = 2;
154
155
156 theContents = pBody.getContents();
157
158
159 final ThemisAnalysisParser myParser = new ThemisAnalysisParser(myHeaders, theContents, this);
160
161
162 theProperties.resolveGeneric(myParser);
163 theReference.resolveGeneric(myParser);
164
165
166 theParameters = new ThemisAnalysisParameters(myParser, myHeaders);
167
168
169 postProcessLines();
170 }
171
172
173
174
175
176
177
178
179
180 private Deque<ThemisAnalysisElement> parseHeaders(final ThemisAnalysisParser pParser,
181 final ThemisAnalysisLine pLine) throws OceanusException {
182
183 final Deque<ThemisAnalysisElement> myHeaders = new ArrayDeque<>();
184 ThemisAnalysisElement myElement = pLine;
185
186
187 while (myElement instanceof ThemisAnalysisLine) {
188 myHeaders.add(myElement);
189 myElement = pParser.popNextLine();
190 }
191
192
193 if (!(myElement instanceof ThemisAnalysisMethodBody)) {
194 throw new ThemisDataException("Unexpected dataType");
195 }
196
197
198 final ThemisAnalysisMethodBody myBody = (ThemisAnalysisMethodBody) myElement;
199 myHeaders.add(myBody.getHeader());
200 theContents.addAll(myBody.getContents());
201 theParent = myBody.getParent();
202 theDataMap = myBody.getDataMap();
203
204
205 return myHeaders;
206 }
207
208
209
210
211
212
213 public String getName() {
214 return theName;
215 }
216
217 @Override
218 public Deque<ThemisAnalysisElement> getContents() {
219 return theContents;
220 }
221
222 @Override
223 public ThemisAnalysisContainer getParent() {
224 return this;
225 }
226
227 @Override
228 public ThemisAnalysisDataMap getDataMap() {
229 return theDataMap;
230 }
231
232 @Override
233 public int getNumLines() {
234 return theNumLines;
235 }
236
237 @Override
238 public String toString() {
239 return isInitializer
240 ? getName() + theParameters.toString()
241 : theReference.toString() + " " + getName() + theParameters.toString();
242 }
243 }