1 /* 2 * Copyright (C) 2007-2010 Júlio 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.HashMap; 27 import java.util.List; 28 import java.util.Map; 29 30 /** 31 * A resolved type. It could be a primitive type or a reference type (enum, class, interface). In the later case it 32 * could take type typeParametersValues (other TypeUsages). It could also be a TypeVariable, like in: 33 * <p> 34 * class A<Bgt; { } 35 * <p> 36 * where B is a TypeVariable. It could also be Wildcard Type, possibly with constraints. 37 * 38 * @author Federico Tomassetti 39 */ 40 public interface ResolvedType { 41 42 /// 43 /// Relation with other types 44 /// 45 46 /** 47 * Does this type represent an array? 48 */ isArray()49 default boolean isArray() { 50 return false; 51 } 52 arrayLevel()53 default int arrayLevel() { 54 if (isArray()) { 55 return 1 + this.asArrayType().getComponentType().arrayLevel(); 56 } else { 57 return 0; 58 } 59 } 60 61 /** 62 * Is this a primitive type? 63 */ isPrimitive()64 default boolean isPrimitive() { 65 return false; 66 } 67 68 /** 69 * Is this the null type? 70 */ isNull()71 default boolean isNull() { 72 return false; 73 } 74 75 /** 76 * Is this a union type (as the ones used in multi catch clauses)? 77 */ isUnionType()78 default boolean isUnionType() { 79 return false; 80 } 81 82 /** 83 * Is this a non primitive value? 84 */ isReference()85 default boolean isReference() { 86 return isReferenceType() || isArray() || isTypeVariable() || isNull() || isWildcard() || isUnionType(); 87 } 88 89 /** 90 * Is this a lambda constraint type? 91 */ isConstraint()92 default boolean isConstraint() { return false; } 93 94 /** 95 * Can this be seen as a ReferenceTypeUsage? 96 * In other words: is this a reference to a class, an interface or an enum? 97 */ isReferenceType()98 default boolean isReferenceType() { 99 return false; 100 } 101 isVoid()102 default boolean isVoid() { 103 return false; 104 } 105 isTypeVariable()106 default boolean isTypeVariable() { 107 return false; 108 } 109 isWildcard()110 default boolean isWildcard() { 111 return false; 112 } 113 114 /// 115 /// Downcasting 116 /// 117 asArrayType()118 default ResolvedArrayType asArrayType() { 119 throw new UnsupportedOperationException(String.format("%s is not an Array", this)); 120 } 121 asReferenceType()122 default ResolvedReferenceType asReferenceType() { 123 throw new UnsupportedOperationException(String.format("%s is not a Reference Type", this)); 124 } 125 asTypeParameter()126 default ResolvedTypeParameterDeclaration asTypeParameter() { 127 throw new UnsupportedOperationException(String.format("%s is not a Type parameter", this)); 128 } 129 asTypeVariable()130 default ResolvedTypeVariable asTypeVariable() { 131 throw new UnsupportedOperationException(String.format("%s is not a Type variable", this)); 132 } 133 asPrimitive()134 default ResolvedPrimitiveType asPrimitive() { 135 throw new UnsupportedOperationException(String.format("%s is not a Primitive type", this)); 136 } 137 asWildcard()138 default ResolvedWildcard asWildcard() { 139 throw new UnsupportedOperationException(String.format("%s is not a Wildcard", this)); 140 } 141 asConstraintType()142 default ResolvedLambdaConstraintType asConstraintType() { 143 throw new UnsupportedOperationException(String.format("%s is not a constraint type", this)); 144 } 145 asUnionType()146 default ResolvedUnionType asUnionType() { 147 throw new UnsupportedOperationException(String.format("%s is not a union type", this)); 148 } 149 150 /// 151 /// Naming 152 /// 153 describe()154 String describe(); 155 156 /// 157 /// TypeParameters 158 /// 159 160 /** 161 * Replace all variables referring to the given TypeParameter with the given value. 162 * By replacing these values I could also infer some type equivalence. 163 * Those would be collected in the given map. 164 */ replaceTypeVariables(ResolvedTypeParameterDeclaration tp, ResolvedType replaced, Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes)165 default ResolvedType replaceTypeVariables(ResolvedTypeParameterDeclaration tp, ResolvedType replaced, Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes) { 166 return this; 167 } 168 169 /** 170 * This is like ({@link #replaceTypeVariables(ResolvedTypeParameterDeclaration, ResolvedType, Map)} but ignores the inferred values. 171 */ replaceTypeVariables(ResolvedTypeParameterDeclaration tp, ResolvedType replaced)172 default ResolvedType replaceTypeVariables(ResolvedTypeParameterDeclaration tp, ResolvedType replaced) { 173 return replaceTypeVariables(tp, replaced, new HashMap<>()); 174 } 175 176 /** 177 * Does this type mention at all, directly or indirectly, the given type parameters? 178 */ mention(List<ResolvedTypeParameterDeclaration> typeParameters)179 default boolean mention(List<ResolvedTypeParameterDeclaration> typeParameters) { 180 throw new UnsupportedOperationException(this.getClass().getCanonicalName()); 181 } 182 183 /// 184 /// Assignability 185 /// 186 187 /** 188 * This method checks if ThisType t = new OtherType() would compile. 189 */ isAssignableBy(ResolvedType other)190 boolean isAssignableBy(ResolvedType other); 191 192 } 193