blob: 4a7e29258b1a33bc9e7097789b61f3c5c1788a61 [file] [log] [blame]
Federico Tomassetti61db2ec2017-09-27 10:05:59 +02001/*
Federico Tomassettiafa8f6f2017-09-27 21:30:13 +02002 * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser.
3 * Copyright (C) 2011, 2013-2016 The JavaParser Team.
Federico Tomassetti61db2ec2017-09-27 10:05:59 +02004 *
Federico Tomassettiafa8f6f2017-09-27 21:30:13 +02005 * This file is part of JavaParser.
Federico Tomassetti61db2ec2017-09-27 10:05:59 +02006 *
Federico Tomassettiafa8f6f2017-09-27 21:30:13 +02007 * 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
Federico Tomassetti61db2ec2017-09-27 10:05:59 +020012 *
Federico Tomassettiafa8f6f2017-09-27 21:30:13 +020013 * 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.
Federico Tomassetti61db2ec2017-09-27 10:05:59 +020020 */
21
22package com.github.javaparser.resolution.types;
23
24import java.util.Arrays;
25import java.util.Collections;
26import java.util.List;
27
28/**
29 * @author Federico Tomassetti
30 */
Federico Tomassetti6e4bbd62018-02-19 23:10:36 +010031public enum ResolvedPrimitiveType implements ResolvedType {
Federico Tomassetti61db2ec2017-09-27 10:05:59 +020032
Federico Tomassetti61db2ec2017-09-27 10:05:59 +020033
Federico Tomassetti6e4bbd62018-02-19 23:10:36 +010034 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));
Federico Tomassetti61db2ec2017-09-27 10:05:59 +020042
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();
Federico Tomassetti6e4bbd62018-02-19 23:10:36 +010059 for (ResolvedPrimitiveType ptu : values()) {
Federico Tomassetti61db2ec2017-09-27 10:05:59 +020060 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}