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.JavaParserMetaModel; |
29 | import com.github.javaparser.metamodel.StringLiteralExprMetaModel; |
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 | import static com.github.javaparser.utils.StringEscapeUtils.*; |
37 | |
38 | /** |
39 | * A literal string. |
40 | * <br>{@code "Hello World!"} |
41 | * <br>{@code "\"\n"} |
42 | * <br>{@code "\u2122"} |
43 | * <br>{@code "™"} |
44 | * <br>{@code "💩"} |
45 | * |
46 | * @author Julio Vilmar Gesser |
47 | */ |
48 | public class StringLiteralExpr extends LiteralStringValueExpr { |
49 | |
50 | public StringLiteralExpr() { |
51 | this(null, "empty"); |
52 | } |
53 | |
54 | /** |
55 | * Creates a string literal expression from given string. Escapes EOL characters. |
56 | * |
57 | * @param value the value of the literal |
58 | */ |
59 | @AllFieldsConstructor |
60 | public StringLiteralExpr(final String value) { |
61 | this(null, Utils.escapeEndOfLines(value)); |
62 | } |
63 | |
64 | /** |
65 | * This constructor is used by the parser and is considered private. |
66 | */ |
67 | @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") |
68 | public StringLiteralExpr(TokenRange tokenRange, String value) { |
69 | super(tokenRange, value); |
70 | customInitialization(); |
71 | } |
72 | |
73 | @Override |
74 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
75 | public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { |
76 | return v.visit(this, arg); |
77 | } |
78 | |
79 | @Override |
80 | @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") |
81 | public <A> void accept(final VoidVisitor<A> v, final A arg) { |
82 | v.visit(this, arg); |
83 | } |
84 | |
85 | @Override |
86 | @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") |
87 | public boolean remove(Node node) { |
88 | if (node == null) |
89 | return false; |
90 | return super.remove(node); |
91 | } |
92 | |
93 | /** |
94 | * Sets the content of this expressions to given value. Escapes EOL characters. |
95 | * |
96 | * @param value the new literal value |
97 | * @return self |
98 | */ |
99 | public StringLiteralExpr setEscapedValue(String value) { |
100 | this.value = Utils.escapeEndOfLines(value); |
101 | return this; |
102 | } |
103 | |
104 | /** |
105 | * @return the unescaped literal value |
106 | */ |
107 | public String asString() { |
108 | return unescapeJava(value); |
109 | } |
110 | |
111 | /** |
112 | * Escapes the given string from special characters and uses it as the literal value. |
113 | * |
114 | * @param value unescaped string |
115 | * @return this literal expression |
116 | */ |
117 | public StringLiteralExpr setString(String value) { |
118 | this.value = escapeJava(value); |
119 | return this; |
120 | } |
121 | |
122 | @Override |
123 | @Generated("com.github.javaparser.generator.core.node.CloneGenerator") |
124 | public StringLiteralExpr clone() { |
125 | return (StringLiteralExpr) accept(new CloneVisitor(), null); |
126 | } |
127 | |
128 | @Override |
129 | @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") |
130 | public StringLiteralExprMetaModel getMetaModel() { |
131 | return JavaParserMetaModel.stringLiteralExprMetaModel; |
132 | } |
133 | |
134 | @Override |
135 | @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") |
136 | public boolean replace(Node node, Node replacementNode) { |
137 | if (node == null) |
138 | return false; |
139 | return super.replace(node, replacementNode); |
140 | } |
141 | |
142 | @Override |
143 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
144 | public boolean isStringLiteralExpr() { |
145 | return true; |
146 | } |
147 | |
148 | @Override |
149 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
150 | public StringLiteralExpr asStringLiteralExpr() { |
151 | return this; |
152 | } |
153 | |
154 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
155 | public void ifStringLiteralExpr(Consumer<StringLiteralExpr> action) { |
156 | action.accept(this); |
157 | } |
158 | |
159 | @Override |
160 | @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") |
161 | public Optional<StringLiteralExpr> toStringLiteralExpr() { |
162 | return Optional.of(this); |
163 | } |
164 | } |
165 |
Members