Skip to content

Commit 93b41ff

Browse files
authored
Merge pull request #2 from antkorwin/feature/escaping
Feature/escaping
2 parents 3302c35 + 54a941e commit 93b41ff

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ jdk:
55
script:
66
- mvn clean -P jdk-tools org.jacoco:jacoco-maven-plugin:prepare-agent package sonar:sonar -Dsonar.issue.ignore.multicriteria="e1" -Dsonar.issue.ignore.multicriteria.e1.ruleKey="squid:S00119" -Dsonar.issue.ignore.multicriteria.e1.resourceKey="**/*.java"
77

8-
98
addons:
109
sonarcloud:
1110
organization: "antkorwin-github"
12-
token:
13-
secure: $SONAR_CLOUD_KEY
11+
token: $SONAR_CLOUD_KEY
1412

1513
after_success:
1614
- bash <(curl -s https://codecov.io/bash)

README.adoc

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ You need to add the following dependency:
9898
<dependency>
9999
<groupId>com.antkorwin</groupId>
100100
<artifactId>better-strings</artifactId>
101-
<version>0.1</version>
101+
<version>0.2</version>
102102
</dependency>
103103
----
104104

@@ -120,8 +120,50 @@ class Foo {
120120

121121
this code prints: `${a+b}`
122122

123+
Also, you can use the following workaround
124+
to escape string interpolation locally in your code:
125+
126+
[source, java]
127+
----
128+
System.out.println("${'$'}{a+b}");
129+
----
130+
131+
the result is : `${a+b}`
132+
123133

124134
## How it works
125135

126136
Better Strings is a Java Annotation Processor,
127-
but it does not process specific annotations, it makes AST modification of your code while javac compiling it.
137+
but it does not process specific annotations, it makes AST modification of your code while javac compiling it.
138+
139+
NOTE: Keep in mind that this feature should be used carefully.
140+
You shouldn't write too much code inside string literals
141+
because it is too difficult to maintain and maybe not obvious for debugging.
142+
143+
If you want to set annotation processors order
144+
or just set manually processors path,
145+
you should set the path for `better-strings` too, like this:
146+
[source, xml]
147+
----
148+
<build>
149+
<plugins>
150+
<plugin>
151+
<groupId>org.apache.maven.plugins</groupId>
152+
<artifactId>maven-compiler-plugin</artifactId>
153+
<version>3.5.1</version> <!-- or newer version -->
154+
<configuration>
155+
<source>1.8</source> <!-- depending on your project -->
156+
<target>1.8</target> <!-- depending on your project -->
157+
<annotationProcessorPaths>
158+
<!-- other annotation processors -->
159+
<path>
160+
<groupId>com.antkorwin</groupId>
161+
<artifactId>better-strings</artifactId>
162+
<version>0.2</version>
163+
</path>
164+
</annotationProcessorPaths>
165+
</configuration>
166+
</plugin>
167+
</plugins>
168+
</build>
169+
----

src/main/java/com/antkorwin/betterstrings/ast/InnerStringVarsAstTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void visitLiteral(JCTree.JCLiteral jcLiteral) {
101101

102102
List<Token> tokens = tokenizer.split(jcLiteral);
103103

104-
if (tokens.size() < 1) {
104+
if (tokens.isEmpty()) {
105105
return;
106106
}
107107

src/test/java/com/antkorwin/betterstrings/BetterStringsProcessorTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,27 @@ void importDisabledAnnotationAndCheckType() {
182182
assertThat(result).isEqualTo("sum = ${3+4}");
183183
}
184184
}
185+
186+
@Nested
187+
class StringInterpolationEscaping {
188+
189+
@Test
190+
void workaround() {
191+
@Language("Java") String classCode = "import com.antkorwin.betterstrings.*;" +
192+
"" +
193+
"public class Test { " +
194+
" public static String sum(){ " +
195+
" return \"sum = ${'$'}{3 + 4}\";" +
196+
" } " +
197+
"}";
198+
199+
Object result = new CompileTest().classCode("Test", classCode)
200+
.processor(new BetterStringsProcessor())
201+
.compile()
202+
.loadClass("Test")
203+
.invokeStatic("sum");
204+
205+
assertThat(result).isEqualTo("sum = ${3 + 4}");
206+
}
207+
}
185208
}

0 commit comments

Comments
 (0)