|
| 1 | +:sectnums: |
| 2 | + |
| 3 | +# Better Strings - Java String Interpolation |
| 4 | + |
| 5 | +image:https://travis-ci.com/antkorwin/better-strings.svg?branch=master["Build Status", link="https://travis-ci.com/antkorwin/better-strings"] |
| 6 | +image:https://codecov.io/gh/antkorwin/better-strings/branch/master/graph/badge.svg[link ="https://codecov.io/gh/antkorwin/better-strings"] |
| 7 | + |
| 8 | +The Java Plugin to use string interpolation for Java (like in Kotlin). |
| 9 | +Supports Java 8, 9, 10, 11, ... |
| 10 | + |
| 11 | + |
| 12 | +## Motivation |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +In the latest JEPs https://openjdk.java.net/jeps/355, we have the only expectation of the RAW string literals, |
| 17 | +but there is nothing about the string interpolation. |
| 18 | + |
| 19 | +And it’s so sad, that we need writing code like this in the 2020 year: |
| 20 | + |
| 21 | +[source, java] |
| 22 | +---- |
| 23 | +int a = 3; |
| 24 | +int b = 4; |
| 25 | +System.out.println(a + " + " + b + " = " + (a + b)); |
| 26 | +---- |
| 27 | + |
| 28 | +just to print the string: `3 + 4 = 7` |
| 29 | + |
| 30 | + |
| 31 | +of course, we can use a `var` since Java 10: |
| 32 | + |
| 33 | +[source, java] |
| 34 | +---- |
| 35 | +var a = 3; |
| 36 | +var b = 4; |
| 37 | +System.out.println(a + " + " + b + " = " + (a + b)); |
| 38 | +---- |
| 39 | + |
| 40 | +But this code is still sad =( |
| 41 | + |
| 42 | +## What can we do with the Better Strings plugin? |
| 43 | + |
| 44 | +### Using variables in string literals: |
| 45 | + |
| 46 | +[source, java] |
| 47 | +---- |
| 48 | +var a = 3; |
| 49 | +var b = 4; |
| 50 | +System.out.println("${a} + ${b} = ${a+b}"); |
| 51 | +---- |
| 52 | + |
| 53 | +### Using expressions: |
| 54 | + |
| 55 | +[source, java] |
| 56 | +---- |
| 57 | +var a = 3; |
| 58 | +var b = 4; |
| 59 | +System.out.println("flag = ${a > 5 ? true : false}"); |
| 60 | +---- |
| 61 | + |
| 62 | +[source, java] |
| 63 | +---- |
| 64 | +var a = 3; |
| 65 | +System.out.println("pow2 = ${a * a}"); |
| 66 | +---- |
| 67 | + |
| 68 | +### Using functions: |
| 69 | + |
| 70 | +[source, java] |
| 71 | +---- |
| 72 | +@Test |
| 73 | +void functionCall() { |
| 74 | + System.out.println("fact(5) = ${factorial(5)}"); |
| 75 | +} |
| 76 | +
|
| 77 | +long factorial(int n) { |
| 78 | + long fact = 1; |
| 79 | + for (int i = 2; i <= n; i++) { |
| 80 | + fact = fact * i; |
| 81 | + } |
| 82 | + return fact; |
| 83 | +} |
| 84 | +---- |
| 85 | + |
| 86 | + |
| 87 | +## Getting started |
| 88 | + |
| 89 | +You need to add the following dependency: |
| 90 | + |
| 91 | +[source, xml] |
| 92 | +---- |
| 93 | +<dependency> |
| 94 | + <groupId>com.antkorwin</groupId> |
| 95 | + <artifactId>better-strings</artifactId> |
| 96 | + <version>0.1</version> |
| 97 | +</dependency> |
| 98 | +---- |
| 99 | + |
| 100 | +And you can use string interpolation anywhere in your code. |
| 101 | + |
| 102 | +## How to turn-off string interpolation |
| 103 | + |
| 104 | +To skip the string interpolation for class, method or field you can use the `@DisabledStringInterpolation` annotation: |
| 105 | + |
| 106 | +[source, java] |
| 107 | +---- |
| 108 | +@DisabledStringInterpolation |
| 109 | +class Foo { |
| 110 | + void test() { |
| 111 | + System.out.println("${a+b}"); |
| 112 | + } |
| 113 | +} |
| 114 | +---- |
| 115 | + |
| 116 | +this code prints: `${a+b}` |
| 117 | + |
| 118 | + |
| 119 | +## How it works |
| 120 | + |
| 121 | +Better Strings is a Java Annotation Processor, |
| 122 | +but it does not process specific annotations, it makes AST modification of your code while javac compiling it. |
0 commit comments