View Javadoc
1   /*
2    * Themis: Java Project Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.themis.parser.base;
18  
19  import com.github.javaparser.ast.Node;
20  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.function.Predicate;
26  
27  /**
28   * Instance definitions.
29   */
30  public interface ThemisInstance {
31      /**
32       * Obtain the node.
33       *
34       * @return the node
35       */
36      Node getNode();
37  
38      /**
39       * Obtain the id of the node.
40       *
41       * @return the id
42       */
43      ThemisId getId();
44  
45      /**
46       * Register child node.
47       *
48       * @param pChild the child node
49       */
50      void registerChild(ThemisInstance pChild);
51  
52      /**
53       * Obtain the parent.
54       *
55       * @return the parent
56       */
57      ThemisInstance getParent();
58  
59      /**
60       * Obtain the list of children.
61       *
62       * @return the list of children
63       */
64      List<ThemisInstance> getChildren();
65  
66      /**
67       * Select children from tree.
68       *
69       * @param pId the type of node
70       * @return the list of selected children
71       */
72      default List<ThemisInstance> discoverChildren(final ThemisId pId) {
73          return discoverChildren(n -> n.getId().equals(pId));
74      }
75  
76      /**
77       * Select children.
78       *
79       * @param pTest the predicate to select children
80       * @return the list of selected children
81       */
82      List<ThemisInstance> discoverChildren(Predicate<ThemisInstance> pTest);
83  
84      /**
85       * Select nodes from tree.
86       *
87       * @param pList the list to populate
88       * @param pTest the predicate to select nodes
89       */
90      void discoverNodes(List<ThemisInstance> pList,
91                         Predicate<ThemisInstance> pTest);
92  
93      /**
94       * Select nodes from tree.
95       *
96       * @param pId the type of node
97       * @return the list of selected nodes
98       */
99      default List<ThemisInstance> discoverNodes(final ThemisId pId) {
100         return discoverNodes(n -> n.getId().equals(pId));
101     }
102 
103     /**
104      * Select nodes from tree.
105      *
106      * @param pTest the predicate to select nodes
107      * @return the list of selected nodes
108      */
109     default List<ThemisInstance> discoverNodes(final Predicate<ThemisInstance> pTest) {
110         final List<ThemisInstance> myList = new ArrayList<>();
111         discoverNodes(myList, pTest);
112         return myList;
113     }
114 
115     /**
116      * The id.
117      */
118     interface ThemisId {
119     }
120 
121     /**
122      * The base declaration interface.
123      */
124     interface ThemisDeclarationInstance
125             extends ThemisInstance {
126     }
127 
128     /**
129      * The base type interface.
130      */
131     interface ThemisTypeInstance
132             extends ThemisInstance {
133     }
134 
135     /**
136      * The base node interface.
137      */
138     interface ThemisNodeInstance
139             extends ThemisInstance {
140     }
141 
142     /**
143      * The base statement interface.
144      */
145     interface ThemisStatementInstance
146             extends ThemisInstance {
147     }
148 
149     /**
150      * The base expression interface.
151      */
152     interface ThemisExpressionInstance
153             extends ThemisInstance {
154     }
155 
156     /**
157      * The base module interface.
158      */
159     interface ThemisModuleInstance
160             extends ThemisInstance {
161     }
162 
163     /**
164      * The base class interface.
165      */
166     interface ThemisClassInstance
167             extends MetisFieldItem {
168         /**
169          * Obtain the name.
170          *
171          * @return the name
172          */
173         String getName();
174 
175         /**
176          * Obtain the fullName.
177          *
178          * @return the fullName
179          */
180         String getFullName();
181 
182         /**
183          * Obtain the modifiers.
184          *
185          * @return the modifiers
186          */
187         ThemisModifierList getModifiers();
188 
189         /**
190          * Obtain the body.
191          *
192          * @return the body
193          */
194         default List<ThemisDeclarationInstance> getBody() {
195             return Collections.emptyList();
196         }
197 
198         /**
199          * Obtain the extends types.
200          *
201          * @return the extends
202          */
203         default List<ThemisTypeInstance> getExtends() {
204             return Collections.emptyList();
205         }
206 
207         /**
208          * Obtain the implements types.
209          *
210          * @return the implements
211          */
212         default List<ThemisTypeInstance> getImplements() {
213             return Collections.emptyList();
214         }
215 
216         /**
217          * Obtain the type parameters.
218          *
219          * @return the parameters
220          */
221         default List<ThemisTypeInstance> getTypeParameters() {
222             return Collections.emptyList();
223         }
224 
225         /**
226          * Obtain the annotations.
227          *
228          * @return the annotations
229          */
230         default List<ThemisExpressionInstance> getAnnotations() {
231             return Collections.emptyList();
232         }
233 
234         /**
235          * is the class a top-level class?
236          *
237          * @return true/false
238          */
239         default boolean isTopLevel() {
240             return false;
241         }
242 
243         /**
244          * is the class an interface?
245          *
246          * @return true/false
247          */
248         default boolean isInterface() {
249             return false;
250         }
251 
252         /**
253          * is the class an inner class?
254          *
255          * @return true/false
256          */
257         default boolean isInner() {
258             return false;
259         }
260 
261         /**
262          * is the class a local declaration?
263          *
264          * @return true/false
265          */
266         default boolean isLocalDeclaration() {
267             return false;
268         }
269 
270         /**
271          * is the class an anonymous class?
272          *
273          * @return true/false
274          */
275         default boolean isAnonClass() {
276             return false;
277         }
278     }
279 
280     /**
281      * The base method interface.
282      */
283     interface ThemisMethodInstance
284             extends MetisFieldItem {
285         /**
286          * Obtain the name.
287          *
288          * @return the name
289          */
290         String getName();
291 
292         /**
293          * Obtain the modifiers.
294          *
295          * @return the modifiers
296          */
297         ThemisModifierList getModifiers();
298 
299         /**
300          * Obtain the parameters.
301          *
302          * @return the parameters
303          */
304         default List<ThemisNodeInstance> getParameters() {
305             return Collections.emptyList();
306         }
307 
308         /**
309          * Obtain the thrown exceptions.
310          *
311          * @return the thrown exceptions
312          */
313         default List<ThemisTypeInstance> getThrown() {
314             return Collections.emptyList();
315         }
316 
317         /**
318          * Obtain the type parameters.
319          *
320          * @return the parameters
321          */
322         default List<ThemisTypeInstance> getTypeParameters() {
323             return Collections.emptyList();
324         }
325 
326         /**
327          * Obtain the body.
328          *
329          * @return the body
330          */
331         ThemisStatementInstance getBody();
332 
333         /**
334          * Obtain the annotations.
335          *
336          * @return the annotations
337          */
338         default List<ThemisExpressionInstance> getAnnotations() {
339             return Collections.emptyList();
340         }
341     }
342 }