| 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 | package com.github.javaparser.ast.type; |
| 22 | |
| 23 | import com.github.javaparser.ast.AllFieldsConstructor; |
| 24 | import com.github.javaparser.ast.NodeList; |
| 25 | import com.github.javaparser.ast.expr.AnnotationExpr; |
| 26 | import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations; |
| 27 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 28 | import com.github.javaparser.ast.visitor.GenericVisitor; |
| 29 | import com.github.javaparser.ast.visitor.VoidVisitor; |
| 30 | import java.util.HashMap; |
| 31 | import static com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType; |
| 32 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 33 | import com.github.javaparser.ast.Node; |
| 34 | import com.github.javaparser.ast.visitor.CloneVisitor; |
| 35 | import com.github.javaparser.metamodel.PrimitiveTypeMetaModel; |
| 36 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
| 37 | import com.github.javaparser.TokenRange; |
| 38 | import com.github.javaparser.resolution.types.ResolvedPrimitiveType; |
| 39 | import java.util.function.Consumer; |
| 40 | import java.util.Optional; |
| 41 | import com.github.javaparser.ast.Generated; |
| 42 | |
| 43 | /** |
| 44 | * A primitive type. |
| 45 | * <br>{@code int} |
| 46 | * <br>{@code boolean} |
| 47 | * <br>{@code short} |
| 48 | * |
| 49 | * @author Julio Vilmar Gesser |
| 50 | */ |
| 51 | public class PrimitiveType extends Type implements NodeWithAnnotations<PrimitiveType> { |
| 52 | |
| 53 | public static PrimitiveType booleanType() { |
| 54 | return new PrimitiveType(Primitive.BOOLEAN); |
| 55 | } |
| 56 | |
| 57 | public static PrimitiveType charType() { |
| 58 | return new PrimitiveType(Primitive.CHAR); |
| 59 | } |
| 60 | |
| 61 | public static PrimitiveType byteType() { |
| 62 | return new PrimitiveType(Primitive.BYTE); |
| 63 | } |
| 64 | |
| 65 | public static PrimitiveType shortType() { |
| 66 | return new PrimitiveType(Primitive.SHORT); |
| 67 | } |
| 68 | |
| 69 | public static PrimitiveType intType() { |
| 70 | return new PrimitiveType(Primitive.INT); |
| 71 | } |
| 72 | |
| 73 | public static PrimitiveType longType() { |
| 74 | return new PrimitiveType(Primitive.LONG); |
| 75 | } |
| 76 | |
| 77 | public static PrimitiveType floatType() { |
| 78 | return new PrimitiveType(Primitive.FLOAT); |
| 79 | } |
| 80 | |
| 81 | public static PrimitiveType doubleType() { |
| 82 | return new PrimitiveType(Primitive.DOUBLE); |
| 83 | } |
| 84 | |
| 85 | public enum Primitive { |
| 86 | |
| 87 | BOOLEAN("Boolean"), |
| 88 | CHAR("Character"), |
| 89 | BYTE("Byte"), |
| 90 | SHORT("Short"), |
| 91 | INT("Integer"), |
| 92 | LONG("Long"), |
| 93 | FLOAT("Float"), |
| 94 | DOUBLE("Double"); |
| 95 | |
| 96 | final String nameOfBoxedType; |
| 97 | |
| 98 | private String codeRepresentation; |
| 99 | |
| 100 | public ClassOrInterfaceType toBoxedType() { |
| 101 | return parseClassOrInterfaceType(nameOfBoxedType); |
| 102 | } |
| 103 | |
| 104 | public String asString() { |
| 105 | return codeRepresentation; |
| 106 | } |
| 107 | |
| 108 | Primitive(String nameOfBoxedType) { |
| 109 | this.nameOfBoxedType = nameOfBoxedType; |
| 110 | this.codeRepresentation = name().toLowerCase(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static final HashMap<String, Primitive> unboxMap = new HashMap<>(); |
| 115 | |
| 116 | static { |
| 117 | for (Primitive unboxedType : Primitive.values()) { |
| 118 | unboxMap.put(unboxedType.nameOfBoxedType, unboxedType); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | private Primitive type; |
| 123 | |
| 124 | public PrimitiveType() { |
| 125 | this(null, Primitive.INT, new NodeList<>()); |
| 126 | } |
| 127 | |
| 128 | public PrimitiveType(final Primitive type) { |
| 129 | this(null, type, new NodeList<>()); |
| 130 | } |
| 131 | |
| 132 | @AllFieldsConstructor |
| 133 | public PrimitiveType(final Primitive type, NodeList<AnnotationExpr> annotations) { |
| 134 | this(null, type, annotations); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * This constructor is used by the parser and is considered private. |
| 139 | */ |
| 140 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
| 141 | public PrimitiveType(TokenRange tokenRange, Primitive type, NodeList<AnnotationExpr> annotations) { |
| 142 | super(tokenRange, annotations); |
| 143 | setType(type); |
| 144 | customInitialization(); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 149 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
| 150 | return v.visit(this, arg); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
| 155 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
| 156 | v.visit(this, arg); |
| 157 | } |
| 158 | |
| 159 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 160 | public Primitive getType() { |
| 161 | return type; |
| 162 | } |
| 163 | |
| 164 | public ClassOrInterfaceType toBoxedType() { |
| 165 | return type.toBoxedType(); |
| 166 | } |
| 167 | |
| 168 | @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") |
| 169 | public PrimitiveType setType(final Primitive type) { |
| 170 | assertNotNull(type); |
| 171 | if (type == this.type) { |
| 172 | return (PrimitiveType) this; |
| 173 | } |
| 174 | notifyPropertyChange(ObservableProperty.TYPE, this.type, type); |
| 175 | this.type = type; |
| 176 | return this; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public String asString() { |
| 181 | return type.asString(); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public PrimitiveType setAnnotations(NodeList<AnnotationExpr> annotations) { |
| 186 | return (PrimitiveType) super.setAnnotations(annotations); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
| 191 | public boolean remove(Node node) { |
| 192 | if (node == null) |
| 193 | return false; |
| 194 | return super.remove(node); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
| 199 | public PrimitiveType clone() { |
| 200 | return (PrimitiveType) accept(new CloneVisitor(), null); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
| 205 | public PrimitiveTypeMetaModel getMetaModel() { |
| 206 | return JavaParserMetaModel.primitiveTypeMetaModel; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
| 211 | public boolean replace(Node node, Node replacementNode) { |
| 212 | if (node == null) |
| 213 | return false; |
| 214 | return super.replace(node, replacementNode); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 219 | public boolean isPrimitiveType() { |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 225 | public PrimitiveType asPrimitiveType() { |
| 226 | return this; |
| 227 | } |
| 228 | |
| 229 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 230 | public void ifPrimitiveType(Consumer<PrimitiveType> action) { |
| 231 | action.accept(this); |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public ResolvedPrimitiveType resolve() { |
| 236 | return getSymbolResolver().toResolvedType(this, ResolvedPrimitiveType.class); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
| 241 | public Optional<PrimitiveType> toPrimitiveType() { |
| 242 | return Optional.of(this); |
| 243 | } |
| 244 | } |
| 245 |
Members