From ade0b739196582ca7cc307c5e29731ee1ad8c2ab Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Thu, 31 Jul 2025 18:17:03 +0200 Subject: [PATCH] OpenRewriteRecipeBestPractices --- Application_Properties.java | 1 + pom.xml | 7 ++++++ .../de/kreth/property2java/Configuration.java | 4 ++-- .../de/kreth/property2java/Generator.java | 2 ++ .../kreth/property2java/GeneratorOptions.java | 4 ++-- .../cli/ArgumentConfiguration.java | 1 + .../de/kreth/property2java/cli/CliConfig.java | 4 ++-- .../config/FreemarkerConfig.java | 2 ++ .../processor/GenerateProperty2Java.java | 1 + .../GenerateResourceBundleProperty2Java.java | 3 ++- .../processor/ProcessorConfiguration.java | 6 ++--- .../processor/Property2JavaGenerator.java | 2 +- .../property2java/ConfigurationTest.java | 1 + .../kreth/property2java/GeneratorTests.java | 8 +++---- .../GeneratorWithInnerPropertiesTest.java | 22 ++++++++----------- .../property2java/TestPropertiesSource.java | 7 +++--- .../parts/ReplaceLogicForTemplate.java | 14 ++++++------ .../parts/ReplaceLogicForTemplateTest.java | 9 +++++--- .../processor/Property2JavaGeneratorTest.java | 2 +- 19 files changed, 57 insertions(+), 43 deletions(-) diff --git a/Application_Properties.java b/Application_Properties.java index 8b13789..139597f 100644 --- a/Application_Properties.java +++ b/Application_Properties.java @@ -1 +1,2 @@ + diff --git a/pom.xml b/pom.xml index 70e4624..fe9e673 100644 --- a/pom.xml +++ b/pom.xml @@ -162,17 +162,24 @@ org.openrewrite.staticanalysis.JavaApiBestPractices org.openrewrite.staticanalysis.CommonStaticAnalysis org.openrewrite.staticanalysis.CodeCleanup + org.openrewrite.recipes.rewrite.OpenRewriteRecipeBestPractices **/generated/** + org.openrewrite.recipe rewrite-migrate-java 3.14.1 + + org.openrewrite.recipe + rewrite-rewrite + 0.5.0 + diff --git a/src/main/java/de/kreth/property2java/Configuration.java b/src/main/java/de/kreth/property2java/Configuration.java index 3227a61..409f5f3 100644 --- a/src/main/java/de/kreth/property2java/Configuration.java +++ b/src/main/java/de/kreth/property2java/Configuration.java @@ -10,9 +10,9 @@ import java.nio.file.Path; import java.util.EnumSet; import java.util.Map; -import org.apache.commons.text.WordUtils; import de.kreth.property2java.config.Regex; +import org.apache.commons.text.WordUtils; public interface Configuration { @@ -45,7 +45,7 @@ public interface Configuration { default EnumSet getOptions() { return EnumSet.noneOf(GeneratorOptions.class); } - + default Writer outWriter(String fileName) throws IOException { return new FileWriter(new File(getRootPath().toFile(), mapFilenameToClassName(fileName) + ".java"), outputCharset()); diff --git a/src/main/java/de/kreth/property2java/Generator.java b/src/main/java/de/kreth/property2java/Generator.java index 1bb883c..218f15f 100644 --- a/src/main/java/de/kreth/property2java/Generator.java +++ b/src/main/java/de/kreth/property2java/Generator.java @@ -17,8 +17,10 @@ import java.util.Map; import java.util.Properties; import java.util.stream.Collectors; + import de.kreth.property2java.cli.ArgumentConfiguration; import de.kreth.property2java.config.FreemarkerConfig; + import freemarker.template.Template; import freemarker.template.TemplateException; diff --git a/src/main/java/de/kreth/property2java/GeneratorOptions.java b/src/main/java/de/kreth/property2java/GeneratorOptions.java index 5b39e69..c990b50 100644 --- a/src/main/java/de/kreth/property2java/GeneratorOptions.java +++ b/src/main/java/de/kreth/property2java/GeneratorOptions.java @@ -11,13 +11,13 @@ public enum GeneratorOptions { * Add a format Method, which uses and supports {@link MessageFormat}. */ WithMessageFormatter("java.text.MessageFormat"); - + private final String[] additionalImport; private GeneratorOptions(String... additionalImport) { this.additionalImport = additionalImport!= null ? additionalImport : new String[]{}; } - + String[] getAdditionalImport() { return additionalImport; } diff --git a/src/main/java/de/kreth/property2java/cli/ArgumentConfiguration.java b/src/main/java/de/kreth/property2java/cli/ArgumentConfiguration.java index 3fee716..ca9d7a0 100644 --- a/src/main/java/de/kreth/property2java/cli/ArgumentConfiguration.java +++ b/src/main/java/de/kreth/property2java/cli/ArgumentConfiguration.java @@ -12,6 +12,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; + import de.kreth.property2java.Configuration; public final class ArgumentConfiguration implements Configuration { diff --git a/src/main/java/de/kreth/property2java/cli/CliConfig.java b/src/main/java/de/kreth/property2java/cli/CliConfig.java index a5a12d3..7ec88ce 100644 --- a/src/main/java/de/kreth/property2java/cli/CliConfig.java +++ b/src/main/java/de/kreth/property2java/cli/CliConfig.java @@ -2,6 +2,8 @@ package de.kreth.property2java.cli; import java.io.IOException; + +import de.kreth.property2java.cli.ArgumentConfiguration.Builder; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; @@ -11,8 +13,6 @@ import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; -import de.kreth.property2java.cli.ArgumentConfiguration.Builder; - public class CliConfig { private final Options options = options(); diff --git a/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java b/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java index 057b1b1..ff8877c 100644 --- a/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java +++ b/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java @@ -2,7 +2,9 @@ package de.kreth.property2java.config; import java.io.IOException; + import de.kreth.property2java.Format; + import freemarker.template.Configuration; import freemarker.template.Template; diff --git a/src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java b/src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java index 52dda7e..9bc318a 100644 --- a/src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java +++ b/src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java @@ -6,6 +6,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; + import de.kreth.property2java.Format; import de.kreth.property2java.GeneratorOptions; diff --git a/src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Java.java b/src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Java.java index a528d08..c1d3410 100644 --- a/src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Java.java +++ b/src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Java.java @@ -7,6 +7,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; + import de.kreth.property2java.Format; import de.kreth.property2java.GeneratorOptions; @@ -46,6 +47,6 @@ public @interface GenerateResourceBundleProperty2Java { String resource(); Format format(); - + GeneratorOptions[] options() default {}; } diff --git a/src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java b/src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java index f170ffa..fe51ab9 100644 --- a/src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java +++ b/src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java @@ -11,7 +11,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; - import javax.annotation.processing.Filer; import javax.lang.model.element.Element; import javax.lang.model.element.PackageElement; @@ -19,6 +18,7 @@ import javax.lang.model.element.TypeElement; import javax.tools.FileObject; import javax.tools.StandardLocation; + import de.kreth.property2java.Configuration; import de.kreth.property2java.Format; import de.kreth.property2java.Generator; @@ -70,7 +70,7 @@ public class ProcessorConfiguration implements Configuration { public EnumSet getOptions() { return EnumSet.copyOf(Arrays.asList(options)); } - + @Override public Path getRootPath() { throw new UnsupportedOperationException( @@ -112,7 +112,7 @@ public class ProcessorConfiguration implements Configuration { this.options = options; return this; } - + public Builder addAll(String[] resourceNames) { this.resourcenames.addAll(Arrays.asList(resourceNames)); return this; diff --git a/src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java b/src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java index 5fd600b..48aadd3 100644 --- a/src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java +++ b/src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java @@ -6,7 +6,6 @@ import java.io.StringWriter; import java.util.Arrays; import java.util.List; import java.util.Set; - import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.SupportedAnnotationTypes; @@ -16,6 +15,7 @@ import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic.Kind; + import de.kreth.property2java.Format; import de.kreth.property2java.GeneratorException; import de.kreth.property2java.GeneratorOptions; diff --git a/src/test/java/de/kreth/property2java/ConfigurationTest.java b/src/test/java/de/kreth/property2java/ConfigurationTest.java index 4d8f16f..bae8019 100644 --- a/src/test/java/de/kreth/property2java/ConfigurationTest.java +++ b/src/test/java/de/kreth/property2java/ConfigurationTest.java @@ -10,6 +10,7 @@ import java.io.FileWriter; import java.io.IOException; import java.io.Writer; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; diff --git a/src/test/java/de/kreth/property2java/GeneratorTests.java b/src/test/java/de/kreth/property2java/GeneratorTests.java index 81b0d6f..c0bfa4b 100644 --- a/src/test/java/de/kreth/property2java/GeneratorTests.java +++ b/src/test/java/de/kreth/property2java/GeneratorTests.java @@ -9,10 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import java.io.File; import java.io.IOException; @@ -28,6 +25,7 @@ import java.util.Optional; import java.util.StringTokenizer; import java.util.stream.Collectors; + import org.hamcrest.Matchers; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -109,7 +107,7 @@ class GeneratorTests { assertFalse(root.containsKey("imports")); } - + @Test void testClassDefinition() throws IOException, GeneratorException { diff --git a/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java b/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java index cb321d5..5fc9052 100644 --- a/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java +++ b/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java @@ -2,14 +2,9 @@ package de.kreth.property2java; import static de.kreth.property2java.TestPropertiesSource.testProperties; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import java.io.File; import java.io.IOException; @@ -25,6 +20,7 @@ import java.util.Optional; import java.util.StringTokenizer; import java.util.stream.Collectors; + import org.hamcrest.Matchers; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -123,11 +119,11 @@ public class GeneratorWithInnerPropertiesTest { String load = null; while (sourceTokenizer.hasMoreTokens()) { String line = sourceTokenizer.nextToken(); - if (line.contains("Properties") - && !line.contains("import") - && !line.contains("enum") - && !line.contains("@link") - && !line.contains("class")) { + if (line.contains("Properties") && + !line.contains("import") && + !line.contains("enum") && + !line.contains("@link") && + !line.contains("class")) { declaration = line; } else if (line.contains(".load")) { load = line; @@ -143,7 +139,7 @@ public class GeneratorWithInnerPropertiesTest { Matchers.containsString("properties.load(Application_Properties.class.getResourceAsStream(\"/application.properties\"));")); } - + @Test void testOneInputGeneratesOneOutput() throws IOException, GeneratorException { diff --git a/src/test/java/de/kreth/property2java/TestPropertiesSource.java b/src/test/java/de/kreth/property2java/TestPropertiesSource.java index 0142c9b..313701d 100644 --- a/src/test/java/de/kreth/property2java/TestPropertiesSource.java +++ b/src/test/java/de/kreth/property2java/TestPropertiesSource.java @@ -5,6 +5,7 @@ import java.io.FileWriter; import java.io.IOException; import java.io.StringReader; + import de.kreth.property2java.generated.GenerateTheTest; public class TestPropertiesSource { @@ -39,7 +40,7 @@ public class TestPropertiesSource { message.with.five.placeholders = Third is first{2}, then last "{4}", second={1}, fourth={3} and first is last={0} """); } - + public static void main(String[] args) throws IOException { File dir = new File("D:\\Markus\\programmierung\\workspace_clubhelper\\PropertyToJavaGenerator\\src\\test\\resources"); @@ -47,7 +48,7 @@ public class TestPropertiesSource { testProperties().transferTo(out); System.out.println("Generated: " + GenerateTheTest.PROPERTY_LOADER_PROPERTIES); } - + try (FileWriter out = new FileWriter(new File(dir, GenerateTheTest.UNARY_OPERATOR_PROPERTIES))) { testProperties().transferTo(out); System.out.println("Generated: " + GenerateTheTest.UNARY_OPERATOR_PROPERTIES); @@ -60,6 +61,6 @@ public class TestPropertiesSource { testProperties().transferTo(out); System.out.println("Generated: " + GenerateTheTest.PROPERTY_LOADER_OPTIONS_PROPERTIES); } - + } } diff --git a/src/test/java/de/kreth/property2java/parts/ReplaceLogicForTemplate.java b/src/test/java/de/kreth/property2java/parts/ReplaceLogicForTemplate.java index 94a50a0..9f8f3b3 100644 --- a/src/test/java/de/kreth/property2java/parts/ReplaceLogicForTemplate.java +++ b/src/test/java/de/kreth/property2java/parts/ReplaceLogicForTemplate.java @@ -1,12 +1,12 @@ package de.kreth.property2java.parts; -public class ReplaceLogicForTemplate { +public final class ReplaceLogicForTemplate { static String doReplacements(String property, Object...objects) { StringBuilder text = new StringBuilder(); int index = property.indexOf('{'); text.append(property.substring(0, index)); - + while (index >= 0) { index++; int endIndex = withEndIndex(index, property); @@ -14,8 +14,8 @@ public class ReplaceLogicForTemplate { String theIndex = property.substring(index, endIndex); int withIndex = Integer.valueOf(theIndex); if (withIndex+1> objects.length) { - throw new IllegalStateException("No Argument for Index {" + theIndex - + "}" + " at Position=" + (index - 1) + " in \"" + property + "\""); + throw new IllegalStateException("No Argument for Index {" + theIndex + + "}" + " at Position=" + (index - 1) + " in \"" + property + "\""); } text.append(objects[withIndex].toString()); index = property.indexOf('{', endIndex); @@ -26,7 +26,7 @@ public class ReplaceLogicForTemplate { } } else { endIndex = index; - + index = property.indexOf('{', index); if (index <0) { text.append('{'); @@ -36,7 +36,7 @@ public class ReplaceLogicForTemplate { } return text.toString(); } - + /** * extracts the end index, if (and only if) the closing } exists and * between the indicee an integer value exists. @@ -45,7 +45,7 @@ public class ReplaceLogicForTemplate { * @return -1 if invalid or not existing */ private static int withEndIndex(int index, String property) { - + int result = -1; int endIndex = property.indexOf('}', index); if (endIndex >index) { diff --git a/src/test/java/de/kreth/property2java/parts/ReplaceLogicForTemplateTest.java b/src/test/java/de/kreth/property2java/parts/ReplaceLogicForTemplateTest.java index a6a81d5..87dc9eb 100644 --- a/src/test/java/de/kreth/property2java/parts/ReplaceLogicForTemplateTest.java +++ b/src/test/java/de/kreth/property2java/parts/ReplaceLogicForTemplateTest.java @@ -1,11 +1,14 @@ package de.kreth.property2java.parts; -import static org.assertj.core.api.Assertions.*; import static de.kreth.property2java.parts.ReplaceLogicForTemplate.doReplacements; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + + import org.junit.jupiter.api.Test; public class ReplaceLogicForTemplateTest { - + @Test void testPlaceholderOnly() { String property = "{0}"; @@ -78,5 +81,5 @@ public class ReplaceLogicForTemplateTest { .hasMessageContaining("Position=5") // Index of missing Placeholder .hasMessageContaining(property); // Orignal Text } - + } diff --git a/src/test/java/de/kreth/property2java/processor/Property2JavaGeneratorTest.java b/src/test/java/de/kreth/property2java/processor/Property2JavaGeneratorTest.java index ef6f54a..59f5676 100644 --- a/src/test/java/de/kreth/property2java/processor/Property2JavaGeneratorTest.java +++ b/src/test/java/de/kreth/property2java/processor/Property2JavaGeneratorTest.java @@ -4,12 +4,12 @@ import static org.mockito.Mockito.when; import java.util.HashSet; import java.util.Set; - import javax.annotation.processing.Messager; import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.RoundEnvironment; import javax.lang.model.element.TypeElement; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith;