1 /* 2 * Copyright 2016 Federico Tomassetti 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.github.javaparser.symbolsolver.model.typesystem; 18 19 import com.github.javaparser.symbolsolver.model.declarations.TypeParameterDeclaration; 20 21 import java.util.HashMap; 22 import java.util.Map; 23 24 /** 25 * A usage of a type. It could be a primitive type or a reference type (enum, class, interface). In the later case it 26 * could take type typeParametersValues (other TypeUsages). It could also be a TypeVariable, like in: 27 * <p> 28 * class A<Bgt; { } 29 * <p> 30 * where B is a TypeVariable. It could also be Wildcard Type, possibly with constraints. 31 * 32 * @author Federico Tomassetti 33 */ 34 public interface Type { 35 36 /// 37 /// Relation with other types 38 /// 39 40 /** 41 * Does this type represent an array? 42 */ isArray()43 default boolean isArray() { 44 return false; 45 } 46 arrayLevel()47 default int arrayLevel() { 48 if (isArray()) { 49 return 1 + this.asArrayType().getComponentType().arrayLevel(); 50 } else { 51 return 0; 52 } 53 } 54 55 /** 56 * Is this a primitive type? 57 */ isPrimitive()58 default boolean isPrimitive() { 59 return false; 60 } 61 62 /** 63 * Is this the null type? 64 */ isNull()65 default boolean isNull() { 66 return false; 67 } 68 69 /** 70 * Is this a non primitive value? 71 */ isReference()72 default boolean isReference() { 73 return isReferenceType() || isArray() || isTypeVariable() || isNull() || isWildcard(); 74 } 75 76 /** 77 * Is this a lambda constraint type? 78 */ isConstraint()79 default boolean isConstraint() { return false; } 80 81 /** 82 * Can this be seen as a ReferenceTypeUsage? 83 * In other words: is this a reference to a class, an interface or an enum? 84 */ isReferenceType()85 default boolean isReferenceType() { 86 return false; 87 } 88 isVoid()89 default boolean isVoid() { 90 return false; 91 } 92 isTypeVariable()93 default boolean isTypeVariable() { 94 return false; 95 } 96 isWildcard()97 default boolean isWildcard() { 98 return false; 99 } 100 101 /// 102 /// Downcasting 103 /// 104 asArrayType()105 default ArrayType asArrayType() { 106 throw new UnsupportedOperationException(String.format("%s is not an Array", this)); 107 } 108 asReferenceType()109 default ReferenceType asReferenceType() { 110 throw new UnsupportedOperationException(String.format("%s is not a Reference Type", this)); 111 } 112 asTypeParameter()113 default TypeParameterDeclaration asTypeParameter() { 114 throw new UnsupportedOperationException(String.format("%s is not a Type parameter", this)); 115 } 116 asTypeVariable()117 default TypeVariable asTypeVariable() { 118 throw new UnsupportedOperationException(String.format("%s is not a Type variable", this)); 119 } 120 asPrimitive()121 default PrimitiveType asPrimitive() { 122 throw new UnsupportedOperationException(String.format("%s is not a Primitive type", this)); 123 } 124 asWildcard()125 default Wildcard asWildcard() { 126 throw new UnsupportedOperationException(String.format("%s is not a Wildcard", this)); 127 } 128 asConstraintType()129 default LambdaConstraintType asConstraintType() { 130 throw new UnsupportedOperationException(String.format("%s is not a constraint type", this)); 131 } 132 133 /// 134 /// Naming 135 /// 136 describe()137 String describe(); 138 139 /// 140 /// TypeParameters 141 /// 142 143 /** 144 * Replace all variables referring to the given TypeParameter with the given value. 145 * By replacing these values I could also infer some type equivalence. 146 * Those would be collected in the given map. 147 */ replaceTypeVariables(TypeParameterDeclaration tp, Type replaced, Map<TypeParameterDeclaration, Type> inferredTypes)148 default Type replaceTypeVariables(TypeParameterDeclaration tp, Type replaced, Map<TypeParameterDeclaration, Type> inferredTypes) { 149 return this; 150 } 151 152 /** 153 * This is like ({@link #replaceTypeVariables(TypeParameterDeclaration, Type, Map)} but ignores the inferred values. 154 */ replaceTypeVariables(TypeParameterDeclaration tp, Type replaced)155 default Type replaceTypeVariables(TypeParameterDeclaration tp, Type replaced) { 156 return replaceTypeVariables(tp, replaced, new HashMap<>()); 157 } 158 159 /// 160 /// Assignability 161 /// 162 163 /** 164 * This method checks if ThisType t = new OtherType() would compile. 165 */ isAssignableBy(Type other)166 boolean isAssignableBy(Type other); 167 168 } 169