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 java.util.Arrays; 25 import java.util.Collections; 26 import java.util.List; 27 28 /** 29 * @author Federico Tomassetti 30 */ 31 public enum ResolvedPrimitiveType implements ResolvedType { 32 33 34 BYTE("byte", Byte.class.getCanonicalName(), Collections.emptyList()), 35 SHORT("short", Short.class.getCanonicalName(), Collections.singletonList(BYTE)), 36 CHAR("char", Character.class.getCanonicalName(), Collections.emptyList()), 37 INT("int", Integer.class.getCanonicalName(), Arrays.asList(BYTE, SHORT, CHAR)), 38 LONG("long", Long.class.getCanonicalName(), Arrays.asList(BYTE, SHORT, INT, CHAR)), 39 BOOLEAN("boolean", Boolean.class.getCanonicalName(), Collections.emptyList()), 40 FLOAT("float", Float.class.getCanonicalName(), Arrays.asList(LONG, INT, SHORT, BYTE, CHAR)), 41 DOUBLE("double", Double.class.getCanonicalName(), Arrays.asList(FLOAT, LONG, INT, SHORT, BYTE, CHAR)); 42 43 /// 44 /// Fields 45 /// 46 47 private String name; 48 private String boxTypeQName; 49 private List<ResolvedPrimitiveType> promotionTypes; 50 51 private ResolvedPrimitiveType(String name, String boxTypeQName, List<ResolvedPrimitiveType> promotionTypes) { 52 this.name = name; 53 this.boxTypeQName = boxTypeQName; 54 this.promotionTypes = promotionTypes; 55 } 56 57 public static ResolvedType byName(String name) { 58 name = name.toLowerCase(); 59 for (ResolvedPrimitiveType ptu : values()) { 60 if (ptu.describe().equals(name)) { 61 return ptu; 62 } 63 } 64 throw new IllegalArgumentException("Name " + name); 65 } 66 67 @Override 68 public String toString() { 69 return "PrimitiveTypeUsage{" + 70 "name='" + name + '\'' + 71 '}'; 72 } 73 74 public ResolvedPrimitiveType asPrimitive() { 75 return this; 76 } 77 78 @Override 79 public boolean isArray() { 80 return false; 81 } 82 83 @Override 84 public boolean isPrimitive() { 85 return true; 86 } 87 88 @Override 89 public boolean isReferenceType() { 90 return false; 91 } 92 93 @Override 94 public String describe() { 95 return name; 96 } 97 98 @Override 99 public boolean isTypeVariable() { 100 return false; 101 } 102 103 @Override 104 public boolean isAssignableBy(ResolvedType other) { 105 if (other.isPrimitive()) { 106 return this == other || promotionTypes.contains(other); 107 } else if (other.isReferenceType()) { 108 if (other.asReferenceType().getQualifiedName().equals(boxTypeQName)) { 109 return true; 110 } 111 for (ResolvedPrimitiveType promotion : promotionTypes) { 112 if (other.asReferenceType().getQualifiedName().equals(promotion.boxTypeQName)) { 113 return true; 114 } 115 } 116 return false; 117 } else if (other.isConstraint()){ 118 return this.isAssignableBy(other.asConstraintType().getBound()); 119 } else { 120 return false; 121 } 122 } 123 124 public String getBoxTypeQName() { 125 return boxTypeQName; 126 } 127 128 } 129