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.node;
18  
19  import com.github.javaparser.ast.CompilationUnit;
20  import com.github.javaparser.ast.NodeList;
21  import com.github.javaparser.ast.body.TypeDeclaration;
22  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
23  import io.github.tonywasher.joceanus.themis.xanalysis.parser.base.ThemisXAnalysisParserDef;
24  
25  import java.util.List;
26  
27  /**
28   * Compilation Unit.
29   */
30  public class ThemisXAnalysisNodeCompilationUnit
31          extends ThemisXAnalysisBaseNode<CompilationUnit> {
32      /**
33       * The Package.
34       */
35      private final ThemisXAnalysisNodeInstance thePackageDef;
36  
37      /**
38       * The Imports.
39       */
40      private final List<ThemisXAnalysisNodeInstance> theImports;
41  
42      /**
43       * The Contents.
44       */
45      private final ThemisXAnalysisClassInstance theContents;
46  
47      /**
48       * Constructor.
49       *
50       * @param pParser the parser
51       * @param pUnit   the unit
52       * @throws OceanusException on error
53       */
54      ThemisXAnalysisNodeCompilationUnit(final ThemisXAnalysisParserDef pParser,
55                                         final CompilationUnit pUnit) throws OceanusException {
56          super(pParser, pUnit);
57          thePackageDef = pParser.parseNode(pUnit.getPackageDeclaration().orElse(null));
58          theImports = pParser.parseNodeList(pUnit.getImports());
59          final NodeList<TypeDeclaration<?>> myTypes = pUnit.getTypes();
60          if (myTypes.size() > 1) {
61              throw pParser.buildException("More than one class definition in file", pUnit);
62          }
63          if (myTypes.isEmpty()) {
64              throw pParser.buildException("No class definition found in file", pUnit);
65          }
66          theContents = (ThemisXAnalysisClassInstance) pParser.parseDeclaration(pUnit.getType(0));
67      }
68  
69      /**
70       * Obtain the package.
71       *
72       * @return the package
73       */
74      public ThemisXAnalysisNodeInstance getPackage() {
75          return thePackageDef;
76      }
77  
78      /**
79       * Obtain the imports.
80       *
81       * @return the imports
82       */
83      public List<ThemisXAnalysisNodeInstance> getImports() {
84          return theImports;
85      }
86  
87      /**
88       * Obtain the contents.
89       *
90       * @return the contents
91       */
92      public ThemisXAnalysisClassInstance getContents() {
93          return theContents;
94      }
95  }