Giter Club home page Giter Club logo

better-strings's Introduction

Better Strings - Java String Interpolation

Build Status badge

The Java Plugin to use string interpolation for Java (like in Kotlin). Supports Java 8, 9, 10, 11, …​

1. Motivation

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.

And it’s so sad, that we need writing code like this in the 2020 year:

int a = 3;
int b = 4;
System.out.println(a + " + " + b + " = " + (a + b));

just to print the string: 3 + 4 = 7

of course, we can use a var since Java 10:

var a = 3;
var b = 4;
System.out.println(a + " + " + b + " = " + (a + b));

But this code is still sad =(

2. What can we do with the Better Strings plugin?

2.1. Using variables in string literals:

var a = 3;
var b = 4;
System.out.println("${a} + ${b} = ${a+b}");

prints: 3 + 4 = 7

2.2. Using expressions:

var a = 3;
var b = 4;
System.out.println("flag = ${a > b ? true : false}");

prints: flag = false

var a = 3;
System.out.println("pow = ${a * a}");

prints: pow = 9

2.3. Using functions:

@Test
void functionCall() {
    System.out.println("fact(5) = ${factorial(5)}");
}

long factorial(int n) {
    long fact = 1;
    for (int i = 2; i <= n; i++) {
        fact = fact * i;
    }
    return fact;
}

prints: fact(5) = 120

3. Getting started

You need to add the following dependency:

<dependency>
    <groupId>com.antkorwin</groupId>
    <artifactId>better-strings</artifactId>
    <version>0.1</version>
</dependency>

And you can use string interpolation anywhere in your code.

4. How to turn-off string interpolation

To skip the string interpolation for class, method or field you can use the @DisabledStringInterpolation annotation:

@DisabledStringInterpolation
class Foo {
    void test() {
        System.out.println("${a+b}");
    }
}

this code prints: ${a+b}

5. How it works

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.

better-strings's People

Contributors

antkorwin avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.