Skip to content

Commit 2db47ca

Browse files
author
Vincent Potucek
committed
(re)activate rewrite capability
1 parent 0807b2c commit 2db47ca

File tree

48 files changed

+131
-88
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+131
-88
lines changed

CONTRIBUTING.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ Eclipse settings.
8989
It is forbidden to use _wildcard imports_ (e.g., `import static org.junit.jupiter.api.Assertions.*;`)
9090
in Java code.
9191

92+
### 🚀 Code Transformation & Automated Rewriting
93+
*(Advanced Refactoring, Modernization, and Bulk Code Changes)*
94+
95+
#### 🔧 Automated rewriting of source code to
96+
- **Refactor** safely (e.g., rename methods, migrate APIs)
97+
- **Modernize** (e.g., Java 8 → Java 17 features)
98+
- **Fix anti-patterns** (e.g., replace `Vector` with `ArrayList`)
99+
- **Enforce conventions** (e.g., JUnit's naming rules)
100+
101+
The build system incorporates [Moderne](https://moderne.io/) rewrite capabilities for automated code transformations. These modifications are environment-driven enlisted in the [junitbuild.java-library-conventions.gradle.kts](gradle/plugins/common/src/main/kotlin/junitbuild.java-library-conventions.gradle.kts)
102+
103+
You can use `gradle rewriteRun` to apply these changes.
104+
92105
#### Documentation
93106

94107
Text in `*.adoc` and `*.md` files should be wrapped at 90 characters whenever technically

documentation/src/test/java/example/ClassTemplateDemo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ void wellKnown() {
5252
}
5353

5454
// end::user_guide[]
55-
static
55+
public
5656
// tag::user_guide[]
57-
public class MyClassTemplateInvocationContextProvider
57+
static class MyClassTemplateInvocationContextProvider
5858
// tag::custom_line_break[]
5959
implements ClassTemplateInvocationContextProvider {
6060

documentation/src/test/java/example/ParameterizedMigrationDemo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public class ParameterizedMigrationDemo {
2525
// tag::before[]
2626
@RunWith(Parameterized.class)
2727
// end::before[]
28-
static
28+
public
2929
// tag::before[]
30-
public class JUnit4ParameterizedClassTests {
30+
static class JUnit4ParameterizedClassTests {
3131

3232
@Parameterized.Parameters
3333
public static Iterable<Object[]> data() {

documentation/src/test/java/example/ParameterizedTestDemo.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ void testWithArgumentsSource(String argument) {
357357
}
358358

359359
// end::ArgumentsSource_example[]
360-
static
360+
public
361361
// tag::ArgumentsProvider_example[]
362-
public class MyArgumentsProvider implements ArgumentsProvider {
362+
static class MyArgumentsProvider implements ArgumentsProvider {
363363

364364
@Override
365365
public Stream<? extends Arguments> provideArguments(ParameterDeclarations parameters,
@@ -375,9 +375,9 @@ void testWithArgumentsSourceWithConstructorInjection(String argument) {
375375
assertNotNull(argument);
376376
}
377377

378-
static
378+
public
379379
// tag::ArgumentsProviderWithConstructorInjection_example[]
380-
public class MyArgumentsProviderWithConstructorInjection implements ArgumentsProvider {
380+
static class MyArgumentsProviderWithConstructorInjection implements ArgumentsProvider {
381381

382382
private final TestInfo testInfo;
383383

@@ -427,9 +427,9 @@ void testWithImplicitFallbackArgumentConversion(Book book) {
427427
}
428428

429429
// end::implicit_fallback_conversion_example[]
430-
static
430+
public
431431
// tag::implicit_fallback_conversion_example_Book[]
432-
public class Book {
432+
static class Book {
433433

434434
private final String title;
435435

@@ -458,10 +458,10 @@ void testWithExplicitArgumentConversion(
458458
}
459459

460460
// end::explicit_conversion_example[]
461-
static
461+
public
462462
@SuppressWarnings({ "NullableProblems", "NullAway" })
463463
// tag::explicit_conversion_example_ToStringArgumentConverter[]
464-
public class ToStringArgumentConverter extends SimpleArgumentConverter {
464+
static class ToStringArgumentConverter extends SimpleArgumentConverter {
465465

466466
@Override
467467
protected Object convert(Object source, Class<?> targetType) {
@@ -474,10 +474,10 @@ protected Object convert(Object source, Class<?> targetType) {
474474
}
475475
// end::explicit_conversion_example_ToStringArgumentConverter[]
476476

477-
static
477+
public
478478
@SuppressWarnings({ "NullableProblems", "NullAway", "ConstantValue" })
479479
// tag::explicit_conversion_example_TypedArgumentConverter[]
480-
public class ToLengthArgumentConverter extends TypedArgumentConverter<String, Integer> {
480+
static class ToLengthArgumentConverter extends TypedArgumentConverter<String, Integer> {
481481

482482
protected ToLengthArgumentConverter() {
483483
super(String.class, Integer.class);
@@ -516,7 +516,7 @@ void testWithArgumentsAccessor(ArgumentsAccessor arguments) {
516516
arguments.get(2, Gender.class),
517517
arguments.get(3, LocalDate.class));
518518

519-
if (person.getFirstName().equals("Jane")) {
519+
if ("Jane".equals(person.getFirstName())) {
520520
assertEquals(Gender.F, person.getGender());
521521
}
522522
else {
@@ -540,9 +540,9 @@ void testWithArgumentsAggregator(@AggregateWith(PersonAggregator.class) Person p
540540
}
541541

542542
// end::ArgumentsAggregator_example[]
543-
static
543+
public
544544
// tag::ArgumentsAggregator_example_PersonAggregator[]
545-
public class PersonAggregator extends SimpleArgumentsAggregator {
545+
static class PersonAggregator extends SimpleArgumentsAggregator {
546546
@Override
547547
protected Person aggregateArguments(ArgumentsAccessor arguments, Class<?> targetType,
548548
AnnotatedElementContext context, int parameterIndex) {

documentation/src/test/java/example/TestInfoDemo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ static void beforeAll(TestInfo testInfo) {
3131

3232
TestInfoDemo(TestInfo testInfo) {
3333
String displayName = testInfo.getDisplayName();
34-
assertTrue(displayName.equals("TEST 1") || displayName.equals("test2()"));
34+
assertTrue("TEST 1".equals(displayName) || "test2()".equals(displayName));
3535
}
3636

3737
@BeforeEach
3838
void init(TestInfo testInfo) {
3939
String displayName = testInfo.getDisplayName();
40-
assertTrue(displayName.equals("TEST 1") || displayName.equals("test2()"));
40+
assertTrue("TEST 1".equals(displayName) || "test2()".equals(displayName));
4141
}
4242

4343
@Test

documentation/src/test/java/example/TestTemplateDemo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ void testTemplate(String fruit) {
3838
}
3939

4040
// end::user_guide[]
41-
static
41+
public
4242
// @formatter:off
4343
// tag::user_guide[]
44-
public class MyTestTemplateInvocationContextProvider
44+
static class MyTestTemplateInvocationContextProvider
4545
implements TestTemplateInvocationContextProvider {
4646

4747
@Override

documentation/src/test/resources/log4j2-test.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
<AppenderRef ref="Console" />
1717
</Root>
1818
</Loggers>
19-
</Configuration>
19+
</Configuration>

gradle/plugins/common/src/main/kotlin/junitbuild.java-library-conventions.gradle.kts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,46 @@ plugins {
1717
id("org.openrewrite.rewrite")
1818
}
1919

20+
/**
21+
* activeRecipe("org.openrewrite.java.RemoveUnusedImports") // there are (non); fails to analyze
22+
* activeRecipe("org.openrewrite.java.format.AutoFormat") // could remove check, spot, and all other recipes if working
23+
* activeRecipe("org.openrewrite.java.format.BlankLines") // IndexOutOfBoundsException: Index 0 out of bounds for length 0
24+
* activeRecipe("org.openrewrite.java.format.NormalizeFormat")
25+
* activeRecipe("org.openrewrite.java.format.NormalizeLineBreaks")
26+
* activeRecipe("org.openrewrite.java.format.RemoveTrailingWhitespace")
27+
* activeRecipe("org.openrewrite.java.format.Spaces")
28+
* activeRecipe("org.openrewrite.java.format.TabsAndIndents")
29+
* activeRecipe("org.openrewrite.java.format.WrappingAndBraces")
30+
* activeRecipe("org.openrewrite.java.migrate.UpgradeToJava17")
31+
* activeRecipe("org.openrewrite.java.testing.assertj.Assertj")
32+
* activeRecipe("org.openrewrite.java.testing.cleanup.AssertTrueNullToAssertNull")
33+
* activeRecipe("org.openrewrite.java.testing.cleanup.TestsShouldNotBePublic")
34+
* activeRecipe("org.openrewrite.java.testing.junit5.JUnit5BestPractices") // exclude legacy
35+
* activeRecipe("org.openrewrite.staticanalysis.CodeCleanup")
36+
* activeRecipe("org.openrewrite.staticanalysis.CommonStaticAnalysis")
37+
* activeRecipe("org.openrewrite.staticanalysis.FinalizeLocalVariables")
38+
* activeRecipe("org.openrewrite.staticanalysis.RedundantFileCreation")
39+
* activeRecipe("org.openrewrite.staticanalysis.RemoveUnusedLocalVariables")
40+
* activeRecipe("org.openrewrite.staticanalysis.RemoveUnusedPrivateFields")
41+
* activeRecipe("org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods") // there are non; fails to analyze (even tho it worked once)
42+
* activeRecipe("org.openrewrite.staticanalysis.StringLiteralEquality")
43+
* activeRecipe("org.openrewrite.text.EndOfLineAtEndOfFile")
44+
* activeRecipe("org.openrewrite.java.migrate.UpgradeToJava21")
45+
*/
2046
rewrite {
21-
activeRecipe("org.openrewrite.java.migrate.UpgradeToJava17")
47+
activeRecipe("org.openrewrite.staticanalysis.EqualsAvoidsNull")
48+
activeRecipe("org.openrewrite.staticanalysis.MissingOverrideAnnotation")
49+
activeRecipe("org.openrewrite.staticanalysis.ModifierOrder")
50+
failOnDryRunResults = true
2251
}
2352

53+
/**
54+
* rewrite("org.openrewrite.recipe:rewrite-testing-frameworks")
55+
* rewrite("org.openrewrite.recipe:rewrite-migrate-java")
56+
*/
2457
dependencies {
2558
rewrite(platform(dependencyFromLibs("openrewrite-recipe-bom")))
26-
rewrite("org.openrewrite.recipe:rewrite-migrate-java")
59+
rewrite("org.openrewrite.recipe:rewrite-static-analysis")
2760
}
2861

2962
val mavenizedProjects: List<Project> by rootProject.extra
@@ -281,6 +314,9 @@ tasks {
281314
checkstyleTest {
282315
config = resources.text.fromFile(checkstyle.configDirectory.file("checkstyleTest.xml"))
283316
}
317+
check {
318+
dependsOn(rewriteDryRun)
319+
}
284320
}
285321

286322
pluginManager.withPlugin("java-test-fixtures") {

gradle/plugins/common/src/main/kotlin/junitbuild.spotless-conventions.gradle.kts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ spotless {
1313
targetExclude("gradle/plugins/**/build/**")
1414
leadingSpacesToTabs()
1515
trimTrailingWhitespace()
16-
endWithNewline()
1716
}
1817

1918
format("documentation") {
2019
target("*.adoc", "*.md", "src/**/*.adoc", "src/**/*.md")
2120
trimTrailingWhitespace()
22-
endWithNewline()
2321
}
2422

2523
pluginManager.withPlugin("java") {
@@ -36,7 +34,6 @@ spotless {
3634
val majorMinorVersion = "([0-9]+\\.[0-9]+).*".toRegex().matchEntire(fullVersion)!!.let { it.groups[1]!!.value }
3735
eclipse(majorMinorVersion).configFile(javaFormatterConfigFile)
3836
trimTrailingWhitespace()
39-
endWithNewline()
4037
removeUnusedImports()
4138
}
4239

@@ -46,7 +43,6 @@ spotless {
4643
})
4744
licenseHeaderFile(license.headerFile, "^$")
4845
trimTrailingWhitespace()
49-
endWithNewline()
5046
}
5147
}
5248

@@ -56,7 +52,6 @@ spotless {
5652
ktlint(requiredVersionFromLibs("ktlint"))
5753
licenseHeaderFile(license.headerFile)
5854
trimTrailingWhitespace()
59-
endWithNewline()
6055
}
6156
configurations.named { it.startsWith("spotless") }.configureEach {
6257
// Workaround for CVE-2024-12798 and CVE-2024-12801
@@ -74,7 +69,6 @@ spotless {
7469
groovy {
7570
licenseHeaderFile(license.headerFile)
7671
trimTrailingWhitespace()
77-
endWithNewline()
7872
}
7973
}
8074
}

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TimeoutInvocationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private ScheduledExecutorService getThreadExecutorForSameThreadInvocation() {
5454
}
5555

5656
@SuppressWarnings({ "deprecation", "try" })
57-
private static abstract class ExecutorResource implements Store.CloseableResource, AutoCloseable {
57+
private abstract static class ExecutorResource implements Store.CloseableResource, AutoCloseable {
5858

5959
protected final ScheduledExecutorService executor;
6060

0 commit comments

Comments
 (0)