Home | History | Annotate | Download | only in types
      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 
     22 package com.github.javaparser.resolution.types;
     23 
     24 import com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration;
     25 
     26 import java.util.Map;
     27 
     28 /**
     29  * Array Type.
     30  *
     31  * @author Federico Tomassetti
     32  */
     33 public class ResolvedArrayType implements ResolvedType {
     34 
     35     private ResolvedType baseType;
     36 
     37     public ResolvedArrayType(ResolvedType baseType) {
     38         this.baseType = baseType;
     39     }
     40 
     41     ///
     42     /// Object methods
     43     ///
     44 
     45     @Override
     46     public boolean equals(Object o) {
     47         if (this == o) return true;
     48         if (o == null || getClass() != o.getClass()) return false;
     49 
     50         ResolvedArrayType that = (ResolvedArrayType) o;
     51 
     52         if (!baseType.equals(that.baseType)) return false;
     53 
     54         return true;
     55     }
     56 
     57     @Override
     58     public int hashCode() {
     59         return baseType.hashCode();
     60     }
     61 
     62     @Override
     63     public String toString() {
     64         return "ResolvedArrayType{" + baseType + "}";
     65     }
     66 
     67     ///
     68     /// Type methods
     69     ///
     70 
     71     @Override
     72     public ResolvedArrayType asArrayType() {
     73         return this;
     74     }
     75 
     76     @Override
     77     public boolean isArray() {
     78         return true;
     79     }
     80 
     81     @Override
     82     public String describe() {
     83         return baseType.describe() + "[]";
     84     }
     85 
     86     public ResolvedType getComponentType() {
     87         return baseType;
     88     }
     89 
     90     @Override
     91     public boolean isAssignableBy(ResolvedType other) {
     92         if (other.isArray()) {
     93             if (baseType.isPrimitive() && other.asArrayType().getComponentType().isPrimitive()) {
     94               return baseType.equals(other.asArrayType().getComponentType());
     95             }
     96             return baseType.isAssignableBy(other.asArrayType().getComponentType());
     97         } else if (other.isNull()) {
     98             return true;
     99         }
    100         return false;
    101     }
    102 
    103     @Override
    104     public ResolvedType replaceTypeVariables(ResolvedTypeParameterDeclaration tpToReplace, ResolvedType replaced, Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes) {
    105         ResolvedType baseTypeReplaced = baseType.replaceTypeVariables(tpToReplace, replaced, inferredTypes);
    106         if (baseTypeReplaced == baseType) {
    107             return this;
    108         } else {
    109             return new ResolvedArrayType(baseTypeReplaced);
    110         }
    111     }
    112 
    113 }
    114