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.lethe.analysis;
18  
19  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20  import io.github.tonywasher.joceanus.themis.lethe.analysis.ThemisAnalysisFile.ThemisAnalysisObject;
21  import io.github.tonywasher.joceanus.themis.lethe.analysis.ThemisAnalysisGeneric.ThemisAnalysisGenericBase;
22  
23  import java.util.ArrayDeque;
24  import java.util.Deque;
25  import java.util.List;
26  
27  /**
28   * Class representation.
29   */
30  public class ThemisAnalysisClass
31          implements ThemisAnalysisObject {
32      /**
33       * The short name of the class.
34       */
35      private final String theShortName;
36  
37      /**
38       * The full name of the class.
39       */
40      private final String theFullName;
41  
42      /**
43       * The ancestors.
44       */
45      private final List<ThemisAnalysisReference> theAncestors;
46  
47      /**
48       * The contents.
49       */
50      private final Deque<ThemisAnalysisElement> theContents;
51  
52      /**
53       * The dataMap.
54       */
55      private final ThemisAnalysisDataMap theDataMap;
56  
57      /**
58       * The number of lines.
59       */
60      private final int theNumLines;
61  
62      /**
63       * The properties.
64       */
65      private ThemisAnalysisProperties theProperties;
66  
67      /**
68       * Constructor.
69       *
70       * @param pParser the parser
71       * @param pLine   the initial class line
72       * @throws OceanusException on error
73       */
74      ThemisAnalysisClass(final ThemisAnalysisParser pParser,
75                          final ThemisAnalysisLine pLine) throws OceanusException {
76          /* Store parameters */
77          theShortName = pLine.stripNextToken();
78          theProperties = pLine.getProperties();
79          final ThemisAnalysisContainer myParent = pParser.getParent();
80          final ThemisAnalysisDataMap myParentDataMap = myParent.getDataMap();
81          theDataMap = new ThemisAnalysisDataMap(myParentDataMap);
82  
83          /* If this is a local class */
84          if (!(myParent instanceof ThemisAnalysisObject)
85                  && (!(myParent instanceof ThemisAnalysisFile))) {
86              final int myId = myParentDataMap.getLocalId(theShortName);
87              theFullName = myParent.determineFullChildName(myId + theShortName);
88  
89              /* else handle standard name */
90          } else {
91              theFullName = myParent.determineFullChildName(theShortName);
92          }
93  
94          /* Handle generic variables */
95          ThemisAnalysisLine myLine = pLine;
96          if (ThemisAnalysisGeneric.isGeneric(pLine)) {
97              /* Declare them to the properties */
98              theProperties = theProperties.setGenericVariables(new ThemisAnalysisGenericBase(pParser, myLine));
99              myLine = (ThemisAnalysisLine) pParser.popNextLine();
100         }
101 
102         /* declare the class */
103         theDataMap.declareObject(this);
104 
105         /* Parse the headers */
106         final Deque<ThemisAnalysisElement> myHeaders = ThemisAnalysisBuilder.parseHeaders(pParser, myLine);
107         theNumLines = myHeaders.size() + 1;
108 
109         /* Parse the body */
110         final Deque<ThemisAnalysisElement> myLines = ThemisAnalysisBuilder.processBody(pParser);
111 
112         /* Create a parser */
113         theContents = new ArrayDeque<>();
114         final ThemisAnalysisParser myParser = new ThemisAnalysisParser(myLines, theContents, this);
115 
116         /* Resolve the generics */
117         theProperties.resolveGeneric(myParser);
118 
119         /* Parse the ancestors and lines */
120         theAncestors = myParser.parseAncestors(myHeaders);
121         myParser.processLines();
122     }
123 
124     @Override
125     public String getShortName() {
126         return theShortName;
127     }
128 
129     @Override
130     public String getFullName() {
131         return theFullName;
132     }
133 
134     @Override
135     public ThemisAnalysisDataMap getDataMap() {
136         return theDataMap;
137     }
138 
139     @Override
140     public ThemisAnalysisProperties getProperties() {
141         return theProperties;
142     }
143 
144     @Override
145     public Deque<ThemisAnalysisElement> getContents() {
146         return theContents;
147     }
148 
149     @Override
150     public ThemisAnalysisContainer getParent() {
151         return this;
152     }
153 
154     @Override
155     public List<ThemisAnalysisReference> getAncestors() {
156         return theAncestors;
157     }
158 
159     @Override
160     public int getNumLines() {
161         return theNumLines;
162     }
163 
164     @Override
165     public String toString() {
166         return getShortName();
167     }
168 }