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.xanalysis.parser.base;
18  
19  import com.github.javaparser.ast.Node;
20  import com.github.javaparser.ast.NodeList;
21  import com.github.javaparser.ast.PackageDeclaration;
22  import com.github.javaparser.ast.body.BodyDeclaration;
23  import com.github.javaparser.ast.expr.Expression;
24  import com.github.javaparser.ast.modules.ModuleDirective;
25  import com.github.javaparser.ast.stmt.Statement;
26  import com.github.javaparser.ast.type.Type;
27  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
28  import io.github.tonywasher.joceanus.themis.xanalysis.parser.base.ThemisXAnalysisInstance.ThemisXAnalysisClassInstance;
29  import io.github.tonywasher.joceanus.themis.xanalysis.parser.base.ThemisXAnalysisInstance.ThemisXAnalysisDeclarationInstance;
30  import io.github.tonywasher.joceanus.themis.xanalysis.parser.base.ThemisXAnalysisInstance.ThemisXAnalysisExpressionInstance;
31  import io.github.tonywasher.joceanus.themis.xanalysis.parser.base.ThemisXAnalysisInstance.ThemisXAnalysisModuleInstance;
32  import io.github.tonywasher.joceanus.themis.xanalysis.parser.base.ThemisXAnalysisInstance.ThemisXAnalysisNodeInstance;
33  import io.github.tonywasher.joceanus.themis.xanalysis.parser.base.ThemisXAnalysisInstance.ThemisXAnalysisStatementInstance;
34  import io.github.tonywasher.joceanus.themis.xanalysis.parser.base.ThemisXAnalysisInstance.ThemisXAnalysisTypeInstance;
35  
36  import java.io.File;
37  import java.util.ArrayList;
38  import java.util.Collections;
39  import java.util.List;
40  
41  /**
42   * Parser interface.
43   */
44  public interface ThemisXAnalysisParserDef {
45      /**
46       * Process the file as javaCode.
47       *
48       * @return the parsed compilation unit
49       * @throws OceanusException on error
50       */
51      ThemisXAnalysisNodeInstance parseJavaFile() throws OceanusException;
52  
53      /**
54       * Process the file as a module-info instance.
55       *
56       * @param pInfoFile the module-info file
57       * @return the parsed moduleInfo
58       * @throws OceanusException on error
59       */
60      ThemisXAnalysisModuleInstance parseModuleInfo(File pInfoFile) throws OceanusException;
61  
62      /**
63       * Set the current package.
64       *
65       * @param pPackage the package
66       */
67      void setCurrentPackage(String pPackage);
68  
69      /**
70       * Set the current file.
71       *
72       * @param pFile the file
73       */
74      void setCurrentFile(File pFile);
75  
76      /**
77       * Obtain the classList.
78       *
79       * @return the classList
80       */
81      List<ThemisXAnalysisClassInstance> getClasses();
82  
83      /**
84       * Build exception.
85       *
86       * @param pMessage the message
87       * @param pNode    the failing node
88       * @return the built exception
89       */
90      OceanusException buildException(String pMessage,
91                                      Node pNode);
92  
93      /**
94       * Check the package name.
95       *
96       * @param pPackage the package name
97       * @throws OceanusException on error
98       */
99      void checkPackage(PackageDeclaration pPackage) throws OceanusException;
100 
101     /**
102      * Register Instance.
103      *
104      * @param pInstance the instance
105      * @return the parent node
106      * @throws OceanusException on error
107      */
108     ThemisXAnalysisInstance registerInstance(ThemisXAnalysisInstance pInstance) throws OceanusException;
109 
110     /**
111      * Register Class.
112      *
113      * @param pClass the class
114      * @return the class name
115      */
116     String registerClass(ThemisXAnalysisClassInstance pClass) throws OceanusException;
117 
118     /**
119      * Parse a declaration.
120      *
121      * @param pDecl the declaration
122      * @return the parsed declaration
123      * @throws OceanusException on error
124      */
125     ThemisXAnalysisDeclarationInstance parseDeclaration(BodyDeclaration<?> pDecl) throws OceanusException;
126 
127     /**
128      * parse a list of declarations.
129      *
130      * @param pDeclList the list of Declarations
131      * @return the list of parsed declarations
132      * @throws OceanusException on error
133      */
134     default List<ThemisXAnalysisDeclarationInstance> parseDeclarationList(final NodeList<? extends BodyDeclaration<?>> pDeclList) throws OceanusException {
135         /* Handle null list */
136         if (pDeclList == null) {
137             return Collections.emptyList();
138         }
139 
140         /* Create list of declarations */
141         final List<ThemisXAnalysisDeclarationInstance> myList = new ArrayList<>();
142         for (BodyDeclaration<?> myDecl : pDeclList) {
143             final ThemisXAnalysisDeclarationInstance myParsed = parseDeclaration(myDecl);
144             myList.add(myParsed);
145         }
146         return myList;
147     }
148 
149     /**
150      * Parse a node.
151      *
152      * @param pNode the node
153      * @return the parsed node
154      * @throws OceanusException on error
155      */
156     ThemisXAnalysisNodeInstance parseNode(Node pNode) throws OceanusException;
157 
158     /**
159      * parse a list of nodes.
160      *
161      * @param pNodeList the list of Nodes
162      * @return the list of parsed nodes
163      * @throws OceanusException on error
164      */
165     default List<ThemisXAnalysisNodeInstance> parseNodeList(final NodeList<? extends Node> pNodeList) throws OceanusException {
166         /* Handle null list */
167         if (pNodeList == null) {
168             return Collections.emptyList();
169         }
170 
171         /* Create list of nodes */
172         final List<ThemisXAnalysisNodeInstance> myList = new ArrayList<>();
173         for (Node myNode : pNodeList) {
174             final ThemisXAnalysisNodeInstance myParsed = parseNode(myNode);
175             myList.add(myParsed);
176         }
177         return myList;
178     }
179 
180     /**
181      * parse a list of modifiers.
182      *
183      * @param pNodeList the list of Modifiers
184      * @return the list of parsed modifiers
185      * @throws OceanusException on error
186      */
187     ThemisXAnalysisModifierList parseModifierList(NodeList<? extends Node> pNodeList) throws OceanusException;
188 
189     /**
190      * Parse a type.
191      *
192      * @param pType the type
193      * @return the parsed type
194      * @throws OceanusException on error
195      */
196     ThemisXAnalysisTypeInstance parseType(Type pType) throws OceanusException;
197 
198     /**
199      * parse a list of types.
200      *
201      * @param pTypeList the list of Types
202      * @return the list of parsed types
203      * @throws OceanusException on error
204      */
205     default List<ThemisXAnalysisTypeInstance> parseTypeList(final NodeList<? extends Type> pTypeList) throws OceanusException {
206         /* Handle null list */
207         if (pTypeList == null) {
208             return Collections.emptyList();
209         }
210 
211         /* Create list of nodes */
212         final List<ThemisXAnalysisTypeInstance> myList = new ArrayList<>();
213         for (Type myType : pTypeList) {
214             final ThemisXAnalysisTypeInstance myParsed = parseType(myType);
215             myList.add(myParsed);
216         }
217         return myList;
218     }
219 
220     /**
221      * Parse a statement.
222      *
223      * @param pStatement the statement
224      * @return the parsed statement
225      * @throws OceanusException on error
226      */
227     ThemisXAnalysisStatementInstance parseStatement(Statement pStatement) throws OceanusException;
228 
229     /**
230      * parse a list of statements.
231      *
232      * @param pStatementList the list of Statements
233      * @return the list of parsed statements
234      * @throws OceanusException on error
235      */
236     default List<ThemisXAnalysisStatementInstance> parseStatementList(final NodeList<? extends Statement> pStatementList) throws OceanusException {
237         /* Handle null list */
238         if (pStatementList == null) {
239             return Collections.emptyList();
240         }
241 
242         /* Create list of statements */
243         final List<ThemisXAnalysisStatementInstance> myList = new ArrayList<>();
244         for (Statement myStatement : pStatementList) {
245             final ThemisXAnalysisStatementInstance myParsed = parseStatement(myStatement);
246             myList.add(myParsed);
247         }
248         return myList;
249     }
250 
251     /**
252      * Parse an expression.
253      *
254      * @param pExpression the expression
255      * @return the parsed expression
256      * @throws OceanusException on error
257      */
258     ThemisXAnalysisExpressionInstance parseExpression(Expression pExpression) throws OceanusException;
259 
260     /**
261      * parse a list of expressions.
262      *
263      * @param pExprList the list of Expressions
264      * @return the list of parsed expressions
265      * @throws OceanusException on error
266      */
267     default List<ThemisXAnalysisExpressionInstance> parseExprList(final NodeList<? extends Expression> pExprList) throws OceanusException {
268         /* Handle null list */
269         if (pExprList == null) {
270             return Collections.emptyList();
271         }
272 
273         /* Create list of expressions */
274         final List<ThemisXAnalysisExpressionInstance> myList = new ArrayList<>();
275         for (Expression myExpr : pExprList) {
276             final ThemisXAnalysisExpressionInstance myParsed = parseExpression(myExpr);
277             myList.add(myParsed);
278         }
279         return myList;
280     }
281 
282     /**
283      * Parse a module.
284      *
285      * @param pDeclaration the module declaration/directive
286      * @return the parsed declaration/directive
287      * @throws OceanusException on error
288      */
289     ThemisXAnalysisModuleInstance parseModule(Node pDeclaration) throws OceanusException;
290 
291     /**
292      * parse a list of module directives.
293      *
294      * @param pDirList the list of Directives
295      * @return the list of parsed directives
296      * @throws OceanusException on error
297      */
298     default List<ThemisXAnalysisModuleInstance> parseModuleList(final NodeList<? extends ModuleDirective> pDirList) throws OceanusException {
299         /* Handle null list */
300         if (pDirList == null) {
301             return Collections.emptyList();
302         }
303 
304         /* Create list of directives */
305         final List<ThemisXAnalysisModuleInstance> myList = new ArrayList<>();
306         for (ModuleDirective myDir : pDirList) {
307             final ThemisXAnalysisModuleInstance myParsed = parseModule(myDir);
308             myList.add(myParsed);
309         }
310         return myList;
311     }
312 }