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
@@ -274,4 +276,39 @@ When a JavaTemplate is used for matching, then the `Matcher` can be used to "ext
274
276
275
277
This can then be passed into another JavaTemplate to generate some new code (such as what was done with the `after` template above).
276
278
277
-
Another useful example that doesn't involve Refaster recipes is our [JavaTemplateMatchTest class](https://github.com/openrewrite/rewrite/blob/main/rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateMatchTest.java#L32). In there, you can see how we create a JavaTemplate for the use case of matching/finding code rather than replacing it.
279
+
Another useful example that doesn't involve Refaster recipes is our [JavaTemplateMatchTest class](https://github.com/openrewrite/rewrite/blob/main/rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateMatchTest.java#L32). In there, you can see how we create a JavaTemplate for the use case of matching/finding code rather than replacing it.
280
+
281
+
## Semantics class shortcut
282
+
283
+
If you're looking for a more concise way to define JavaTemplates, the [Semantics class](https://github.com/openrewrite/rewrite-templating/blob/main/src/main/java/org/openrewrite/java/template/Semantics.java) offers a powerful alternative. Instead of writing the raw template string yourself, you can define the desired expression or statement directly in Java, and a annotation processor will automatically generate the corresponding template code for you.
284
+
285
+
This approach helps you avoid dealing with the syntax of raw templates and lets the compiler validate your logic directly.
Semantics.expression(this, "IsEmpty", (String s) -> (s ==null|| s.isEmpty())).build();
299
+
```
300
+
301
+
Similarly, for statements:
302
+
303
+
```java
304
+
JavaTemplate newInstanceTemplate =
305
+
Semantics.statement(this, "Example", () -> { String example ="some statement"; }).build();
306
+
```
307
+
308
+
Behind the scenes, the annotation processor transforms these `Semantics.expression(...)` and `Semantics.statement(...)` calls into actual JavaTemplate definitions with proper substitution indicators and imports.
309
+
310
+
:::info
311
+
Prerequisite: To use this feature, make sure your project is set up for Refaster-style recipes with annotation processing enabled. See the [How to create a Refaster recipe](../authoring-recipes/refaster-recipes#how-to-create-a-refaster-recipe) section for more details.
312
+
:::
313
+
314
+
Since the Semantics class is functionally equivalent to defining a JavaTemplate manually, while offering compile-time safety and slightly less boilerplate, it is well-suited for cases where your template is just beyond what Refaster can express easily. It provides a convenient middle ground between Refaster and fully custom JavaTemplate logic.
0 commit comments