• 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      *
71      * @return
72      */
containerType()73     Optional<ResolvedReferenceTypeDeclaration> containerType();
74 
75     ///
76     /// Misc
77     ///
78 
79     /**
80      * Is this the declaration of a class?
81      * Note that an Enum is not considered a Class in this case.
82      */
isClass()83     default boolean isClass() {
84         return false;
85     }
86 
87     /**
88      * Is this the declaration of an interface?
89      */
isInterface()90     default boolean isInterface() {
91         return false;
92     }
93 
94     /**
95      * Is this the declaration of an enum?
96      */
isEnum()97     default boolean isEnum() {
98         return false;
99     }
100 
101     /**
102      * Is this the declaration of a type parameter?
103      */
isTypeParameter()104     default boolean isTypeParameter() {
105         return false;
106     }
107 
108     @Override
isType()109     default boolean isType() {
110         return true;
111     }
112 
113     /**
114      * Is this type declaration corresponding to an anonymous class?
115      *
116      * This is an example of anonymous class:
117      * <pre>
118      * HelloWorld frenchGreeting = new HelloWorld() {
119      *     String name = "tout le monde";
120      *
121      *     public void greet() {
122      *         greetSomeone("tout le monde");
123      *     }
124      *
125      *     public void greetSomeone(String someone) {
126      *         name = someone;
127      *         System.out.println("Salut " + name);
128      *     }
129      * };
130      * </pre>
131      */
isAnonymousClass()132     default boolean isAnonymousClass() {
133         return false;
134     }
135 
136     @Override
asType()137     default ResolvedTypeDeclaration asType() {
138         return this;
139     }
140 
141     /**
142      * Return this as a ClassDeclaration or throw UnsupportedOperationException.
143      */
asClass()144     default ResolvedClassDeclaration asClass() {
145         throw new UnsupportedOperationException(String.format("%s is not a class", this));
146     }
147 
148     /**
149      * Return this as a InterfaceDeclaration or throw UnsupportedOperationException.
150      */
asInterface()151     default ResolvedInterfaceDeclaration asInterface() {
152         throw new UnsupportedOperationException(String.format("%s is not an interface", this));
153     }
154 
155     /**
156      * Return this as a EnumDeclaration or throw UnsupportedOperationException.
157      */
asEnum()158     default ResolvedEnumDeclaration asEnum() {
159         throw new UnsupportedOperationException(String.format("%s is not an enum", this));
160     }
161 
162     /**
163      * Return this as a TypeParameterDeclaration or throw UnsupportedOperationException.
164      */
asTypeParameter()165     default ResolvedTypeParameterDeclaration asTypeParameter() {
166         throw new UnsupportedOperationException(String.format("%s is not a type parameter", this));
167     }
168 
asReferenceType()169     default ResolvedReferenceTypeDeclaration asReferenceType() {
170         throw new UnsupportedOperationException(String.format("%s is not a reference type", this));
171     }
172 
173     /**
174      * The package name of the type.
175      */
getPackageName()176     String getPackageName();
177 
178     /**
179      * The class(es) wrapping this type.
180      */
getClassName()181     String getClassName();
182 
183     /**
184      * The fully qualified name of the type declared.
185      */
getQualifiedName()186     String getQualifiedName();
187 
188     /**
189      * The ID corresponds most of the type to the qualified name. It differs only for local
190      * classes which do not have a qualified name but have an ID.
191      */
getId()192     default String getId() {
193         String qname = getQualifiedName();
194         if (qname == null) {
195             return String.format("<localClass>:%s", getName());
196         }
197         return qname;
198     }
199 
200 }
201