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.decl;
18  
19  import com.github.javaparser.ast.body.BodyDeclaration;
20  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
21  import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisId;
22  import io.github.tonywasher.joceanus.themis.parser.base.ThemisParserDef;
23  
24  import java.util.function.Predicate;
25  
26  /**
27   * Analysis BodyType.
28   */
29  public enum ThemisDeclaration
30          implements ThemisId {
31      /**
32       * Annotation.
33       */
34      ANNOTATION(BodyDeclaration::isAnnotationDeclaration),
35  
36      /**
37       * Annotation Member.
38       */
39      ANNOTATIONMEMBER(BodyDeclaration::isAnnotationMemberDeclaration),
40  
41      /**
42       * Class/Interface.
43       */
44      CLASSINTERFACE(BodyDeclaration::isClassOrInterfaceDeclaration),
45  
46      /**
47       * Compact Constructor.
48       */
49      COMPACT(BodyDeclaration::isCompactConstructorDeclaration),
50  
51      /**
52       * Class.
53       */
54      CONSTRUCTOR(BodyDeclaration::isConstructorDeclaration),
55  
56      /**
57       * Enum.
58       */
59      ENUM(BodyDeclaration::isEnumDeclaration),
60  
61      /**
62       * EnumValue.
63       */
64      ENUMVALUE(BodyDeclaration::isEnumConstantDeclaration),
65  
66      /**
67       * Field.
68       */
69      FIELD(BodyDeclaration::isFieldDeclaration),
70  
71      /**
72       * Initializer.
73       */
74      INITIALIZER(BodyDeclaration::isInitializerDeclaration),
75  
76      /**
77       * Method.
78       */
79      METHOD(BodyDeclaration::isMethodDeclaration),
80  
81      /**
82       * Record.
83       */
84      RECORD(BodyDeclaration::isRecordDeclaration);
85  
86      /**
87       * The test.
88       */
89      private final Predicate<BodyDeclaration<?>> theTester;
90  
91      /**
92       * Constructor.
93       *
94       * @param pTester the test method
95       */
96      ThemisDeclaration(final Predicate<BodyDeclaration<?>> pTester) {
97          theTester = pTester;
98      }
99  
100     /**
101      * Determine type of Declaration.
102      *
103      * @param pParser the parser
104      * @param pDecl   the declaration
105      * @return the declType
106      * @throws OceanusException on error
107      */
108     public static ThemisDeclaration determineDeclaration(final ThemisParserDef pParser,
109                                                          final BodyDeclaration<?> pDecl) throws OceanusException {
110         /* Loop testing each declaration type */
111         for (ThemisDeclaration myDecl : values()) {
112             if (myDecl.theTester.test(pDecl)) {
113                 return myDecl;
114             }
115         }
116 
117         /* Unrecognised declType */
118         throw pParser.buildException("Unexpected Declaration", pDecl);
119     }
120 }