-
Notifications
You must be signed in to change notification settings - Fork 78
/
DeclarationInfo.java
170 lines (151 loc) · 4.78 KB
/
DeclarationInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package jakarta.enterprise.lang.model.declarations;
import jakarta.enterprise.lang.model.AnnotationTarget;
import jakarta.enterprise.lang.model.types.Type;
/**
* A declaration is an element of a program source code.
* Declarations are:
*
* <ul>
* <li>{@linkplain PackageInfo packages}</li>
* <li>{@linkplain ClassInfo classes}, including interfaces, enums, annotations, and records</li>
* <li>{@linkplain FieldInfo fields}</li>
* <li>{@linkplain MethodInfo methods}, including constructors</li>
* <li>{@linkplain ParameterInfo method parameters}, including constructor parameters</li>
* </ul>
*
* @since 4.0
*/
public interface DeclarationInfo extends AnnotationTarget {
@Override
default boolean isDeclaration() {
return true;
}
@Override
default boolean isType() {
return false;
}
@Override
default DeclarationInfo asDeclaration() {
return this;
}
@Override
default Type asType() {
throw new IllegalStateException("Not a type");
}
enum Kind {
PACKAGE,
CLASS,
METHOD,
PARAMETER,
FIELD,
RECORD_COMPONENT,
}
/**
* Returns the {@linkplain Kind kind} of this declaration.
*
* @return the kind of this declaration
*/
Kind kind();
/**
* Returns whether this declaration is a {@linkplain PackageInfo package}.
*
* @return {@code true} if this is a package, {@code false} otherwise
*/
default boolean isPackage() {
return kind() == Kind.PACKAGE;
}
/**
* Returns whether this declaration is a {@linkplain ClassInfo class}.
*
* @return {@code true} if this is a class, {@code false} otherwise
*/
default boolean isClass() {
return kind() == Kind.CLASS;
}
/**
* Returns whether this declaration is a {@linkplain MethodInfo method}.
*
* @return {@code true} if this is a method, {@code false} otherwise
*/
default boolean isMethod() {
return kind() == Kind.METHOD;
}
/**
* Returns whether this declaration is a {@linkplain ParameterInfo method parameter}.
*
* @return {@code true} if this is a parameter, {@code false} otherwise
*/
default boolean isParameter() {
return kind() == Kind.PARAMETER;
}
/**
* Returns whether this declaration is a {@linkplain FieldInfo field}.
*
* @return {@code true} if this is a field, {@code false} otherwise
*/
default boolean isField() {
return kind() == Kind.FIELD;
}
/**
* Returns whether this declaration is a {@linkplain RecordComponentInfo record component}.
*
* @return {@code true} if this is a record component, {@code false} otherwise
*/
default boolean isRecordComponent() {
return kind() == Kind.RECORD_COMPONENT;
}
/**
* Returns this declaration as a {@linkplain PackageInfo package}.
*
* @return this package, never {@code null}
* @throws IllegalStateException if {@link #isPackage()} returns {@code false}
*/
default PackageInfo asPackage() {
throw new IllegalStateException("Not a package");
}
/**
* Returns this declaration as a {@linkplain ClassInfo class}.
*
* @return this class, never {@code null}
* @throws IllegalStateException if {@link #isClass()} returns {@code false}
*/
default ClassInfo asClass() {
throw new IllegalStateException("Not a class");
}
/**
* Returns this declaration as a {@linkplain MethodInfo method}.
*
* @return this method, never {@code null}
* @throws IllegalStateException if {@link #isMethod()} returns {@code false}
*/
default MethodInfo asMethod() {
throw new IllegalStateException("Not a method");
}
/**
* Returns this declaration as a {@linkplain ParameterInfo method parameter}.
*
* @return this parameter, never {@code null}
* @throws IllegalStateException if {@link #isParameter()} returns {@code false}
*/
default ParameterInfo asParameter() {
throw new IllegalStateException("Not a parameter");
}
/**
* Returns this declaration as a {@linkplain FieldInfo field}.
*
* @return this field, never {@code null}
* @throws IllegalStateException if {@link #isField()} returns {@code false}
*/
default FieldInfo asField() {
throw new IllegalStateException("Not a field");
}
/**
* Returns this declaration as a {@linkplain RecordComponentInfo record component}.
*
* @return this record component, never {@code null}
* @throws IllegalStateException if {@link #isRecordComponent()} returns {@code false}
*/
default RecordComponentInfo asRecordComponent() {
throw new IllegalStateException("Not a record component");
}
}