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