Annotation Processor for the generator

master
Markus Kreth 3 years ago
parent 11f18a9abf
commit 1d447b2045
  1. 22
      .classpath
  2. 1
      META-INF/services/javax.annotation.processing.Processor
  3. 33
      pom.xml
  4. 12
      src/main/java/de/kreth/property2java/Configuration.java
  5. 30
      src/main/java/de/kreth/property2java/Generator.java
  6. 5
      src/main/java/de/kreth/property2java/cli/ArgumentConfiguration.java
  7. 40
      src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java
  8. 105
      src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java
  9. 61
      src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java
  10. 1
      src/main/resources/META-INF/services/javax.annotation.processing.Processor
  11. 4
      src/test/java/de/kreth/property2java/GeneratorTests.java
  12. 46
      src/test/java/de/kreth/property2java/processor/Property2JavaGeneratorTest.java

@ -6,7 +6,7 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> <classpathentry kind="src" output="target/classes" path="src/main/resources">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
@ -15,22 +15,34 @@
<attributes> <attributes>
<attribute name="test" value="true"/> <attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> <classpathentry exported="true" kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes> <attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry exported="true" kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"> <classpathentry kind="src" path="target/generated-sources/annotations">
<attributes> <attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>

@ -0,0 +1 @@
de.kreth.property2java.processor.Property2JavaGenerator

@ -14,12 +14,14 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version> <java.version>11</java.version>
<org.junit.jupiter>5.3.0-M1</org.junit.jupiter> <org.junit.jupiter>5.3.0-M1</org.junit.jupiter>
<org.slf4j>1.7.21</org.slf4j> <org.slf4j>1.7.36</org.slf4j>
<org.apache.logging.log4j>2.17.1</org.apache.logging.log4j> <org.apache.logging.log4j>2.17.2</org.apache.logging.log4j>
<timestamp>${maven.build.timestamp}</timestamp> <timestamp>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format> <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
<auto-service.version>1.0-rc2</auto-service.version>
<sonar.login>3f3a1fa86ea83226b564895f3a8503f67e855440</sonar.login> <sonar.login>3f3a1fa86ea83226b564895f3a8503f67e855440</sonar.login>
<sonar.jacoco.reportPaths>target/surefire-reports</sonar.jacoco.reportPaths> <sonar.jacoco.reportPaths>target/surefire-reports</sonar.jacoco.reportPaths>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin> <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
@ -136,34 +138,9 @@
<version>3.8.0</version> <version>3.8.0</version>
<configuration> <configuration>
<release>${java.version}</release> <release>${java.version}</release>
<compilerArgs>-proc:none</compilerArgs>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>default-jacoco-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-jacoco-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>

@ -16,19 +16,23 @@ import de.kreth.property2java.config.Regex;
public interface Configuration { public interface Configuration {
/** /**
* Package for generated Java Classes eg. "de.kreth.property2java". If null - no package line is generated. * Package for generated Java Classes eg. "de.kreth.property2java". If null - no
* package line is generated.
*
* @return * @return
*/ */
String getPackage(); String getPackage();
/** /**
* Filename to InputReader Entries * Filename to InputReader Entries
*
* @return * @return
*/ */
Map<String, Reader> getInput(); Map<String, Reader> getInput();
/** /**
* Path of java source folder. * Path of java source folder.
*
* @return * @return
*/ */
Path getRootPath(); Path getRootPath();
@ -44,7 +48,11 @@ public interface Configuration {
default String mapFilenameToClassName(String fileName) { default String mapFilenameToClassName(String fileName) {
String path = Regex.PATTERN.matcher(fileName).replaceAll(".").replaceAll("\\.", "_").replaceAll(" ", "_"); String path = Regex.PATTERN.matcher(fileName)
.replaceAll(".")
.replaceAll("\\.", "_")
.replaceAll(" ", "_")
.replaceAll("/", "_");
path = WordUtils.capitalize(path, '_'); path = WordUtils.capitalize(path, '_');
return path; return path;
} }

@ -1,8 +1,10 @@
package de.kreth.property2java; package de.kreth.property2java;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.Writer; import java.io.Writer;
import java.net.URL;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -30,8 +32,7 @@ public class Generator {
this.config = config; this.config = config;
try { try {
template = FreemarkerConfig.INSTANCE.getTemplate(); template = FreemarkerConfig.INSTANCE.getTemplate();
} } catch (IOException e) {
catch (IOException e) {
throw new IllegalStateException("Unable to load freemarker template", e); throw new IllegalStateException("Unable to load freemarker template", e);
} }
} }
@ -46,8 +47,7 @@ public class Generator {
properties.load(entry.getValue()); properties.load(entry.getValue());
try { try {
generate(properties, out, fileName, config); generate(properties, out, fileName, config);
} } catch (TemplateException e) {
catch (TemplateException e) {
throw new GeneratorException("Error configuring Engine", e); throw new GeneratorException("Error configuring Engine", e);
} }
} }
@ -86,8 +86,28 @@ public class Generator {
generator.start(); generator.start();
} }
public static void generateFor(Class<?> locationClass, List<URL> rescources, String relativeTargetDir)
throws IOException, GeneratorException {
ArgumentConfiguration.Builder config = new ArgumentConfiguration.Builder();
rescources
.stream()
.map(URL::getFile)
.map(File::new)
.map(File::getAbsolutePath)
.forEach(config::addPropFile);
config.setPackageName(locationClass.getPackageName())
.setTarget(relativeTargetDir);
Generator g = new Generator(config.build());
g.start();
}
/** /**
* Represents an Property Entry for the generated java class. * Represents an Property Entry for the generated java class.
*
* @author markus * @author markus
* *
*/ */
@ -101,6 +121,7 @@ public class Generator {
/** /**
* Creates Property Entry data for the generated java class. * Creates Property Entry data for the generated java class.
*
* @param constant name for the created constant. * @param constant name for the created constant.
* @param key property key * @param key property key
* @param value property value * @param value property value
@ -130,4 +151,5 @@ public class Generator {
} }
} }
} }

@ -53,8 +53,7 @@ public class ArgumentConfiguration implements Configuration {
File dir; File dir;
if (packageName != null && packageName.isBlank() == false) { if (packageName != null && packageName.isBlank() == false) {
dir = new File(rootPath.toFile(), packageName.replace('.', File.separatorChar)); dir = new File(rootPath.toFile(), packageName.replace('.', File.separatorChar));
} } else {
else {
dir = rootPath.toFile(); dir = rootPath.toFile();
} }
return new FileWriter(new File(dir, mapFilenameToClassName(fileName) + ".java"), false); return new FileWriter(new File(dir, mapFilenameToClassName(fileName) + ".java"), false);
@ -68,7 +67,7 @@ public class ArgumentConfiguration implements Configuration {
return builder.build(); return builder.build();
} }
static class Builder { public static class Builder {
String target; String target;
List<String> propFiles = new ArrayList<>(); List<String> propFiles = new ArrayList<>();

@ -0,0 +1,40 @@
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>
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;version&gt;3.8.0&lt;/version&gt;
&lt;configuration&gt;
&lt;release&gt;${java.version}&lt;/release&gt;
<b>&lt;showWarnings&gt;true&lt;/showWarnings&gt;</b>
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
* </pre>
*
* @author Markus
*
*/
public @interface GenerateProperty2Java {
String[] resources();
}

@ -0,0 +1,105 @@
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 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;
ProcessorConfiguration(Builder builder) throws IOException {
this.filer = builder.filer;
this.element = builder.element;
this.input = new HashMap<>();
for (String resource : builder.resourcenames) {
FileObject ressource = filer.getResource(StandardLocation.CLASS_PATH, "",
resource);
String className = mapFilenameToClassName(resource);
input.put(className, 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 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 + "." + 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 Builder(Filer filer, Element element) {
this.filer = filer;
this.element = element;
this.resourcenames = new ArrayList<>();
}
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,61 @@
package de.kreth.property2java.processor;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
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" })
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class Property2JavaGenerator extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (!roundEnv.processingOver()) {
processingEnv.getMessager().printMessage(Kind.NOTE,
"Processing annotation " + GenerateProperty2Java.class);
Set<? extends Element> elementsAnnotatedWith = roundEnv
.getElementsAnnotatedWith(GenerateProperty2Java.class);
processElements(elementsAnnotatedWith);
} else {
processingEnv.getMessager().printMessage(Kind.NOTE,
"finished working on annotation " + GenerateProperty2Java.class);
}
return true;
}
private void processElements(Set<? extends Element> elementsAnnotatedWith) {
for (Element element : elementsAnnotatedWith) {
String[] resources = element.getAnnotation(GenerateProperty2Java.class).resources();
processingEnv.getMessager().printMessage(Kind.NOTE,
"Generating Java for " + Arrays.asList(resources));
try {
ProcessorConfiguration
.builder(processingEnv.getFiler(), element)
.addAll(resources)
.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

@ -69,8 +69,7 @@ class GeneratorTests {
String line = sourceTokenizer.nextToken(); String line = sourceTokenizer.nextToken();
if (line.trim().startsWith("package")) { if (line.trim().startsWith("package")) {
linePackage = line; linePackage = line;
} } else if (line.trim().startsWith("public enum")) {
else if (line.trim().startsWith("public enum")) {
lineClass = line; lineClass = line;
} }
if (line.contains("{")) { if (line.contains("{")) {
@ -131,6 +130,7 @@ class GeneratorTests {
private void assertLineMatch(List<String> lines, String key, String expected) { private void assertLineMatch(List<String> lines, String key, String expected) {
Optional<String> found = lines.stream().filter(line -> keyMatches(line, key)) Optional<String> found = lines.stream().filter(line -> keyMatches(line, key))
.findFirst(); .findFirst();
assertTrue(found.isPresent(), "No line found with key = " + key); assertTrue(found.isPresent(), "No line found with key = " + key);
final String line = found.get().trim(); final String line = found.get().trim();
int indexEquals = line.indexOf('('); int indexEquals = line.indexOf('(');

@ -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…
Cancel
Save