{"id":16514252,"url":"https://github.com/antkorwin/better-strings","last_synced_at":"2025-03-16T19:30:40.121Z","repository":{"id":57716875,"uuid":"237779343","full_name":"antkorwin/better-strings","owner":"antkorwin","description":"Java String Interpolation Plugin","archived":false,"fork":false,"pushed_at":"2020-07-20T13:22:46.000Z","size":314,"stargazers_count":98,"open_issues_count":8,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-12T16:11:51.360Z","etag":null,"topics":["interpolation","java","java-string","string","string-formatter","string-interpolation"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/antkorwin.png","metadata":{"files":{"readme":"README.adoc","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-02T13:59:05.000Z","updated_at":"2024-09-05T17:01:13.000Z","dependencies_parsed_at":"2022-09-26T21:40:21.255Z","dependency_job_id":null,"html_url":"https://github.com/antkorwin/better-strings","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antkorwin%2Fbetter-strings","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antkorwin%2Fbetter-strings/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antkorwin%2Fbetter-strings/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antkorwin%2Fbetter-strings/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antkorwin","download_url":"https://codeload.github.com/antkorwin/better-strings/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221667137,"owners_count":16860558,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["interpolation","java","java-string","string","string-formatter","string-interpolation"],"created_at":"2024-10-11T16:11:59.579Z","updated_at":"2024-10-27T11:08:34.082Z","avatar_url":"https://github.com/antkorwin.png","language":"Java","readme":":sectnums:\n\n# Better Strings - Java String Interpolation\n\nimage:https://travis-ci.com/antkorwin/better-strings.svg?branch=master[\"Build Status\",link=\"https://travis-ci.com/antkorwin/better-strings\"]\nimage:https://codecov.io/gh/antkorwin/better-strings/branch/master/graph/badge.svg[link =\"https://codecov.io/gh/antkorwin/better-strings\"]\nimage:https://maven-badges.herokuapp.com/maven-central/com.antkorwin/better-strings/badge.svg[link=\"https://search.maven.org/search?q=g:com.antkorwin%20AND%20a:better-strings\"]\n\nThe Java Plugin to use string interpolation for Java (like in Kotlin).\nSupports Java 8, 9, 10, 11, ...\n\n## Motivation\n\nIn 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.\n\nAnd it’s so sad, that we need writing code like this in the 2020 year:\n\n[source,java]\n----\nint a = 3;\nint b = 4;\nSystem.out.println(a + \" + \" + b + \" = \" + (a + b));\n----\n\njust to print the string: `3 + 4 = 7`\n\n\nof course, we can use a `var` since Java 10:\n\n[source,java]\n----\nvar a = 3;\nvar b = 4;\nSystem.out.println(a + \" + \" + b + \" = \" + (a + b));\n----\n\nBut this code is still sad =(\n\n## What can we do with the Better Strings plugin?\n\n### Using variables in string literals\n\n[source,java]\n----\nvar a = 3;\nvar b = 4;\nSystem.out.println(\"${a} + ${b} = ${a+b}\");\n----\n\nprints:  `3 + 4 = 7`\n\n### Using expressions\n\n[source,java]\n----\nvar a = 3;\nvar b = 4;\nSystem.out.println(\"flag = ${a \u003e b ? true : false}\");\n----\n\nprints:  `flag = false`\n\n[source,java]\n----\nvar a = 3;\nSystem.out.println(\"pow = ${a * a}\");\n----\n\nprints:  `pow = 9`\n\n### Using functions\n\n[source,java]\n----\n@Test\nvoid functionCall() {\n    System.out.println(\"fact(5) = ${factorial(5)}\");\n}\n\nlong factorial(int n) {\n    long fact = 1;\n    for (int i = 2; i \u003c= n; i++) {\n        fact = fact * i;\n    }\n    return fact;\n}\n----\n\nprints:  `fact(5) = 120`\n\n### Using string interpolation in class fields\n\nyou can use better-string for string interpolation in class fields, for example:\n\n[source,java]\n----\npublic class Test {\n\tpublic String field = \"${3+4}\";\n\tpublic String getField(){\n\t\treturn \"field = ${field}\";\n\t}\n}\n----\n\n`new Test().getField()` prints : `field = 7`\n\n### Using string interpolation in default methods of interfaces\n\nalso you can use string interpolation with default methods in interfaces like this:\n\n[source,java]\n----\npublic interface InterfaceWithDefaultMethod {\n\tdefault String sum(){\n\t\treturn \"sum = ${1+2}\";\n\t}\n}\n\npublic class Test implements InterfaceWithDefaultMethod {\n\tpublic String test() {\n\t\treturn sum();\n\t}\n}\n----\n\nThe result of  `new Test().test()` is  `sum = 3`\n\n### Using string interpolation in enums\n\nIn addition you can use string interpolation for code of enums:\n\n[source, java]\n----\npublic enum EnumCode {\n\tFIRST,\n\tSECOND,\n\tTHIRD;\n\n\t@Override\n\tpublic String toString() {\n\t\treturn \"value: ${this.name()}, order: ${this.ordinal() + 1}\";\n\t}\n}\n----\n\n`EnumCode.THIRD.toString();` should print: `value: THIRD, order: 3`\n\n### Limitations\n\nIt's impossible to use the string interpolation within annotations value.\nIt provides compatibility with spring framework properties injecting by the `@Value` annotation.\n\n\n### Disclaimer\n\nNOTE: Keep in mind that this feature should be used carefully.\nYou shouldn't write too much code inside string literals because it is too difficult to maintain and maybe not obvious for debugging.\n\n\n## Getting started\n\n### Maven\n\nYou need to add the following dependency:\n\n[source,xml]\n----\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.antkorwin\u003c/groupId\u003e\n    \u003cartifactId\u003ebetter-strings\u003c/artifactId\u003e\n    \u003cversion\u003e0.5\u003c/version\u003e\n\u003c/dependency\u003e\n----\n\nAnd you can use string interpolation anywhere in your code.\n\nNOTE: if you use `maven-compiler-plugin` in your pom file then declare `better-string`\nin the annotation processors configuration section:\n\n[source, xml]\n----\n\u003cplugin\u003e\n    \u003cgroupId\u003eorg.apache.maven.plugins\u003c/groupId\u003e\n    \u003cartifactId\u003emaven-compiler-plugin\u003c/artifactId\u003e\n    \u003cversion\u003e3.5.1\u003c/version\u003e\n    \u003cconfiguration\u003e\n        \u003cannotationProcessorPaths\u003e\n            \u003cpath\u003e\n               \u003cgroupId\u003ecom.antkorwin\u003c/groupId\u003e\n               \u003cartifactId\u003ebetter-strings\u003c/artifactId\u003e\n               \u003cversion\u003e${better-strings.version}\u003c/version\u003e\n            \u003c/path\u003e\n        \u003c/annotationProcessorPaths\u003e\n    \u003c/configuration\u003e\n\u003c/plugin\u003e\n----\n\nYou can read more about configuration of multiple annotation processors\nfor one project https://github.com/antkorwin/better-strings#6-how-to-use-with-other-annotation-processors[here].\n\n### Gradle\n\nAdd the following dependencies in your `build.gradle` file:\n\n[source, gradle]\n----\ncompileOnly 'com.antkorwin:better-strings:0.4'\nannotationProcessor 'com.antkorwin:better-strings:0.4'\n----\n\nif you want use string interpolation for tests:\n\n[source]\n----\ntestCompileOnly 'com.antkorwin:better-strings:0.4'\ntestAnnotationProcessor 'com.antkorwin:better-strings:0.4'\n----\n\nExample of a simple application with gradle build:\nhttps://github.com/antkorwin/better-strings-demo\n\n### Intellij IDEA with Gradle\n\nSometimes you can get into problems with gradle projects in IDEA,\nan internal runner(in IDEA) may not execute our annotation processor.\n\nYou can read more about this problem here: https://stackoverflow.com/a/55605950\n\nI suggest to turn on `enable annotation processing`\n\nimage::./documentation/enable-annotation-processors.png[enable annotation processing]\n\nAnd select the `gradle test runner` in the Intellij IDEA settings.\n\nimage::./documentation/test-runner.png[gradle test runner]\n\n### Eclipse\n\nUnfortunately, better-string doesn't work with Eclipse.\nEclipse uses its own java compiler and the annotation processing\nwith AST modification isn't work with them out of the box.\n\n\n## How to turn-off string interpolation\n\nTo skip the string interpolation for class, method or field you can use the `@DisabledStringInterpolation` annotation:\n\n[source,java]\n----\n@DisabledStringInterpolation\nclass Foo {\n    void test() {\n        System.out.println(\"${a+b}\");\n    }\n}\n----\n\nthis code prints: `${a+b}`\n\nAlso, you can use the following workaround to escape string interpolation locally in your code:\n\n[source,java]\n----\nSystem.out.println(\"${'$'}{a+b}\");\n----\n\nthe result is : `${a+b}`\n\n## How to control the generated code\n\nBetter Strings is a Java Annotation Processor, but it does not process specific annotations, it makes AST modification of your code while javac compiling it.\n\nBy default, each `${...}` occurrence translates into an invocation of `String#valueOf`.\nFor instance, a string:\n\n[source,java]\n----\n\"Result: ${obj}.method() = ${obj.method()}\"\n----\n\nwill yield:\n\n[source,java]\n----\n\"Result: \"\n  + String.valueOf(obj)\n  + \".method() = \"\n  + String.valueOf(obj.method())\n----\n\nUnder 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.\nThis can be controlled with `-AcallToStringExplicitlyInInterpolations` compiler option, which will instead make the above string translate into:\n\n[source,java]\n----\n\"Result: \"\n  + (java.util.Objects.nonNull(obj) ? java.util.Objects.requireNonNull(obj).toString() : \"null\")\n  + \".method() = \"\n  + (java.util.Objects.nonNull(obj.method()) ? java.util.Objects.requireNonNull(obj.method()).toString() : \"null\")\n----\n\nNOTE: 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.\n\n## How to use with other annotation processors\n\nIf 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.\n\nIn maven, you should declare dependencies as usually, then describe annotation processors in the configuration of the `maven-compiler-plugin`\nin the build section:\n\n[source,xml]\n----\n\u003cplugin\u003e\n    \u003cgroupId\u003eorg.apache.maven.plugins\u003c/groupId\u003e\n    \u003cartifactId\u003emaven-compiler-plugin\u003c/artifactId\u003e\n    \u003cversion\u003e3.5.1\u003c/version\u003e\n    \u003cconfiguration\u003e\n        \u003cannotationProcessorPaths\u003e\n\n            \u003c!-- first annotation processor --\u003e\n            \u003cpath\u003e\n                \u003cgroupId\u003eorg.projectlombok\u003c/groupId\u003e\n                \u003cartifactId\u003elombok\u003c/artifactId\u003e\n                \u003cversion\u003e${lombok.version}\u003c/version\u003e\n            \u003c/path\u003e\n\n            \u003c!-- second annotation processor --\u003e\n            \u003cpath\u003e\n               \u003cgroupId\u003ecom.antkorwin\u003c/groupId\u003e\n               \u003cartifactId\u003ebetter-strings\u003c/artifactId\u003e\n               \u003cversion\u003e${better-strings.version}\u003c/version\u003e\n            \u003c/path\u003e\n\n        \u003c/annotationProcessorPaths\u003e\n    \u003c/configuration\u003e\n\u003c/plugin\u003e\n----\n\nNOTE: The order of annotation processors paths is necessary.\nYou should describe the all used APT when you write `annotationProcessorPaths` section.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantkorwin%2Fbetter-strings","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantkorwin%2Fbetter-strings","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantkorwin%2Fbetter-strings/lists"}