You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Java Plugin to use string interpolation for Java (like in Kotlin).
10
10
Supports Java 8, 9, 10, 11, ...
11
11
12
-
13
12
## Motivation
14
13
15
-
In the latest JEPs https://openjdk.java.net/jeps/355, we have the only expectation of the RAW string literals,
16
-
but there is nothing about the string interpolation.
14
+
In the latest JEPs https://openjdk.java.net/jeps/355, we have the only expectation of the RAW string literals, but there is nothing about the string interpolation.
17
15
18
16
And it’s so sad, that we need writing code like this in the 2020 year:
19
17
20
-
[source,java]
18
+
[source,java]
21
19
----
22
20
int a = 3;
23
21
int b = 4;
@@ -29,7 +27,7 @@ just to print the string: `3 + 4 = 7`
29
27
30
28
of course, we can use a `var` since Java 10:
31
29
32
-
[source,java]
30
+
[source,java]
33
31
----
34
32
var a = 3;
35
33
var b = 4;
@@ -42,7 +40,7 @@ But this code is still sad =(
42
40
43
41
### Using variables in string literals
44
42
45
-
[source,java]
43
+
[source,java]
46
44
----
47
45
var a = 3;
48
46
var b = 4;
@@ -53,24 +51,26 @@ prints: `3 + 4 = 7`
53
51
54
52
### Using expressions
55
53
56
-
[source,java]
54
+
[source,java]
57
55
----
58
56
var a = 3;
59
57
var b = 4;
60
58
System.out.println("flag = ${a > b ? true : false}");
61
59
----
60
+
62
61
prints: `flag = false`
63
62
64
-
[source,java]
63
+
[source,java]
65
64
----
66
65
var a = 3;
67
66
System.out.println("pow = ${a * a}");
68
67
----
68
+
69
69
prints: `pow = 9`
70
70
71
71
### Using functions
72
72
73
-
[source,java]
73
+
[source,java]
74
74
----
75
75
@Test
76
76
void functionCall() {
@@ -85,21 +85,77 @@ long factorial(int n) {
85
85
return fact;
86
86
}
87
87
----
88
+
88
89
prints: `fact(5) = 120`
89
90
91
+
### Using string interpolation in class fields
92
+
93
+
you can use better-string for string interpolation in class fields, for example:
94
+
95
+
[source,java]
96
+
----
97
+
public class Test {
98
+
public String field = "${3+4}";
99
+
public String getField(){
100
+
return "field = ${field}";
101
+
}
102
+
}
103
+
----
104
+
105
+
`new Test().getField()` prints : `field = 7`
106
+
107
+
### Using string interpolation in default methods of interfaces
108
+
109
+
also you can use string interpolation with default methods in interfaces like this:
110
+
111
+
[source,java]
112
+
----
113
+
public interface InterfaceWithDefaultMethod {
114
+
default String sum(){
115
+
return "sum = ${1+2}";
116
+
}
117
+
}
118
+
119
+
public class Test implements InterfaceWithDefaultMethod {
120
+
public String test() {
121
+
return sum();
122
+
}
123
+
}
124
+
----
125
+
126
+
The result of `new Test().test()` is `sum = 3`
127
+
128
+
### Using string interpolation in enums
129
+
130
+
In addition you can use string interpolation for code of enums:
`EnumCode.THIRD.toString();` should print: `value: THIRD, order: 3`
90
147
91
148
### Disclaimer
92
149
93
150
NOTE: Keep in mind that this feature should be used carefully.
94
-
You shouldn't write too much code inside string literals
95
-
because it is too difficult to maintain and maybe not obvious for debugging.
151
+
You shouldn't write too much code inside string literals because it is too difficult to maintain and maybe not obvious for debugging.
96
152
97
153
98
154
## Getting started
99
155
100
156
You need to add the following dependency:
101
157
102
-
[source,xml]
158
+
[source,xml]
103
159
----
104
160
<dependency>
105
161
<groupId>com.antkorwin</groupId>
@@ -114,7 +170,7 @@ And you can use string interpolation anywhere in your code.
114
170
115
171
To skip the string interpolation for class, method or field you can use the `@DisabledStringInterpolation` annotation:
116
172
117
-
[source,java]
173
+
[source,java]
118
174
----
119
175
@DisabledStringInterpolation
120
176
class Foo {
@@ -126,66 +182,58 @@ class Foo {
126
182
127
183
this code prints: `${a+b}`
128
184
129
-
Also, you can use the following workaround
130
-
to escape string interpolation locally in your code:
185
+
Also, you can use the following workaround to escape string interpolation locally in your code:
131
186
132
-
[source,java]
187
+
[source,java]
133
188
----
134
189
System.out.println("${'$'}{a+b}");
135
190
----
136
191
137
192
the result is : `${a+b}`
138
193
139
-
140
194
## How to control the generated code
141
195
142
-
Better Strings is a Java Annotation Processor,
143
-
but it does not process specific annotations, it makes AST modification of your code while javac compiling it.
196
+
Better Strings is a Java Annotation Processor, but it does not process specific annotations, it makes AST modification of your code while javac compiling it.
144
197
145
198
By default, each `${...}` occurrence translates into an invocation of `String#valueOf`.
146
199
For instance, a string:
147
200
148
-
[source,java]
201
+
[source,java]
149
202
----
150
203
"Result: ${obj}.method() = ${obj.method()}"
151
204
----
152
205
153
206
will yield:
154
207
155
-
[source,java]
208
+
[source,java]
156
209
----
157
210
"Result: "
158
211
+ String.valueOf(obj)
159
212
+ ".method() = "
160
213
+ String.valueOf(obj.method())
161
214
----
162
215
163
-
Under certain circumstances (e.g. with certain static code analyzers), however,
164
-
it might be preferred that the generated code contains an explicit `toString` invocation for each `${...}` occurrence containing a non-null value.
216
+
Under certain circumstances (e.g. with certain static code analyzers), however, it might be preferred that the generated code contains an explicit `toString` invocation for each `${...}` occurrence containing a non-null value.
165
217
This can be controlled with `-AcallToStringExplicitlyInInterpolations` compiler option, which will instead make the above string translate into:
NOTE: this causes the inner part of each `${...}` to be evaluated twice,
176
-
which might be problematic if the expression is side-effecting, non-deterministic or expensive to compute.
177
-
227
+
NOTE: this causes the inner part of each `${...}` to be evaluated twice, which might be problematic if the expression is side-effecting, non-deterministic or expensive to compute.
178
228
179
229
## How to use with other annotation processors
180
230
181
-
If you need to use multiple annotation processors (for example `better-strings` with `lombok` or `mapstruct`)
182
-
and the order of processing is necessary for you then you can set the order in your building tool.
231
+
If you need to use multiple annotation processors (for example `better-strings` with `lombok` or `mapstruct`) and the order of processing is necessary for you then you can set the order in your building tool.
183
232
184
-
In maven, you should declare dependencies as usually,
185
-
then describe annotation processors in the configuration of the `maven-compiler-plugin`
233
+
In maven, you should declare dependencies as usually, then describe annotation processors in the configuration of the `maven-compiler-plugin`
0 commit comments