OpenRewriteRecipeBestPractices

master
Markus Kreth 4 months ago
parent f8d3595afe
commit ade0b73919
  1. 1
      Application_Properties.java
  2. 7
      pom.xml
  3. 4
      src/main/java/de/kreth/property2java/Configuration.java
  4. 2
      src/main/java/de/kreth/property2java/Generator.java
  5. 4
      src/main/java/de/kreth/property2java/GeneratorOptions.java
  6. 1
      src/main/java/de/kreth/property2java/cli/ArgumentConfiguration.java
  7. 4
      src/main/java/de/kreth/property2java/cli/CliConfig.java
  8. 2
      src/main/java/de/kreth/property2java/config/FreemarkerConfig.java
  9. 1
      src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java
  10. 3
      src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Java.java
  11. 6
      src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java
  12. 2
      src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java
  13. 1
      src/test/java/de/kreth/property2java/ConfigurationTest.java
  14. 8
      src/test/java/de/kreth/property2java/GeneratorTests.java
  15. 22
      src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java
  16. 7
      src/test/java/de/kreth/property2java/TestPropertiesSource.java
  17. 14
      src/test/java/de/kreth/property2java/parts/ReplaceLogicForTemplate.java
  18. 9
      src/test/java/de/kreth/property2java/parts/ReplaceLogicForTemplateTest.java
  19. 2
      src/test/java/de/kreth/property2java/processor/Property2JavaGeneratorTest.java

@ -162,17 +162,24 @@
<recipe>org.openrewrite.staticanalysis.JavaApiBestPractices</recipe>
<recipe>org.openrewrite.staticanalysis.CommonStaticAnalysis</recipe>
<recipe>org.openrewrite.staticanalysis.CodeCleanup</recipe>
<recipe>org.openrewrite.recipes.rewrite.OpenRewriteRecipeBestPractices</recipe>
</activeRecipes>
<exclusions>
<exclusion>**/generated/**</exclusion>
</exclusions>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-migrate-java</artifactId>
<version>3.14.1</version>
</dependency>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-rewrite</artifactId>
<version>0.5.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>

@ -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<GeneratorOptions> getOptions() {
return EnumSet.noneOf(GeneratorOptions.class);
}
default Writer outWriter(String fileName) throws IOException {
return new FileWriter(new File(getRootPath().toFile(), mapFilenameToClassName(fileName) + ".java"),
outputCharset());

@ -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;

@ -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;
}

@ -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 {

@ -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();

@ -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;

@ -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;

@ -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 {};
}

@ -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<GeneratorOptions> 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;

@ -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;

@ -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;

@ -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 {

@ -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 {

@ -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);
}
}
}

@ -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) {

@ -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
}
}

@ -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;

Loading…
Cancel
Save