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