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.expr; |
22 | |
23 | import com.github.javaparser.ast.AllFieldsConstructor; |
24 | import com.github.javaparser.ast.Node; |
25 | import com.github.javaparser.ast.visitor.CloneVisitor; |
26 | import com.github.javaparser.ast.visitor.GenericVisitor; |
27 | import com.github.javaparser.ast.visitor.VoidVisitor; |
28 | import com.github.javaparser.metamodel.CharLiteralExprMetaModel; |
29 | import com.github.javaparser.metamodel.JavaParserMetaModel; |
30 | import com.github.javaparser.utils.StringEscapeUtils; |
31 | import com.github.javaparser.utils.Utils; |
32 | import com.github.javaparser.TokenRange; |
33 | import java.util.function.Consumer; |
34 | import java.util.Optional; |
35 | import com.github.javaparser.ast.Generated; |
36 | |
37 | /** |
38 | * A literal character. |
39 | * <br>{@code 'a'} |
40 | * <br>{@code '\t'} |
41 | * <br>{@code 'Ω'} |
42 | * <br>{@code '\177'} |
43 | * <br>{@code '💩'} |
44 | * |
45 | * @author Julio Vilmar Gesser |
46 | */ |
47 | public class CharLiteralExpr extends LiteralStringValueExpr { |
48 | |
49 | public CharLiteralExpr() { |
50 | this(null, "?"); |
51 | } |
52 | |
53 | @AllFieldsConstructor |
54 | public CharLiteralExpr(String value) { |
55 | this(null, value); |
56 | } |
57 | |
58 | /** |
59 | * Constructs a CharLiteralExpr with given escaped character. |
60 | * |
61 | * @param value a char |
62 | */ |
63 | public CharLiteralExpr(char value) { |
64 | this(null, StringEscapeUtils.escapeJava(String.valueOf(value))); |
65 | } |
66 | |
67 | /** |
68 | * This constructor is used by the parser and is considered private. |
69 | */ |
70 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
71 | public CharLiteralExpr(TokenRange tokenRange, String value) { |
72 | super(tokenRange, value); |
73 | customInitialization(); |
74 | } |
75 | |
76 | /** |
77 | * Utility method that creates a new StringLiteralExpr. Escapes EOL characters. |
78 | */ |
79 | public static CharLiteralExpr escape(String string) { |
80 | return new CharLiteralExpr(Utils.escapeEndOfLines(string)); |
81 | } |
82 | |
83 | @Override |
84 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
85 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
86 | return v.visit(this, arg); |
87 | } |
88 | |
89 | @Override |
90 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
91 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
92 | v.visit(this, arg); |
93 | } |
94 | |
95 | @Override |
96 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
97 | public boolean remove(Node node) { |
98 | if (node == null) |
99 | return false; |
100 | return super.remove(node); |
101 | } |
102 | |
103 | /** |
104 | * @return the unescaped value character of this literal |
105 | */ |
106 | public char asChar() { |
107 | return StringEscapeUtils.unescapeJava(value).charAt(0); |
108 | } |
109 | |
110 | /** |
111 | * Sets the given char as the literal value |
112 | * |
113 | * @param value a char |
114 | * @return this expression |
115 | */ |
116 | public CharLiteralExpr setChar(char value) { |
117 | this.value = String.valueOf(value); |
118 | return this; |
119 | } |
120 | |
121 | @Override |
122 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
123 | public CharLiteralExpr clone() { |
124 | return (CharLiteralExpr) accept(new CloneVisitor(), null); |
125 | } |
126 | |
127 | @Override |
128 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
129 | public CharLiteralExprMetaModel getMetaModel() { |
130 | return JavaParserMetaModel.charLiteralExprMetaModel; |
131 | } |
132 | |
133 | @Override |
134 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
135 | public boolean replace(Node node, Node replacementNode) { |
136 | if (node == null) |
137 | return false; |
138 | return super.replace(node, replacementNode); |
139 | } |
140 | |
141 | @Override |
142 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
143 | public boolean isCharLiteralExpr() { |
144 | return true; |
145 | } |
146 | |
147 | @Override |
148 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
149 | public CharLiteralExpr asCharLiteralExpr() { |
150 | return this; |
151 | } |
152 | |
153 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
154 | public void ifCharLiteralExpr(Consumer<CharLiteralExpr> action) { |
155 | action.accept(this); |
156 | } |
157 | |
158 | @Override |
159 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
160 | public Optional<CharLiteralExpr> toCharLiteralExpr() { |
161 | return Optional.of(this); |
162 | } |
163 | } |
164 |
Members