Skip to content

Commit 95577f9

Browse files
OpenRewrite recipe best practices
Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.recipes.rewrite.OpenRewriteRecipeBestPractices?organizationId=QUxML01vZGVybmUvTW9kZXJuZSArIE9wZW5SZXdyaXRl Co-authored-by: Moderne <team@moderne.io>
1 parent 4238f5a commit 95577f9

2 files changed

Lines changed: 131 additions & 1 deletion

File tree

src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForConstructors.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import lombok.EqualsAndHashCode;
1919
import lombok.Value;
20-
import org.jspecify.annotations.Nullable;
2120
import org.openrewrite.ExecutionContext;
2221
import org.openrewrite.Preconditions;
2322
import org.openrewrite.Recipe;

src/main/resources/META-INF/rewrite/examples.yml

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7508,6 +7508,137 @@ examples:
75087508
language: java
75097509
---
75107510
type: specs.openrewrite.org/v1beta/example
7511+
recipeName: org.openrewrite.java.migrate.lang.var.UseVarForConstructors
7512+
examples:
7513+
- description: '`UseVarForConstructorsTest#replacePatterns`'
7514+
sources:
7515+
- before: |
7516+
import java.io.ByteArrayInputStream;
7517+
import java.util.ArrayList;
7518+
import java.util.HashMap;
7519+
7520+
class Test<E, K, V> {
7521+
void test() {
7522+
// Basic constructor
7523+
StringBuilder sb = new StringBuilder();
7524+
7525+
// Constructor with arguments
7526+
StringBuilder sbWithArg = new StringBuilder("initial");
7527+
7528+
// Final modifier
7529+
final StringBuilder finalSb = new StringBuilder();
7530+
7531+
// Generics with concrete types
7532+
ArrayList<String> list = new ArrayList<>();
7533+
7534+
// Explicit type arguments on constructor (non-diamond)
7535+
ArrayList<String> explicitList = new ArrayList<String>();
7536+
7537+
// Nested generics with concrete types
7538+
HashMap<String, ArrayList<Integer>> map = new HashMap<>();
7539+
7540+
// Type variable in generic
7541+
ArrayList<E> typeVarList = new ArrayList<>();
7542+
7543+
// Multiple type variables
7544+
HashMap<K, V> typeVarMap = new HashMap<>();
7545+
7546+
// Nested type variables
7547+
HashMap<K, ArrayList<V>> nested = new HashMap<>();
7548+
7549+
// Inner class constructor
7550+
HashMap.SimpleEntry<String, Integer> entry = new HashMap.SimpleEntry<>("key", 1);
7551+
7552+
// In lambda
7553+
Runnable r = () -> {
7554+
ArrayList<String> lambdaList = new ArrayList<>();
7555+
};
7556+
7557+
// For-loop initializer
7558+
for (StringBuilder forSb = new StringBuilder(); forSb.length() < 10; forSb.append("x")) {
7559+
}
7560+
7561+
// Try-with-resources
7562+
try (ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0])) {
7563+
} catch (Exception e) {
7564+
}
7565+
}
7566+
7567+
// Instance initializer
7568+
{
7569+
StringBuilder initSb = new StringBuilder();
7570+
}
7571+
7572+
// Static initializer
7573+
static {
7574+
StringBuilder staticSb = new StringBuilder();
7575+
}
7576+
}
7577+
after: |
7578+
import java.io.ByteArrayInputStream;
7579+
import java.util.ArrayList;
7580+
import java.util.HashMap;
7581+
7582+
class Test<E, K, V> {
7583+
void test() {
7584+
// Basic constructor
7585+
var sb = new StringBuilder();
7586+
7587+
// Constructor with arguments
7588+
var sbWithArg = new StringBuilder("initial");
7589+
7590+
// Final modifier
7591+
final var finalSb = new StringBuilder();
7592+
7593+
// Generics with concrete types
7594+
var list = new ArrayList<String>();
7595+
7596+
// Explicit type arguments on constructor (non-diamond)
7597+
var explicitList = new ArrayList<String>();
7598+
7599+
// Nested generics with concrete types
7600+
var map = new HashMap<String, ArrayList<Integer>>();
7601+
7602+
// Type variable in generic
7603+
var typeVarList = new ArrayList<E>();
7604+
7605+
// Multiple type variables
7606+
var typeVarMap = new HashMap<K, V>();
7607+
7608+
// Nested type variables
7609+
var nested = new HashMap<K, ArrayList<V>>();
7610+
7611+
// Inner class constructor
7612+
var entry = new HashMap.SimpleEntry<String, Integer>("key", 1);
7613+
7614+
// In lambda
7615+
Runnable r = () -> {
7616+
var lambdaList = new ArrayList<String>();
7617+
};
7618+
7619+
// For-loop initializer
7620+
for (var forSb = new StringBuilder(); forSb.length() < 10; forSb.append("x")) {
7621+
}
7622+
7623+
// Try-with-resources
7624+
try (var bais = new ByteArrayInputStream(new byte[0])) {
7625+
} catch (Exception e) {
7626+
}
7627+
}
7628+
7629+
// Instance initializer
7630+
{
7631+
var initSb = new StringBuilder();
7632+
}
7633+
7634+
// Static initializer
7635+
static {
7636+
var staticSb = new StringBuilder();
7637+
}
7638+
}
7639+
language: java
7640+
---
7641+
type: specs.openrewrite.org/v1beta/example
75117642
recipeName: org.openrewrite.java.migrate.lang.var.UseVarForGenericMethodInvocations
75127643
examples:
75137644
- description: '`Applicable#withJDKFactoryMethods`'

0 commit comments

Comments
 (0)