1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 Júlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 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 | 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 | /* |
68 | * Returns an array containing all numeric types |
69 | */ |
70 | public static ResolvedPrimitiveType[] getNumericPrimitiveTypes() { |
71 | return new ResolvedPrimitiveType[] {BYTE,SHORT,CHAR,INT,LONG,FLOAT,DOUBLE}; |
72 | } |
73 | |
74 | @Override |
75 | public String toString() { |
76 | return "PrimitiveTypeUsage{" + |
77 | "name='" + name + '\'' + |
78 | '}'; |
79 | } |
80 | |
81 | public ResolvedPrimitiveType asPrimitive() { |
82 | return this; |
83 | } |
84 | |
85 | @Override |
86 | public boolean isArray() { |
87 | return false; |
88 | } |
89 | |
90 | @Override |
91 | public boolean isPrimitive() { |
92 | return true; |
93 | } |
94 | |
95 | @Override |
96 | public boolean isReferenceType() { |
97 | return false; |
98 | } |
99 | |
100 | @Override |
101 | public String describe() { |
102 | return name; |
103 | } |
104 | |
105 | @Override |
106 | public boolean isTypeVariable() { |
107 | return false; |
108 | } |
109 | |
110 | @Override |
111 | public boolean isAssignableBy(ResolvedType other) { |
112 | if (other.isPrimitive()) { |
113 | return this == other || promotionTypes.contains(other); |
114 | } else if (other.isReferenceType()) { |
115 | if (other.asReferenceType().getQualifiedName().equals(boxTypeQName)) { |
116 | return true; |
117 | } |
118 | for (ResolvedPrimitiveType promotion : promotionTypes) { |
119 | if (other.asReferenceType().getQualifiedName().equals(promotion.boxTypeQName)) { |
120 | return true; |
121 | } |
122 | } |
123 | return false; |
124 | } else { |
125 | return other.isConstraint() && this.isAssignableBy(other.asConstraintType().getBound()); |
126 | } |
127 | } |
128 | |
129 | public String getBoxTypeQName() { |
130 | return boxTypeQName; |
131 | } |
132 | |
133 | public boolean isNumeric() { |
134 | return this != BOOLEAN; |
135 | } |
136 | |
137 | /** |
138 | * Is this a boolean type? |
139 | */ |
140 | public boolean isBoolean() { |
141 | return this == BOOLEAN; |
142 | } |
143 | |
144 | /* |
145 | * Binary primitive promotion (see https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2) |
146 | * If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8). |
147 | */ |
148 | public ResolvedPrimitiveType bnp(ResolvedPrimitiveType other) { |
149 | // If either operand is of type double, the other is converted to double. |
150 | if (this == ResolvedPrimitiveType.DOUBLE || other == ResolvedPrimitiveType.DOUBLE) { |
151 | return ResolvedPrimitiveType.DOUBLE; |
152 | // Otherwise, if either operand is of type float, the other is converted to float. |
153 | } else if (this == ResolvedPrimitiveType.FLOAT || other == ResolvedPrimitiveType.FLOAT) { |
154 | return ResolvedPrimitiveType.FLOAT; |
155 | // Otherwise, if either operand is of type long, the other is converted to long. |
156 | } else if (this == ResolvedPrimitiveType.LONG || other == ResolvedPrimitiveType.LONG) { |
157 | return ResolvedPrimitiveType.LONG; |
158 | } |
159 | // Otherwise, both operands are converted to type int. |
160 | return ResolvedPrimitiveType.INT; |
161 | } |
162 | |
163 | /* |
164 | * Verify if the ResolvedPrimitiveType is in the list of ResolvedPrimitiveType |
165 | */ |
166 | public boolean in(ResolvedPrimitiveType[] types) { |
167 | return Arrays.stream(types).anyMatch(type -> this == type); |
168 | } |
169 | |
170 | } |
171 |
Members