• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
3  * Copyright (C) 2011, 2013-2016 The JavaParser Team.
4  *
5  * This file is part of JavaParser.
6  *
7  * JavaParser can be used either under the terms of
8  * a) the GNU Lesser General Public License as published by
9  *     the Free Software Foundation, either version 3 of the License, or
10  *     (at your option) any later version.
11  * b) the terms of the Apache License
12  *
13  * You should have received a copy of both licenses in LICENCE.LGPL and
14  * LICENCE.APACHE. Please refer to those files for details.
15  *
16  * JavaParser is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser General Public License for more details.
20  */
21 
22 package com.github.javaparser.resolution.declarations;
23 
24 import com.github.javaparser.resolution.UnsolvedSymbolException;
25 
26 import java.util.Optional;
27 import java.util.Set;
28 
29 /**
30  * A declaration of a type. It could be a primitive type, an enum, a class, an interface
31  * or a type variable.
32  * It cannot be an annotation or an array.
33  *
34  * @author Federico Tomassetti
35  */
36 public interface ResolvedTypeDeclaration extends ResolvedDeclaration {
37 
38     ///
39     /// Containment
40     ///
41 
42     /**
43      * Get the list of types defined inside the current type.
44      */
internalTypes()45     default Set<ResolvedReferenceTypeDeclaration> internalTypes() {
46         throw new UnsupportedOperationException("InternalTypes not available for " + this.getClass().getCanonicalName());
47     }
48 
49     /**
50      * Returns a type declaration for the internal type based on name.
51      * (Does not include internal types inside internal types).
52      */
getInternalType(String name)53     default ResolvedReferenceTypeDeclaration getInternalType(String name) {
54         Optional<ResolvedReferenceTypeDeclaration> type =
55                 this.internalTypes().stream().filter(f -> f.getName().equals(name)).findFirst();
56         return type.orElseThrow(() ->
57                 new UnsolvedSymbolException("Internal type not found: " + name));
58     }
59 
60     /**
61      * Does this type contain an internal type with the given name?
62      * (Does not include internal types inside internal types).
63      */
hasInternalType(String name)64     default boolean hasInternalType(String name) {
65         return this.internalTypes().stream().anyMatch(f -> f.getName().equals(name));
66     }
67 
68     /**
69      * Get the ReferenceTypeDeclaration enclosing this declaration.
70      */
containerType()71     Optional<ResolvedReferenceTypeDeclaration> containerType();
72 
73     ///
74     /// Misc
75     ///
76 
77     /**
78      * Is this the declaration of a class?
79      * Note that an Enum is not considered a Class in this case.
80      */
isClass()81     default boolean isClass() {
82         return false;
83     }
84 
85     /**
86      * Is this the declaration of an interface?
87      */
isInterface()88     default boolean isInterface() {
89         return false;
90     }
91 
92     /**
93      * Is this the declaration of an enum?
94      */
isEnum()95     default boolean isEnum() {
96         return false;
97     }
98 
99     /**
100      * Is this the declaration of a type parameter?
101      */
isTypeParameter()102     default boolean isTypeParameter() {
103         return false;
104     }
105 
106     @Override
isType()107     default boolean isType() {
108         return true;
109     }
110 
111     /**
112      * Is this type declaration corresponding to an anonymous class?
113      *
114      * This is an example of anonymous class:
115      * <pre>
116      * HelloWorld frenchGreeting = new HelloWorld() {
117      *     String name = "tout le monde";
118      *
119      *     public void greet() {
120      *         greetSomeone("tout le monde");
121      *     }
122      *
123      *     public void greetSomeone(String someone) {
124      *         name = someone;
125      *         System.out.println("Salut " + name);
126      *     }
127      * };
128      * </pre>
129      */
isAnonymousClass()130     default boolean isAnonymousClass() {
131         return false;
132     }
133 
134     @Override
asType()135     default ResolvedTypeDeclaration asType() {
136         return this;
137     }
138 
139     /**
140      * Return this as a ClassDeclaration or throw UnsupportedOperationException.
141      */
asClass()142     default ResolvedClassDeclaration asClass() {
143         throw new UnsupportedOperationException(String.format("%s is not a class", this));
144     }
145 
146     /**
147      * Return this as a InterfaceDeclaration or throw UnsupportedOperationException.
148      */
asInterface()149     default ResolvedInterfaceDeclaration asInterface() {
150         throw new UnsupportedOperationException(String.format("%s is not an interface", this));
151     }
152 
153     /**
154      * Return this as a EnumDeclaration or throw UnsupportedOperationException.
155      */
asEnum()156     default ResolvedEnumDeclaration asEnum() {
157         throw new UnsupportedOperationException(String.format("%s is not an enum", this));
158     }
159 
160     /**
161      * Return this as a TypeParameterDeclaration or throw UnsupportedOperationException.
162      */
asTypeParameter()163     default ResolvedTypeParameterDeclaration asTypeParameter() {
164         throw new UnsupportedOperationException(String.format("%s is not a type parameter", this));
165     }
166 
asReferenceType()167     default ResolvedReferenceTypeDeclaration asReferenceType() {
168         throw new UnsupportedOperationException(String.format("%s is not a reference type", this));
169     }
170 
171     /**
172      * The package name of the type.
173      */
getPackageName()174     String getPackageName();
175 
176     /**
177      * The class(es) wrapping this type.
178      */
getClassName()179     String getClassName();
180 
181     /**
182      * The fully qualified name of the type declared.
183      */
getQualifiedName()184     String getQualifiedName();
185 
186     /**
187      * The ID corresponds most of the type to the qualified name. It differs only for local
188      * classes which do not have a qualified name but have an ID.
189      */
getId()190     default String getId() {
191         String qname = getQualifiedName();
192         if (qname == null) {
193             return String.format("<localClass>:%s", getName());
194         }
195         return qname;
196     }
197 
198 }
199