Compare commits
43 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 @@ |
|||||||
|
.classpath |
||||||
\.settings/ |
.project |
||||||
|
.factorypath |
||||||
|
.settings |
||||||
target |
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 |
||||||
@ -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); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -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); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -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); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue