JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ast/type/PrimitiveType.java
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 */
21package com.github.javaparser.ast.type;
22
23import com.github.javaparser.ast.AllFieldsConstructor;
24import com.github.javaparser.ast.NodeList;
25import com.github.javaparser.ast.expr.AnnotationExpr;
26import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
27import com.github.javaparser.ast.observer.ObservableProperty;
28import com.github.javaparser.ast.visitor.GenericVisitor;
29import com.github.javaparser.ast.visitor.VoidVisitor;
30import java.util.HashMap;
31import static com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType;
32import static com.github.javaparser.utils.Utils.assertNotNull;
33import com.github.javaparser.ast.Node;
34import com.github.javaparser.ast.visitor.CloneVisitor;
35import com.github.javaparser.metamodel.PrimitiveTypeMetaModel;
36import com.github.javaparser.metamodel.JavaParserMetaModel;
37import com.github.javaparser.TokenRange;
38import com.github.javaparser.resolution.types.ResolvedPrimitiveType;
39import java.util.function.Consumer;
40import java.util.Optional;
41import 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 */
51public 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<StringPrimitiveunboxMap = new HashMap<>();
115
116    static {
117        for (Primitive unboxedType : Primitive.values()) {
118            unboxMap.put(unboxedType.nameOfBoxedTypeunboxedType);
119        }
120    }
121
122    private Primitive type;
123
124    public PrimitiveType() {
125        this(nullPrimitive.INT, new NodeList<>());
126    }
127
128    public PrimitiveType(final Primitive type) {
129        this(nulltype, new NodeList<>());
130    }
131
132    @AllFieldsConstructor
133    public PrimitiveType(final Primitive typeNodeList<AnnotationExprannotations) {
134        this(nulltypeannotations);
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 tokenRangePrimitive typeNodeList<AnnotationExprannotations) {
142        super(tokenRangeannotations);
143        setType(type);
144        customInitialization();
145    }
146
147    @Override
148    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
149    public <RAR accept(final GenericVisitor<RAvfinal A arg) {
150        return v.visit(this, arg);
151    }
152
153    @Override
154    @Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
155    public <Avoid accept(final VoidVisitor<Avfinal 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.typetype);
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<AnnotationExprannotations) {
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 (PrimitiveTypeaccept(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 nodeNode replacementNode) {
212        if (node == null)
213            return false;
214        return super.replace(nodereplacementNode);
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<PrimitiveTypeaction) {
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<PrimitiveTypetoPrimitiveType() {
242        return Optional.of(this);
243    }
244}
245
MembersX
PrimitiveType:intType
PrimitiveType:doubleType
PrimitiveType:shortType
PrimitiveType:Primitive:nameOfBoxedType
PrimitiveType:getMetaModel
PrimitiveType:Primitive:Primitive
PrimitiveType:unboxMap
PrimitiveType:toBoxedType
PrimitiveType:clone
PrimitiveType:Primitive:asString
PrimitiveType:getType
PrimitiveType:setAnnotations
PrimitiveType:asPrimitiveType
PrimitiveType:Primitive:FLOAT
PrimitiveType:ifPrimitiveType
PrimitiveType:Primitive:BYTE
PrimitiveType:Primitive:DOUBLE
PrimitiveType:Primitive:toBoxedType
PrimitiveType:floatType
PrimitiveType:Primitive:BOOLEAN
PrimitiveType:asString
PrimitiveType:Primitive:LONG
PrimitiveType:Primitive:SHORT
PrimitiveType:Primitive:INT
PrimitiveType:remove
PrimitiveType:replace
PrimitiveType:Primitive:codeRepresentation
PrimitiveType:byteType
PrimitiveType:Primitive:CHAR
PrimitiveType:PrimitiveType
PrimitiveType:setType
PrimitiveType:booleanType
PrimitiveType:longType
PrimitiveType:toPrimitiveType
PrimitiveType:charType
PrimitiveType:resolve
PrimitiveType:type
PrimitiveType:isPrimitiveType
PrimitiveType:accept
Members
X