| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
| 3 | * Copyright (C) 2011, 2013-2020 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 | package com.github.javaparser.ast.type; |
| 22 | |
| 23 | import com.github.javaparser.ast.AllFieldsConstructor; |
| 24 | import com.github.javaparser.ast.Node; |
| 25 | import com.github.javaparser.ast.NodeList; |
| 26 | import com.github.javaparser.ast.expr.AnnotationExpr; |
| 27 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 28 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 29 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 30 | import com.github.javaparser.metamodel.TypeMetaModel; |
| 31 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 32 | import com.github.javaparser.TokenRange; |
| 33 | import com.github.javaparser.resolution.Resolvable; |
| 34 | import com.github.javaparser.resolution.types.ResolvedType; |
| 35 | import com.github.javaparser.ast.Generated; |
| 36 | import java.util.function.Consumer; |
| 37 | import static com.github.javaparser.utils.CodeGenerationUtils.f; |
| 38 | import java.util.Optional; |
| 39 | |
| 40 | /** |
| 41 | * Base class for types. |
| 42 | * |
| 43 | * @author Julio Vilmar Gesser |
| 44 | */ |
| 45 | public abstract class Type extends Node implements Resolvable<ResolvedType> { |
| 46 | |
| 47 | private NodeList<AnnotationExpr> annotations; |
| 48 | |
| 49 | /** |
| 50 | * Several sub classes do not support annotations. |
| 51 | * This is a support constructor for them. |
| 52 | */ |
| 53 | protected Type(TokenRange range) { |
| 54 | this(range, new NodeList<>()); |
| 55 | } |
| 56 | |
| 57 | @AllFieldsConstructor |
| 58 | public Type(NodeList<AnnotationExpr> annotations) { |
| 59 | this(null, annotations); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * This constructor is used by the parser and is considered private. |
| 64 | */ |
| 65 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 66 | public Type(TokenRange tokenRange, NodeList<AnnotationExpr> annotations) { |
| 67 | super(tokenRange); |
| 68 | setAnnotations(annotations); |
| 69 | customInitialization(); |
| 70 | } |
| 71 | |
| 72 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 73 | public NodeList<AnnotationExpr> getAnnotations() { |
| 74 | return annotations; |
| 75 | } |
| 76 | |
| 77 | public AnnotationExpr getAnnotation(int i) { |
| 78 | return getAnnotations().get(i); |
| 79 | } |
| 80 | |
| 81 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 82 | public Type setAnnotations(final NodeList<AnnotationExpr> annotations) { |
| 83 | assertNotNull(annotations); |
| 84 | if (annotations == this.annotations) { |
| 85 | return (Type) this; |
| 86 | } |
| 87 | notifyPropertyChange(ObservableProperty.ANNOTATIONS, this.annotations, annotations); |
| 88 | if (this.annotations != null) |
| 89 | this.annotations.setParentNode(null); |
| 90 | this.annotations = annotations; |
| 91 | setAsParentNodeOf(annotations); |
| 92 | return this; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Finds the element type, meaning: the type without ArrayTypes around it. |
| 97 | * <p> |
| 98 | * In "{@code int[] a[];}", the element type is int. |
| 99 | */ |
| 100 | public Type getElementType() { |
| 101 | if (this instanceof ArrayType) { |
| 102 | return ((ArrayType) this).getComponentType().getElementType(); |
| 103 | } |
| 104 | return this; |
| 105 | } |
| 106 | |
| 107 | public int getArrayLevel() { |
| 108 | if (this instanceof ArrayType) { |
| 109 | return 1 + ((ArrayType) this).getComponentType().getArrayLevel(); |
| 110 | } else { |
| 111 | return 0; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 117 | public boolean remove(Node node) { |
| 118 | if (node == null) |
| 119 | return false; |
| 120 | for (int i = 0; i < annotations.size(); i++) { |
| 121 | if (annotations.get(i) == node) { |
| 122 | annotations.remove(i); |
| 123 | return true; |
| 124 | } |
| 125 | } |
| 126 | return super.remove(node); |
| 127 | } |
| 128 | |
| 129 | public abstract String asString(); |
| 130 | |
| 131 | @Override |
| 132 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 133 | public Type clone() { |
| 134 | return (Type) accept(new CloneVisitor(), null); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 139 | public TypeMetaModel getMetaModel() { |
| 140 | return JavaParserMetaModel.typeMetaModel; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 145 | public boolean replace(Node node, Node replacementNode) { |
| 146 | if (node == null) |
| 147 | return false; |
| 148 | for (int i = 0; i < annotations.size(); i++) { |
| 149 | if (annotations.get(i) == node) { |
| 150 | annotations.set(i, (AnnotationExpr) replacementNode); |
| 151 | return true; |
| 152 | } |
| 153 | } |
| 154 | return super.replace(node, replacementNode); |
| 155 | } |
| 156 | |
| 157 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 158 | public boolean isArrayType() { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 163 | public ArrayType asArrayType() { |
| 164 | throw new IllegalStateException(f("%s is not an ArrayType", this)); |
| 165 | } |
| 166 | |
| 167 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 168 | public boolean isClassOrInterfaceType() { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 173 | public ClassOrInterfaceType asClassOrInterfaceType() { |
| 174 | throw new IllegalStateException(f("%s is not an ClassOrInterfaceType", this)); |
| 175 | } |
| 176 | |
| 177 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 178 | public boolean isIntersectionType() { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 183 | public IntersectionType asIntersectionType() { |
| 184 | throw new IllegalStateException(f("%s is not an IntersectionType", this)); |
| 185 | } |
| 186 | |
| 187 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 188 | public boolean isPrimitiveType() { |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 193 | public PrimitiveType asPrimitiveType() { |
| 194 | throw new IllegalStateException(f("%s is not an PrimitiveType", this)); |
| 195 | } |
| 196 | |
| 197 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 198 | public boolean isReferenceType() { |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 203 | public ReferenceType asReferenceType() { |
| 204 | throw new IllegalStateException(f("%s is not an ReferenceType", this)); |
| 205 | } |
| 206 | |
| 207 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 208 | public boolean isTypeParameter() { |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 213 | public TypeParameter asTypeParameter() { |
| 214 | throw new IllegalStateException(f("%s is not an TypeParameter", this)); |
| 215 | } |
| 216 | |
| 217 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 218 | public boolean isUnionType() { |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 223 | public UnionType asUnionType() { |
| 224 | throw new IllegalStateException(f("%s is not an UnionType", this)); |
| 225 | } |
| 226 | |
| 227 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 228 | public boolean isUnknownType() { |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 233 | public UnknownType asUnknownType() { |
| 234 | throw new IllegalStateException(f("%s is not an UnknownType", this)); |
| 235 | } |
| 236 | |
| 237 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 238 | public boolean isVoidType() { |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 243 | public VoidType asVoidType() { |
| 244 | throw new IllegalStateException(f("%s is not an VoidType", this)); |
| 245 | } |
| 246 | |
| 247 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 248 | public boolean isWildcardType() { |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 253 | public WildcardType asWildcardType() { |
| 254 | throw new IllegalStateException(f("%s is not an WildcardType", this)); |
| 255 | } |
| 256 | |
| 257 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 258 | public void ifArrayType(Consumer<ArrayType> action) { |
| 259 | } |
| 260 | |
| 261 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 262 | public void ifClassOrInterfaceType(Consumer<ClassOrInterfaceType> action) { |
| 263 | } |
| 264 | |
| 265 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 266 | public void ifIntersectionType(Consumer<IntersectionType> action) { |
| 267 | } |
| 268 | |
| 269 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 270 | public void ifPrimitiveType(Consumer<PrimitiveType> action) { |
| 271 | } |
| 272 | |
| 273 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 274 | public void ifReferenceType(Consumer<ReferenceType> action) { |
| 275 | } |
| 276 | |
| 277 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 278 | public void ifTypeParameter(Consumer<TypeParameter> action) { |
| 279 | } |
| 280 | |
| 281 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 282 | public void ifUnionType(Consumer<UnionType> action) { |
| 283 | } |
| 284 | |
| 285 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 286 | public void ifUnknownType(Consumer<UnknownType> action) { |
| 287 | } |
| 288 | |
| 289 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 290 | public void ifVoidType(Consumer<VoidType> action) { |
| 291 | } |
| 292 | |
| 293 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 294 | public void ifWildcardType(Consumer<WildcardType> action) { |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public abstract ResolvedType resolve(); |
| 299 | |
| 300 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 301 | public Optional<ArrayType> toArrayType() { |
| 302 | return Optional.empty(); |
| 303 | } |
| 304 | |
| 305 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 306 | public Optional<ClassOrInterfaceType> toClassOrInterfaceType() { |
| 307 | return Optional.empty(); |
| 308 | } |
| 309 | |
| 310 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 311 | public Optional<IntersectionType> toIntersectionType() { |
| 312 | return Optional.empty(); |
| 313 | } |
| 314 | |
| 315 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 316 | public Optional<PrimitiveType> toPrimitiveType() { |
| 317 | return Optional.empty(); |
| 318 | } |
| 319 | |
| 320 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 321 | public Optional<ReferenceType> toReferenceType() { |
| 322 | return Optional.empty(); |
| 323 | } |
| 324 | |
| 325 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 326 | public Optional<TypeParameter> toTypeParameter() { |
| 327 | return Optional.empty(); |
| 328 | } |
| 329 | |
| 330 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 331 | public Optional<UnionType> toUnionType() { |
| 332 | return Optional.empty(); |
| 333 | } |
| 334 | |
| 335 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 336 | public Optional<UnknownType> toUnknownType() { |
| 337 | return Optional.empty(); |
| 338 | } |
| 339 | |
| 340 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 341 | public Optional<VoidType> toVoidType() { |
| 342 | return Optional.empty(); |
| 343 | } |
| 344 | |
| 345 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 346 | public Optional<WildcardType> toWildcardType() { |
| 347 | return Optional.empty(); |
| 348 | } |
| 349 | |
| 350 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 351 | public boolean isVarType() { |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 356 | public VarType asVarType() { |
| 357 | throw new IllegalStateException(f("%s is not an VarType", this)); |
| 358 | } |
| 359 | |
| 360 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 361 | public Optional<VarType> toVarType() { |
| 362 | return Optional.empty(); |
| 363 | } |
| 364 | |
| 365 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 366 | public void ifVarType(Consumer<VarType> action) { |
| 367 | } |
| 368 | } |
| 369 |
Members