Compare commits
50 Commits
REL-BRANCH
...
master
@ -1,37 +0,0 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<classpath> |
||||
<classpathentry kind="src" output="target/classes" path="src/main/java"> |
||||
<attributes> |
||||
<attribute name="optional" value="true"/> |
||||
<attribute name="maven.pomderived" value="true"/> |
||||
</attributes> |
||||
</classpathentry> |
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> |
||||
<attributes> |
||||
<attribute name="maven.pomderived" value="true"/> |
||||
</attributes> |
||||
</classpathentry> |
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> |
||||
<attributes> |
||||
<attribute name="test" value="true"/> |
||||
<attribute name="maven.pomderived" value="true"/> |
||||
</attributes> |
||||
</classpathentry> |
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> |
||||
<attributes> |
||||
<attribute name="test" value="true"/> |
||||
<attribute name="maven.pomderived" value="true"/> |
||||
</attributes> |
||||
</classpathentry> |
||||
<classpathentry exported="true" kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> |
||||
<attributes> |
||||
<attribute name="maven.pomderived" value="true"/> |
||||
</attributes> |
||||
</classpathentry> |
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"> |
||||
<attributes> |
||||
<attribute name="maven.pomderived" value="true"/> |
||||
</attributes> |
||||
</classpathentry> |
||||
<classpathentry kind="output" path="target/classes"/> |
||||
</classpath> |
||||
@ -0,0 +1,24 @@ |
||||
# This workflow will build a Java project with Maven |
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven |
||||
|
||||
name: PropertyToJavaGenerator Library and console programm |
||||
|
||||
on: |
||||
push: |
||||
branches: [ master ] |
||||
pull_request: |
||||
branches: [ master ] |
||||
|
||||
jobs: |
||||
build: |
||||
|
||||
runs-on: ubuntu-latest |
||||
|
||||
steps: |
||||
- uses: actions/checkout@v2 |
||||
- name: Set up JDK 1.8 |
||||
uses: actions/setup-java@v1 |
||||
with: |
||||
java-version: 11 |
||||
- name: Build with Maven |
||||
run: mvn -B package --file pom.xml |
||||
@ -1,3 +1,5 @@ |
||||
|
||||
\.settings/ |
||||
.classpath |
||||
.project |
||||
.factorypath |
||||
.settings |
||||
target |
||||
|
||||
@ -1,23 +0,0 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<projectDescription> |
||||
<name>PropertyToJavaGenerator</name> |
||||
<comment></comment> |
||||
<projects> |
||||
</projects> |
||||
<buildSpec> |
||||
<buildCommand> |
||||
<name>org.eclipse.jdt.core.javabuilder</name> |
||||
<arguments> |
||||
</arguments> |
||||
</buildCommand> |
||||
<buildCommand> |
||||
<name>org.eclipse.m2e.core.maven2Builder</name> |
||||
<arguments> |
||||
</arguments> |
||||
</buildCommand> |
||||
</buildSpec> |
||||
<natures> |
||||
<nature>org.eclipse.jdt.core.javanature</nature> |
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature> |
||||
</natures> |
||||
</projectDescription> |
||||
@ -0,0 +1 @@ |
||||
de.kreth.property2java.processor.Property2JavaGenerator |
||||
@ -1,35 +0,0 @@ |
||||
package de.kreth.property2java; |
||||
|
||||
public class Entry { |
||||
|
||||
public final String constant; |
||||
|
||||
public final String key; |
||||
|
||||
public final String value; |
||||
|
||||
public Entry(String constant, String key, String value) { |
||||
super(); |
||||
this.constant = constant; |
||||
this.key = key; |
||||
this.value = value; |
||||
} |
||||
|
||||
public String getConstant() { |
||||
return constant; |
||||
} |
||||
|
||||
public String getKey() { |
||||
return key; |
||||
} |
||||
|
||||
public String getValue() { |
||||
return value; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "Entry [constant=" + constant + ", key=" + key + ", value=" + value + "]"; |
||||
} |
||||
|
||||
} |
||||
@ -1,5 +1,37 @@ |
||||
package de.kreth.property2java.config; |
||||
|
||||
public interface FreemarkerConfig { |
||||
import java.io.IOException; |
||||
|
||||
import de.kreth.property2java.processor.Format; |
||||
import freemarker.template.Configuration; |
||||
import freemarker.template.Template; |
||||
|
||||
public enum FreemarkerConfig { |
||||
|
||||
INSTANCE; |
||||
|
||||
private final Configuration cfg; |
||||
|
||||
public Template getTemplate(Format format) throws IOException { |
||||
switch (format) { |
||||
case WithInitializer: |
||||
return cfg.getTemplate("enum_template_with_initializer.tpl"); |
||||
case WithInnerPropertyLoader: |
||||
return cfg.getTemplate("enum_template_with_inner_properties.tpl"); |
||||
case WithInnerPropertyResourceBundle: |
||||
return cfg.getTemplate("enum_template_with_inner_propertyresourcebundle.tpl"); |
||||
case WithUnaryOperatorParameter: |
||||
return cfg.getTemplate("enum_template.tpl"); |
||||
default: |
||||
throw new IllegalArgumentException("Format " + format + " is not supported."); |
||||
} |
||||
|
||||
} |
||||
|
||||
private FreemarkerConfig() { |
||||
cfg = new Configuration(Configuration.VERSION_2_3_28); |
||||
cfg.setClassForTemplateLoading(this.getClass(), "/template/"); |
||||
cfg.setDefaultEncoding("UTF-8"); |
||||
} |
||||
|
||||
} |
||||
|
||||
@ -1,24 +0,0 @@ |
||||
package de.kreth.property2java.config; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
import freemarker.template.Configuration; |
||||
import freemarker.template.Template; |
||||
|
||||
public enum FreemarkerConfigImpl implements FreemarkerConfig { |
||||
|
||||
INSTANCE; |
||||
|
||||
private final Configuration cfg; |
||||
|
||||
public Template getTemplate() throws IOException { |
||||
return cfg.getTemplate("enum_template.tpl"); |
||||
} |
||||
|
||||
private FreemarkerConfigImpl() { |
||||
cfg = new Configuration(Configuration.VERSION_2_3_28); |
||||
cfg.setClassForTemplateLoading(this.getClass(), "/template/"); |
||||
cfg.setDefaultEncoding("UTF-8"); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,27 @@ |
||||
package de.kreth.property2java.processor; |
||||
|
||||
import java.util.PropertyResourceBundle; |
||||
|
||||
public enum Format { |
||||
|
||||
/** |
||||
* Offers a getString(UnaryOperator<String> resourceFunction) method to access |
||||
* the String value |
||||
*/ |
||||
WithUnaryOperatorParameter, |
||||
/** |
||||
* Generates {@link PropertyResourceBundle} to offer a getText() method without |
||||
* parameters. |
||||
*/ |
||||
WithInnerPropertyResourceBundle, |
||||
/** |
||||
* Offers a generated {@link PropertyResourceBundle} to offer a getText() method |
||||
* without parameters. |
||||
*/ |
||||
WithInnerPropertyLoader, |
||||
/** |
||||
* Offers a static init(UnaryOperator<String> resourceFunction) method to offer |
||||
* a getText() method. The init method must be called before any getText() call. |
||||
*/ |
||||
WithInitializer |
||||
} |
||||
@ -0,0 +1,42 @@ |
||||
package de.kreth.property2java.processor; |
||||
|
||||
import static java.lang.annotation.ElementType.TYPE; |
||||
|
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
@Target(TYPE) |
||||
@Retention(RetentionPolicy.SOURCE) |
||||
/** |
||||
* Für die konfigurierten Resourcen wird jeweils eine Java Klasse erzeugt. Es |
||||
* muss nur die Abhängigkeit eingebunden werden und die Annotation in einer |
||||
* Klasse verwendet werden, in deren Package die neuen Klassen generiert werden. |
||||
* |
||||
* Für die Ausgabe der Prozessornachrichten muss folgendes im maven compiler |
||||
* konfiguriert werden: |
||||
* |
||||
* <pre> |
||||
<build> |
||||
<plugins> |
||||
<plugin> |
||||
<groupId>org.apache.maven.plugins</groupId> |
||||
<artifactId>maven-compiler-plugin</artifactId> |
||||
<version>3.8.0</version> |
||||
<configuration> |
||||
<release>${java.version}</release> |
||||
<b><showWarnings>true</showWarnings></b> |
||||
</configuration> |
||||
</plugin> |
||||
</plugins> |
||||
</build> |
||||
* </pre> |
||||
* |
||||
* @author Markus |
||||
* |
||||
*/ |
||||
public @interface GenerateProperty2Java { |
||||
String[] resources(); |
||||
|
||||
Format format() default Format.WithUnaryOperatorParameter; |
||||
} |
||||
@ -0,0 +1,46 @@ |
||||
package de.kreth.property2java.processor; |
||||
|
||||
import static java.lang.annotation.ElementType.TYPE; |
||||
|
||||
import java.lang.annotation.Repeatable; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
@Target(TYPE) |
||||
@Retention(RetentionPolicy.SOURCE) |
||||
@Repeatable(value = GenerateResourceBundleProperty2Javas.class) |
||||
/** |
||||
* Für die konfigurierten Resourcen wird jeweils eine Java Klasse erzeugt. Es |
||||
* muss nur die Abhängigkeit eingebunden werden und die Annotation in einer |
||||
* Klasse verwendet werden, in deren Package die neuen Klassen generiert werden. |
||||
* Wenn mehrere Resourcen verarbeitet werden sollen, kann diese Annotation |
||||
* mehrfach parallel angegeben werden. |
||||
* |
||||
* Für die Ausgabe der Prozessornachrichten muss folgendes im maven compiler |
||||
* konfiguriert werden: |
||||
* |
||||
* <pre> |
||||
<build> |
||||
<plugins> |
||||
<plugin> |
||||
<groupId>org.apache.maven.plugins</groupId> |
||||
<artifactId>maven-compiler-plugin</artifactId> |
||||
<version>3.8.0</version> |
||||
<configuration> |
||||
<release>${java.version}</release> |
||||
<b><showWarnings>true</showWarnings></b> |
||||
</configuration> |
||||
</plugin> |
||||
</plugins> |
||||
</build> |
||||
* </pre> |
||||
* |
||||
* @author Markus |
||||
* |
||||
*/ |
||||
public @interface GenerateResourceBundleProperty2Java { |
||||
String resource(); |
||||
|
||||
Format format(); |
||||
} |
||||
@ -0,0 +1,21 @@ |
||||
package de.kreth.property2java.processor; |
||||
|
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
/** |
||||
* Diese Annotation sollte nicht verwendet werden. Sie sammelt nur |
||||
* {@link GenerateResourceBundleProperty2Java} wenn diese mehrfach verwendet |
||||
* wird. |
||||
* |
||||
* @author Markus |
||||
* |
||||
*/ |
||||
@Target(ElementType.TYPE) |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
public @interface GenerateResourceBundleProperty2Javas { |
||||
|
||||
GenerateResourceBundleProperty2Java[] value(); |
||||
} |
||||
@ -0,0 +1,116 @@ |
||||
package de.kreth.property2java.processor; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.Reader; |
||||
import java.io.Writer; |
||||
import java.nio.file.Path; |
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
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; |
||||
import javax.lang.model.element.TypeElement; |
||||
import javax.tools.FileObject; |
||||
import javax.tools.StandardLocation; |
||||
|
||||
import de.kreth.property2java.Configuration; |
||||
import de.kreth.property2java.Generator; |
||||
import de.kreth.property2java.GeneratorException; |
||||
|
||||
public class ProcessorConfiguration implements Configuration { |
||||
|
||||
private final Filer filer; |
||||
private final Element element; |
||||
private final Map<String, Reader> input; |
||||
private final Format format; |
||||
|
||||
ProcessorConfiguration(Builder builder) throws IOException { |
||||
this.filer = builder.filer; |
||||
this.element = builder.element; |
||||
this.format = Objects.requireNonNullElse(builder.format, Format.WithUnaryOperatorParameter); |
||||
this.input = new HashMap<>(); |
||||
for (String resource : builder.resourcenames) { |
||||
FileObject ressource = filer.getResource(StandardLocation.CLASS_PATH, "", resource); |
||||
input.put(resource, ressource.openReader(false)); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public String getPackage() { |
||||
|
||||
String packageName = ""; |
||||
if (element instanceof TypeElement) { |
||||
TypeElement typeElement = (TypeElement) element; |
||||
PackageElement packageElement = (PackageElement) typeElement.getEnclosingElement(); |
||||
packageName = packageElement.getQualifiedName().toString(); |
||||
} |
||||
return packageName; |
||||
} |
||||
|
||||
@Override |
||||
public Format getFormat() { |
||||
return format; |
||||
} |
||||
|
||||
@Override |
||||
public Map<String, Reader> getInput() { |
||||
return input; |
||||
} |
||||
|
||||
@Override |
||||
public Path getRootPath() { |
||||
throw new UnsupportedOperationException( |
||||
"For Annotation Processor this is not supported as outWriter is overwritten."); |
||||
} |
||||
|
||||
@Override |
||||
public Writer outWriter(String fileName) throws IOException { |
||||
String packageName = getPackage(); |
||||
if (packageName != null && !packageName.isBlank()) { |
||||
fileName = packageName + "." + mapFilenameToClassName(fileName); |
||||
} |
||||
return filer.createSourceFile(fileName, element).openWriter(); |
||||
} |
||||
|
||||
static Builder builder(Filer filer, Element element) { |
||||
return new Builder(filer, element); |
||||
} |
||||
|
||||
static class Builder { |
||||
private final Filer filer; |
||||
private final Element element; |
||||
private final List<String> resourcenames; |
||||
private Format format = Format.WithUnaryOperatorParameter; |
||||
|
||||
private Builder(Filer filer, Element element) { |
||||
this.filer = filer; |
||||
this.element = element; |
||||
this.resourcenames = new ArrayList<>(); |
||||
} |
||||
|
||||
public Builder withFormat(Format format) { |
||||
this.format = format; |
||||
return this; |
||||
} |
||||
|
||||
public Builder addAll(String[] resourceNames) { |
||||
this.resourcenames.addAll(Arrays.asList(resourceNames)); |
||||
return this; |
||||
} |
||||
|
||||
public Builder addAll(List<String> resourceNames) { |
||||
this.resourcenames.addAll(resourceNames); |
||||
return this; |
||||
} |
||||
|
||||
public void startGeneration() throws IOException, GeneratorException { |
||||
Generator g = new Generator(new ProcessorConfiguration(this)); |
||||
g.start(); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,91 @@ |
||||
package de.kreth.property2java.processor; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.PrintWriter; |
||||
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; |
||||
import javax.annotation.processing.SupportedSourceVersion; |
||||
import javax.lang.model.SourceVersion; |
||||
import javax.lang.model.element.Element; |
||||
import javax.lang.model.element.TypeElement; |
||||
import javax.tools.Diagnostic.Kind; |
||||
|
||||
import de.kreth.property2java.GeneratorException; |
||||
|
||||
@SupportedAnnotationTypes({ "de.kreth.property2java.processor.GenerateProperty2Java", |
||||
"de.kreth.property2java.processor.GenerateResourceBundleProperty2Javas", |
||||
"de.kreth.property2java.processor.GenerateResourceBundleProperty2Java" }) |
||||
@SupportedSourceVersion(SourceVersion.RELEASE_8) |
||||
public class Property2JavaGenerator extends AbstractProcessor { |
||||
|
||||
@Override |
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
||||
|
||||
if (!roundEnv.processingOver()) { |
||||
processGenerateProperty2Java(roundEnv); |
||||
processGenerateResourceBundleProperty2Javas(roundEnv); |
||||
} else { |
||||
processingEnv.getMessager().printMessage(Kind.NOTE, |
||||
"finished working on annotation " + annotations); |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
private void processGenerateProperty2Java(RoundEnvironment roundEnv) { |
||||
processingEnv.getMessager().printMessage(Kind.NOTE, |
||||
"Processing annotation " + GenerateProperty2Java.class); |
||||
|
||||
Set<? extends Element> elementsAnnotatedWith = roundEnv |
||||
.getElementsAnnotatedWith(GenerateProperty2Java.class); |
||||
|
||||
for (Element element : elementsAnnotatedWith) { |
||||
GenerateProperty2Java generateAnnotation = element.getAnnotation(GenerateProperty2Java.class); |
||||
String[] resources = generateAnnotation.resources(); |
||||
Format format = generateAnnotation.format(); |
||||
generateElementProperties(element, Arrays.asList(resources), format); |
||||
} |
||||
} |
||||
|
||||
private void processGenerateResourceBundleProperty2Javas(RoundEnvironment roundEnv) { |
||||
|
||||
processingEnv.getMessager().printMessage(Kind.NOTE, |
||||
"Processing annotation " + GenerateResourceBundleProperty2Javas.class); |
||||
|
||||
Set<? extends Element> elementsAnnotatedWith = roundEnv |
||||
.getElementsAnnotatedWith(GenerateResourceBundleProperty2Javas.class); |
||||
|
||||
for (Element element : elementsAnnotatedWith) { |
||||
GenerateResourceBundleProperty2Java[] value = element |
||||
.getAnnotation(GenerateResourceBundleProperty2Javas.class).value(); |
||||
for (GenerateResourceBundleProperty2Java generateResourceBundleProperty2Java : value) { |
||||
List<String> resources = Arrays.asList(generateResourceBundleProperty2Java.resource()); |
||||
generateElementProperties(element, resources, generateResourceBundleProperty2Java.format()); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private void generateElementProperties(Element element, List<String> resources, Format format) { |
||||
processingEnv.getMessager().printMessage(Kind.NOTE, |
||||
"Generating Java for " + Arrays.asList(resources)); |
||||
try { |
||||
ProcessorConfiguration |
||||
.builder(processingEnv.getFiler(), element) |
||||
.addAll(resources) |
||||
.withFormat(format) |
||||
.startGeneration(); |
||||
} catch (IOException | GeneratorException e) { |
||||
StringWriter out = new StringWriter(); |
||||
e.printStackTrace(new PrintWriter(out)); |
||||
out.flush(); |
||||
processingEnv.getMessager().printMessage(Kind.ERROR, "Exception " + e + "\n" + out.toString(), |
||||
element); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,152 +0,0 @@ |
||||
package de.kreth.property2java.replace; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
public class Replacement { |
||||
|
||||
private static final List<Character> CHARS_NOT_ALLOWED_BEFORE_PROPERTY = Arrays.asList('.'); |
||||
|
||||
private final String packageName; |
||||
|
||||
private final String simpleClassName; |
||||
|
||||
private final String oldPropertyName; |
||||
|
||||
private final String newPropertyName; |
||||
|
||||
private String qualifiedClassName; |
||||
|
||||
private String qualifiedOldProperty; |
||||
|
||||
public Replacement(String packageName, String simpleClassName, String oldPropertyName, String newPropertyName) { |
||||
super(); |
||||
this.packageName = packageName; |
||||
this.simpleClassName = simpleClassName; |
||||
this.oldPropertyName = oldPropertyName; |
||||
this.newPropertyName = newPropertyName; |
||||
this.qualifiedClassName = packageName + "." + simpleClassName; |
||||
this.qualifiedOldProperty = qualifiedClassName + "." + oldPropertyName; |
||||
} |
||||
|
||||
public String getOldPropertyName() { |
||||
return oldPropertyName; |
||||
} |
||||
|
||||
public String getNewPropertyName() { |
||||
return newPropertyName; |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
final int prime = 31; |
||||
int result = 1; |
||||
result = prime * result + ((newPropertyName == null) ? 0 : newPropertyName.hashCode()); |
||||
result = prime * result + ((oldPropertyName == null) ? 0 : oldPropertyName.hashCode()); |
||||
result = prime * result + ((packageName == null) ? 0 : packageName.hashCode()); |
||||
result = prime * result + ((simpleClassName == null) ? 0 : simpleClassName.hashCode()); |
||||
return result; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (this == obj) { |
||||
return true; |
||||
} |
||||
if (obj == null) { |
||||
return false; |
||||
} |
||||
if (getClass() != obj.getClass()) { |
||||
return false; |
||||
} |
||||
Replacement other = (Replacement) obj; |
||||
if (newPropertyName == null) { |
||||
if (other.newPropertyName != null) { |
||||
return false; |
||||
} |
||||
} |
||||
else if (!newPropertyName.equals(other.newPropertyName)) { |
||||
return false; |
||||
} |
||||
if (oldPropertyName == null) { |
||||
if (other.oldPropertyName != null) { |
||||
return false; |
||||
} |
||||
} |
||||
else if (!oldPropertyName.equals(other.oldPropertyName)) { |
||||
return false; |
||||
} |
||||
if (packageName == null) { |
||||
if (other.packageName != null) { |
||||
return false; |
||||
} |
||||
} |
||||
else if (!packageName.equals(other.packageName)) { |
||||
return false; |
||||
} |
||||
if (simpleClassName == null) { |
||||
if (other.simpleClassName != null) { |
||||
return false; |
||||
} |
||||
} |
||||
else if (!simpleClassName.equals(other.simpleClassName)) { |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "Replacement [packageName=" + packageName + ", simpleClassName=" + simpleClassName + ", oldPropertyName=" |
||||
+ oldPropertyName + ", newPropertyName=" + newPropertyName + "]"; |
||||
} |
||||
|
||||
public String replaceOccurrences(String sourceCode) { |
||||
|
||||
if (sourceCode.contains("import static " + qualifiedOldProperty)) { |
||||
sourceCode = replaceStaticImportedUsages(sourceCode); |
||||
} |
||||
if (sourceCode.contains(qualifiedOldProperty)) { |
||||
sourceCode = replaceFullQualifiedUnimported(sourceCode); |
||||
} |
||||
if (sourceCode.contains("import " + qualifiedClassName)) { |
||||
sourceCode = replaceClassUsages(sourceCode); |
||||
} |
||||
return sourceCode; |
||||
} |
||||
|
||||
private String replaceClassUsages(String sourceCode) { |
||||
String search = this.simpleClassName + "." + oldPropertyName; |
||||
String replacement = this.simpleClassName + "." + newPropertyName; |
||||
|
||||
int index = sourceCode.indexOf(search); |
||||
while (index > 0) { |
||||
if (!CHARS_NOT_ALLOWED_BEFORE_PROPERTY.contains(sourceCode.charAt(index - 1))) { |
||||
StringBuilder source = new StringBuilder(sourceCode); |
||||
source.replace(index, index + search.length(), replacement); |
||||
sourceCode = source.toString(); |
||||
} |
||||
index = sourceCode.indexOf(search, index + 1); |
||||
} |
||||
return sourceCode; |
||||
} |
||||
|
||||
private String replaceStaticImportedUsages(String sourceCode) { |
||||
int index = sourceCode.indexOf(oldPropertyName); |
||||
while (index > 0) { |
||||
if (!CHARS_NOT_ALLOWED_BEFORE_PROPERTY.contains(sourceCode.charAt(index - 1))) { |
||||
StringBuilder source = new StringBuilder(sourceCode); |
||||
source.replace(index, index + oldPropertyName.length(), newPropertyName); |
||||
sourceCode = source.toString(); |
||||
} |
||||
index = sourceCode.indexOf(oldPropertyName, index + 1); |
||||
} |
||||
return sourceCode; |
||||
} |
||||
|
||||
private String replaceFullQualifiedUnimported(String sourceCode) { |
||||
return sourceCode.replace(qualifiedOldProperty, |
||||
qualifiedClassName + "." + newPropertyName); |
||||
} |
||||
|
||||
} |
||||
@ -1,14 +0,0 @@ |
||||
package de.kreth.property2java.replace; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class Replacements { |
||||
|
||||
private final List<Replacement> replacementList = new ArrayList<>(); |
||||
|
||||
public boolean add(Replacement e) { |
||||
return replacementList.add(e); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1 @@ |
||||
de.kreth.property2java.processor.Property2JavaGenerator |
||||
@ -0,0 +1,62 @@ |
||||
<#if package??>package ${package}; |
||||
|
||||
</#if>import java.util.Properties; |
||||
import java.util.ResourceBundle; |
||||
import java.util.function.UnaryOperator; |
||||
|
||||
import javax.annotation.processing.Generated; |
||||
|
||||
/** |
||||
* Property keys from ${fileName} |
||||
* {@link #getValue()} gives the key for the entry, with {@link #getText()} the value for the key is given directly. |
||||
* This enum needs to be initialized before any use by {@link #init(UnaryOperator)}. |
||||
*/ |
||||
@Generated(date = "${generation_date}", value = "${generator_name}") |
||||
public enum ${classname} { |
||||
|
||||
<#list entries as e> |
||||
/** |
||||
* ${e.key} = "${e.value}" |
||||
*/ |
||||
${e.constant} ("${e.key}")<#sep>, |
||||
</#sep> |
||||
</#list>; |
||||
|
||||
private static UnaryOperator<String> function; |
||||
private final String value; |
||||
|
||||
private ${classname} (String value) { |
||||
this.value = value; |
||||
} |
||||
|
||||
public static void init(UnaryOperator<String> resourceFunction) { |
||||
function = resourceFunction; |
||||
} |
||||
|
||||
/** |
||||
* Represented Key in property File. |
||||
* @return key |
||||
*/ |
||||
public String getValue() { |
||||
return value; |
||||
} |
||||
|
||||
/** |
||||
* Resolves the value for this key. |
||||
* {@link #init(UnaryOperator<String>)} must be called before. |
||||
*/ |
||||
public String getText() { |
||||
return function.apply(value); |
||||
} |
||||
|
||||
/** |
||||
* Resolves the value for this key from the parameter function. |
||||
* <p> |
||||
* e.g. <code>${classname}.getString(resBundle::getString)</code> |
||||
* @param resourceFunction {@link Properties#getProperty(String)} or {@link ResourceBundle#getString(String)} |
||||
* @return |
||||
*/ |
||||
public String getString(UnaryOperator<String> resourceFunction) { |
||||
return resourceFunction.apply(value); |
||||
} |
||||
} |
||||
@ -0,0 +1,48 @@ |
||||
<#if package??>package ${package}; |
||||
|
||||
</#if>import java.util.Properties; |
||||
import java.util.PropertyResourceBundle; |
||||
import java.util.ResourceBundle; |
||||
import java.util.function.UnaryOperator; |
||||
|
||||
import javax.annotation.processing.Generated; |
||||
|
||||
/** |
||||
* Property keys from ${fileName} |
||||
*/ |
||||
@Generated(date = "${generation_date}", value = "${generator_name}") |
||||
public enum ${classname} { |
||||
|
||||
<#list entries as e> |
||||
/** |
||||
* ${e.key} = "${e.value}" |
||||
*/ |
||||
${e.constant} ("${e.key}")<#sep>, |
||||
</#sep> |
||||
</#list>; |
||||
|
||||
private final String value; |
||||
|
||||
private ${classname} (String value) { |
||||
this.value = value; |
||||
} |
||||
|
||||
/** |
||||
* Represented Key in property File. |
||||
* @return key |
||||
*/ |
||||
public String getValue() { |
||||
return value; |
||||
} |
||||
|
||||
private static ResourceBundle bundle = PropertyResourceBundle.getBundle("${bundle_base_name}"); |
||||
|
||||
/** |
||||
* The Text for this Key from PropertyResourceBundle |
||||
* @return human readable text |
||||
*/ |
||||
public String getText() { |
||||
return bundle.getString(value); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,50 @@ |
||||
<#if package??>package ${package}; |
||||
|
||||
</#if>import java.util.Properties; |
||||
import java.util.PropertyResourceBundle; |
||||
import java.util.ResourceBundle; |
||||
import java.util.function.UnaryOperator; |
||||
|
||||
import javax.annotation.processing.Generated; |
||||
|
||||
/** |
||||
* Property keys from ${fileName} |
||||
* {@link #getValue()} gives the key for the entry, with {@link #getText()} the value for the key is given directly. |
||||
* Initializationis generated also. |
||||
*/ |
||||
@Generated(date = "${generation_date}", value = "${generator_name}") |
||||
public enum ${classname} { |
||||
|
||||
<#list entries as e> |
||||
/** |
||||
* ${e.key} = "${e.value}" |
||||
*/ |
||||
${e.constant} ("${e.key}")<#sep>, |
||||
</#sep> |
||||
</#list>; |
||||
|
||||
private final String value; |
||||
|
||||
private ${classname} (String value) { |
||||
this.value = value; |
||||
} |
||||
|
||||
/** |
||||
* Represented Key in property File. |
||||
* @return key |
||||
*/ |
||||
public String getValue() { |
||||
return value; |
||||
} |
||||
|
||||
private static ResourceBundle bundle = PropertyResourceBundle.getBundle("${bundle_base_name}"); |
||||
|
||||
/** |
||||
* The Text for this Key from PropertyResourceBundle |
||||
* @return human readable text |
||||
*/ |
||||
public String getText() { |
||||
return bundle.getString(value); |
||||
} |
||||
|
||||
} |
||||
@ -1,55 +0,0 @@ |
||||
package de.kreth.property2java.cli; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.io.Reader; |
||||
import java.util.Map; |
||||
|
||||
import org.junit.jupiter.api.AfterAll; |
||||
import org.junit.jupiter.api.BeforeAll; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import de.kreth.property2java.Configuration; |
||||
|
||||
class ArgumentConfigurationTest { |
||||
|
||||
private static File target; |
||||
|
||||
private static File input; |
||||
|
||||
private static File input2; |
||||
|
||||
@BeforeAll |
||||
static void createTargetDir() throws IOException { |
||||
target = new File("testTargetDir"); |
||||
target.mkdir(); |
||||
input = new File(target, "application.properties"); |
||||
input.createNewFile(); |
||||
input2 = new File(target, "application2.properties"); |
||||
input2.createNewFile(); |
||||
} |
||||
|
||||
@AfterAll |
||||
static void deleteTestfiles() { |
||||
target.delete(); |
||||
input.delete(); |
||||
input2.delete(); |
||||
} |
||||
|
||||
@Test |
||||
void testArgumentParser() throws IOException { |
||||
|
||||
String[] args = { "-t", target.getAbsolutePath(), |
||||
"-f", |
||||
input.getAbsolutePath() + "," + input2.getAbsolutePath(), |
||||
"-p", "de.kreth.clubinvoice" }; |
||||
Configuration config = ArgumentConfiguration.parse(args); |
||||
assertEquals("de.kreth.clubinvoice", config.getPackage()); |
||||
assertEquals(target.getAbsolutePath(), config.getRootPath().toFile().getAbsolutePath()); |
||||
Map<String, Reader> inputMap = config.getInput(); |
||||
assertEquals(2, inputMap.size()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,46 @@ |
||||
package de.kreth.property2java.processor; |
||||
|
||||
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.mockito.ArgumentMatchers; |
||||
import org.mockito.Mock; |
||||
import org.mockito.MockitoAnnotations; |
||||
|
||||
public class Property2JavaGeneratorTest { |
||||
|
||||
private Property2JavaGenerator processor; |
||||
@Mock |
||||
private ProcessingEnvironment processingEnv; |
||||
@Mock |
||||
private RoundEnvironment roundEnv; |
||||
private Set<TypeElement> annotations; |
||||
@Mock |
||||
private Messager messanger; |
||||
|
||||
@BeforeEach |
||||
void initProcesor() { |
||||
MockitoAnnotations.initMocks(this); |
||||
annotations = new HashSet<>(); |
||||
|
||||
processor = new Property2JavaGenerator(); |
||||
processor.init(processingEnv); |
||||
when(processingEnv.getMessager()).thenReturn(messanger); |
||||
} |
||||
|
||||
@Test |
||||
void testGeneratorInitializedCorrectly() { |
||||
when(roundEnv.getElementsAnnotatedWith(ArgumentMatchers.any(Class.class))) |
||||
.thenReturn(annotations); |
||||
processor.process(annotations, roundEnv); |
||||
} |
||||
} |
||||
@ -1,163 +0,0 @@ |
||||
package de.kreth.property2java.replace; |
||||
|
||||
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 java.util.List; |
||||
import java.util.Optional; |
||||
import java.util.stream.Collectors; |
||||
|
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import de.kreth.property2java.replace.Replacement; |
||||
|
||||
class ReplacementTest { |
||||
|
||||
private Replacement captionArticles; |
||||
|
||||
private Replacement labelLogout; |
||||
|
||||
private Replacement captionUserDetails; |
||||
|
||||
@BeforeEach |
||||
void initTestItem() { |
||||
captionArticles = new Replacement("de.kreth.clubinvoice", "Application_Properties", "CAPTION_ARTICLES", |
||||
"CAPTION1_ARTICLES"); |
||||
captionUserDetails = new Replacement("de.kreth.clubinvoice", "Application_Properties", |
||||
"CAPTION_USER_DETAILS", |
||||
"CAPTION1_USER_DETAILS"); |
||||
labelLogout = new Replacement("de.kreth.clubinvoice", "Application_Properties", "LABEL_LOGOUT", |
||||
"LABEL1_LOGOUT"); |
||||
} |
||||
|
||||
@Test |
||||
void testReplaceStaticImported() { |
||||
|
||||
String exampleSourceCode = getExampleSourceCode(); |
||||
String result = captionArticles.replaceOccurrences(exampleSourceCode); |
||||
assertNotNull(result); |
||||
|
||||
Optional<String> firstLine = result.lines() |
||||
.filter(line -> line.contains("de.kreth.clubinvoice.Application_Properties")).findFirst(); |
||||
assertTrue(firstLine.isPresent()); |
||||
|
||||
String captionArticlesLine = firstLine.get(); |
||||
|
||||
assertEquals("import static de.kreth.clubinvoice.Application_Properties.CAPTION1_ARTICLES;", |
||||
captionArticlesLine.trim()); |
||||
|
||||
firstLine = result.lines().filter(l -> l.contains("new Button(CAPTION1_ARTICLES.getString(")).findFirst(); |
||||
assertTrue(firstLine.isPresent(), "CAPTION1_ARTICLES usage not found"); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
void testReplaceClassImport() { |
||||
|
||||
String exampleSourceCode = getExampleSourceCode(); |
||||
String result = labelLogout.replaceOccurrences(exampleSourceCode); |
||||
|
||||
Optional<String> line = result.lines() |
||||
.filter(l -> l.contains(".getString(Application_Properties.LABEL1_LOGOUT")) |
||||
.findFirst(); |
||||
assertTrue(line.isPresent()); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
void testReplaceUnimportedOccurrence() { |
||||
|
||||
String exampleSourceCode = getExampleSourceCode(); |
||||
String result = captionUserDetails.replaceOccurrences(exampleSourceCode); |
||||
Optional<String> line = result.lines() |
||||
.filter(l -> l.contains("new Button(de.kreth.clubinvoice.Application_Properties") |
||||
&& l.contains("_USER_DETAILS")) |
||||
.findFirst(); |
||||
assertTrue(line.isPresent()); |
||||
|
||||
assertTrue(line.get().contains("de.kreth.clubinvoice.Application_Properties.CAPTION1_USER_DETAILS"), |
||||
"absoulute class field CAPTION1_USER_DETAILS not found in : " + line.get()); |
||||
} |
||||
|
||||
@Test |
||||
void testReplaceUnimportedOccurrenceWithLinebreak() { |
||||
|
||||
String exampleSourceCode = getExampleSourceCode(); |
||||
String result = captionUserDetails.replaceOccurrences(exampleSourceCode); |
||||
List<String> lines = result.lines() |
||||
.filter(l -> l.contains(".Application_Properties") |
||||
&& l.contains("_USER_DETAILS")) |
||||
.collect(Collectors.toList()); |
||||
assertEquals(2, lines.size()); |
||||
//
|
||||
// assertTrue(line.get().contains("de.kreth.clubinvoice.Application_Properties.CAPTION1_USER_DETAILS"),
|
||||
// "absoulute class field CAPTION1_USER_DETAILS not found in : " + line.get());
|
||||
} |
||||
|
||||
String getExampleSourceCode() { |
||||
return "package de.kreth.clubinvoice.ui;\r\n" + |
||||
"\r\n" + |
||||
"import static de.kreth.clubinvoice.Application_Properties.CAPTION_ARTICLES;\r\n" + |
||||
"import static de.kreth.clubinvoice.Application_Properties.CAPTION_INVOICEITEM_ADD;\r\n" + |
||||
"" + |
||||
"import static de.kreth.clubinvoice.Application_Properties.CAPTION_INVOICE_PATTERN;\r\n" + |
||||
"\r\n" + |
||||
"import org.slf4j.Logger;\r\n" + |
||||
"import org.slf4j.LoggerFactory;\r\n" + |
||||
"\r\n" + |
||||
"\r\n" + |
||||
"import de.kreth.clubinvoice.Application_Properties;\r\n" + |
||||
"\r\n" + |
||||
"public class OverviewUi extends VerticalLayout implements InvoiceUi {\r\n" + |
||||
"\r\n" + |
||||
" public void setContent(UI ui, VaadinRequest vaadinRequest) {\r\n" + |
||||
"\r\n" + |
||||
" }\r\n" + |
||||
"\r\n" + |
||||
" public VerticalLayout createInvoicesView(final UI ui) {\r\n" + |
||||
"\r\n" + |
||||
" createInvoice = new Button(CAPTION_INVOICE_CREATE.getString(resBundle::getString));\r\n" + |
||||
" return right;\r\n" + |
||||
" }\r\n" + |
||||
"\r\n" + |
||||
" public VerticalLayout createItemsView(final UI ui) {\r\n" + |
||||
"\r\n" + |
||||
" addItem = new Button(de.kreth.clubinvoice.Application_Properties.CAPTION_INVOICE_CREATE.getString(resBundle::getString));\r\n" |
||||
+ |
||||
"\r\n" + |
||||
" VerticalLayout left = new VerticalLayout();\r\n" + |
||||
" addItem = new Button(de.kreth.clubinvoice" |
||||
+ " \t.Application_Properties.CAPTION_INVOICE_CREATE.getString(resBundle::getString));\r\n" + |
||||
" left.addComponents(addItem, gridItems);\r\n" + |
||||
" left.setStyleName(STYLE_BORDERED);\r\n" + |
||||
" return left;\r\n" + |
||||
" }\r\n" + |
||||
"\r\n" + |
||||
" public HorizontalLayout createHeadView(final UI ui, VaadinRequest vaadinRequest) {\r\n" + |
||||
" Label l2 = new Label(String.format(\"%s %s\", user.getPrename(), user.getSurname()));\r\n" + |
||||
"\r\n" + |
||||
" Button addArticle = new Button(CAPTION_ARTICLES.getString(resBundle::getString));\r\n" + |
||||
" Button logoutButton = new Button(resBundle.getString(Application_Properties.LABEL_LOGOUT.getValue()));\r\n" |
||||
+ |
||||
" logoutButton.addClickListener(ev -> {\r\n" + |
||||
" LOGGER.warn(\"Logging out.\");\r\n" + |
||||
" logout(ui, vaadinRequest);\r\n" + |
||||
" });\r\n" + |
||||
"\r\n" + |
||||
" Button userDetail = new Button(de.kreth.clubinvoice.Application_Properties.CAPTION_USER_DETAILS.getString(resBundle::getString), ev -> {\r\n" |
||||
+ |
||||
" Button userDetail2 = new Button(de.kreth.clubinvoice" |
||||
+ " \t.Application_Properties.CAPTION_USER_DETAILS.getString(resBundle::getString), ev -> {\r\n" |
||||
+ |
||||
" showUserDetailDialog(ui);\r\n" + |
||||
" });\r\n" + |
||||
"\r\n" + |
||||
" return head;\r\n" + |
||||
" }\r\n" + |
||||
"\r\n" + |
||||
"}\r\n" + |
||||
""; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue