From 5985320b973911b1fff0977a8c12a10da7114871 Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Sun, 7 Jul 2024 23:06:09 +0200 Subject: [PATCH] Formatting --- .../de/kreth/property2java/Configuration.java | 87 ++++---- .../de/kreth/property2java/Generator.java | 202 +++++++++--------- .../cli/ArgumentConfiguration.java | 122 +++++------ .../de/kreth/property2java/cli/CliConfig.java | 6 +- .../config/FreemarkerConfig.java | 46 ++-- .../kreth/property2java/processor/Format.java | 40 ++-- .../processor/GenerateProperty2Java.java | 4 +- .../GenerateResourceBundleProperty2Java.java | 4 +- .../GenerateResourceBundleProperty2Javas.java | 4 +- .../processor/ProcessorConfiguration.java | 148 ++++++------- .../processor/Property2JavaGenerator.java | 96 ++++----- 11 files changed, 370 insertions(+), 389 deletions(-) diff --git a/src/main/java/de/kreth/property2java/Configuration.java b/src/main/java/de/kreth/property2java/Configuration.java index 0e19fdf..47e750d 100644 --- a/src/main/java/de/kreth/property2java/Configuration.java +++ b/src/main/java/de/kreth/property2java/Configuration.java @@ -16,50 +16,47 @@ import de.kreth.property2java.processor.Format; public interface Configuration { - /** - * Package for generated Java Classes eg. "de.kreth.property2java". If null - no - * package line is generated. - * - * @return - */ - String getPackage(); - - default Format getFormat() { - return Format.WithUnaryOperatorParameter; - } - - /** - * Filename to InputReader Entries - * - * @return - */ - Map getInput(); - - /** - * Path of java source folder. - * - * @return - */ - Path getRootPath(); - - default Writer outWriter(String fileName) throws IOException { - return new FileWriter(new File(getRootPath().toFile(), mapFilenameToClassName(fileName) + ".java"), - outputCharset()); - } - - default Charset outputCharset() { - return Charset.defaultCharset(); - } - - default String mapFilenameToClassName(String fileName) { - - String path = Regex.PATTERN.matcher(fileName) - .replaceAll(".") - .replaceAll("\\.", "_") - .replaceAll(" ", "_") - .replaceAll("/", "_"); - path = WordUtils.capitalize(path, '_'); - return path; - } + /** + * Package for generated Java Classes eg. "de.kreth.property2java". If null - no + * package line is generated. + * + * @return + */ + String getPackage(); + + default Format getFormat() { + return Format.WithUnaryOperatorParameter; + } + + /** + * Filename to InputReader Entries + * + * @return + */ + Map getInput(); + + /** + * Path of java source folder. + * + * @return + */ + Path getRootPath(); + + default Writer outWriter(String fileName) throws IOException { + return new FileWriter(new File(getRootPath().toFile(), mapFilenameToClassName(fileName) + ".java"), + outputCharset()); + } + + default Charset outputCharset() { + return Charset.defaultCharset(); + } + + default String mapFilenameToClassName(String fileName) { + + String path = Regex.PATTERN.matcher(fileName).replaceAll(".").replaceAll("\\.", "_").replaceAll(" ", "_") + .replaceAll("/", "_"); + path = WordUtils.capitalize(path, '_'); + return path; + } } diff --git a/src/main/java/de/kreth/property2java/Generator.java b/src/main/java/de/kreth/property2java/Generator.java index 9bb690b..2bb9d9f 100644 --- a/src/main/java/de/kreth/property2java/Generator.java +++ b/src/main/java/de/kreth/property2java/Generator.java @@ -22,143 +22,137 @@ import freemarker.template.TemplateException; public class Generator { - private static final DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(); + private static final DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(); - private final Configuration config; + private final Configuration config; - private final Template template; + private final Template template; - public Generator(Configuration config) { - this.config = config; - try { - template = FreemarkerConfig.INSTANCE.getTemplate(config.getFormat()); - } catch (IOException e) { - throw new IllegalStateException("Unable to load freemarker template", e); + public Generator(Configuration config) { + this.config = config; + try { + template = FreemarkerConfig.INSTANCE.getTemplate(config.getFormat()); + } catch (IOException e) { + throw new IllegalStateException("Unable to load freemarker template", e); + } } - } - public void start() throws IOException, GeneratorException { + public void start() throws IOException, GeneratorException { - for (Map.Entry entry : config.getInput().entrySet()) { - String fileName = entry.getKey(); - try (Writer out = config.outWriter(fileName)) { + for (Map.Entry entry : config.getInput().entrySet()) { + String fileName = entry.getKey(); + try (Writer out = config.outWriter(fileName)) { - Properties properties = new Properties(); - properties.load(entry.getValue()); - try { - generate(properties, out, fileName, config); - } catch (TemplateException e) { - throw new GeneratorException("Error configuring Engine", e); + Properties properties = new Properties(); + properties.load(entry.getValue()); + try { + generate(properties, out, fileName, config); + } catch (TemplateException e) { + throw new GeneratorException("Error configuring Engine", e); + } + } } - } } - } - void generate(Properties properties, Writer out, String fileName, Configuration config) - throws IOException, TemplateException { + void generate(Properties properties, Writer out, String fileName, Configuration config) + throws IOException, TemplateException { - Map root = new HashMap<>(); - root.put("generator_name", getClass().getName()); - root.put("generation_date", dateTimeInstance.format(new Date())); - root.put("package", config.getPackage()); - root.put("fileName", fileName); - root.put("bundle_base_name", fileName.substring(0, min(fileName.length(), fileName.lastIndexOf('.')))); - root.put("classname", config.mapFilenameToClassName(fileName)); + Map root = new HashMap<>(); + root.put("generator_name", getClass().getName()); + root.put("generation_date", dateTimeInstance.format(new Date())); + root.put("package", config.getPackage()); + root.put("fileName", fileName); + root.put("bundle_base_name", fileName.substring(0, min(fileName.length(), fileName.lastIndexOf('.')))); + root.put("classname", config.mapFilenameToClassName(fileName)); - List entries = new ArrayList<>(); + List entries = new ArrayList<>(); - root.put("entries", entries); + root.put("entries", entries); - @SuppressWarnings("unchecked") - List propertyNames = Collections.list((Enumeration) properties.propertyNames()); - Collections.sort(propertyNames); + @SuppressWarnings("unchecked") + List propertyNames = Collections.list((Enumeration) properties.propertyNames()); + Collections.sort(propertyNames); - for (String propertyKeyString : propertyNames) { - final String propertyValue = properties.getProperty(propertyKeyString); + for (String propertyKeyString : propertyNames) { + final String propertyValue = properties.getProperty(propertyKeyString); - entries.add(new Entry(propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_"), propertyKeyString, - propertyValue)); + entries.add(new Entry(propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_"), propertyKeyString, + propertyValue)); + } + template.process(root, out); } - template.process(root, out); - } - int min(int a, int b) { - int result = Math.min(a, b); - if (result < 0) { - result = Math.max(a, b); + int min(int a, int b) { + int result = Math.min(a, b); + if (result < 0) { + result = Math.max(a, b); + } + return result; } - return result; - } - - public static void main(String[] args) throws IOException, GeneratorException { - Generator generator = new Generator(ArgumentConfiguration.parse(args)); - generator.start(); - } - public static void generateFor(Class locationClass, List 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); + public static void main(String[] args) throws IOException, GeneratorException { + Generator generator = new Generator(ArgumentConfiguration.parse(args)); + generator.start(); + } - Generator g = new Generator(config.build()); - g.start(); - } + public static void generateFor(Class locationClass, List rescources, String relativeTargetDir) + throws IOException, GeneratorException { - /** - * Represents an Property Entry for the generated java class. - * - * @author markus - * - */ - public class Entry { + ArgumentConfiguration.Builder config = new ArgumentConfiguration.Builder(); - public final String constant; + rescources.stream().map(URL::getFile).map(File::new).map(File::getAbsolutePath).forEach(config::addPropFile); - public final String key; + config.setPackageName(locationClass.getPackageName()).setTarget(relativeTargetDir); - public final String value; + Generator g = new Generator(config.build()); + g.start(); + } /** - * Creates Property Entry data for the generated java class. + * Represents an Property Entry for the generated java class. + * + * @author markus * - * @param constant name for the created constant. - * @param key property key - * @param value property value */ - public Entry(String constant, String key, String value) { - super(); - this.constant = constant; - this.key = key; - this.value = value; - } + public class Entry { + + public final String constant; + + public final String key; + + public final String value; + + /** + * Creates Property Entry data for the generated java class. + * + * @param constant name for the created constant. + * @param key property key + * @param value property 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 getConstant() { + return constant; + } - public String getKey() { - return key; - } + public String getKey() { + return key; + } - public String getValue() { - return value; - } + public String getValue() { + return value; + } - @Override - public String toString() { - return "Entry [constant=" + constant + ", key=" + key + ", value=" + value + "]"; - } + @Override + public String toString() { + return "Entry [constant=" + constant + ", key=" + key + ", value=" + value + "]"; + } - } + } } diff --git a/src/main/java/de/kreth/property2java/cli/ArgumentConfiguration.java b/src/main/java/de/kreth/property2java/cli/ArgumentConfiguration.java index ce13603..c8d8b7c 100644 --- a/src/main/java/de/kreth/property2java/cli/ArgumentConfiguration.java +++ b/src/main/java/de/kreth/property2java/cli/ArgumentConfiguration.java @@ -16,81 +16,81 @@ import de.kreth.property2java.Configuration; public class ArgumentConfiguration implements Configuration { - private final String packageName; + private final String packageName; - private final Map files; + private final Map files; - private final Path rootPath; + private final Path rootPath; - private ArgumentConfiguration(Builder builder) throws IOException { - this.packageName = builder.packageName; - rootPath = new File(builder.target).toPath(); - files = new HashMap<>(); - for (String filePath : builder.propFiles) { - File f = new File(filePath); - files.put(f.getName(), new FileReader(f)); - } + private ArgumentConfiguration(Builder builder) throws IOException { + this.packageName = builder.packageName; + rootPath = new File(builder.target).toPath(); + files = new HashMap<>(); + for (String filePath : builder.propFiles) { + File f = new File(filePath); + files.put(f.getName(), new FileReader(f)); + } - } - - @Override - public String getPackage() { - return packageName; - } - - @Override - public Map getInput() { - return files; - } - - @Override - public Path getRootPath() { - return rootPath; - } - - @Override - public Writer outWriter(String fileName) throws IOException { - File dir; - if (packageName != null && packageName.isBlank() == false) { - dir = new File(rootPath.toFile(), packageName.replace('.', File.separatorChar)); - } else { - dir = rootPath.toFile(); } - return new FileWriter(new File(dir, mapFilenameToClassName(fileName) + ".java"), false); - } - public static Configuration parse(String[] args) throws IOException { - CliConfig cliConfig = new CliConfig(); + @Override + public String getPackage() { + return packageName; + } - Builder builder = new Builder(); - cliConfig.fill(builder, args); - return builder.build(); - } + @Override + public Map getInput() { + return files; + } - public static class Builder { - String target; + @Override + public Path getRootPath() { + return rootPath; + } - List propFiles = new ArrayList<>(); + @Override + public Writer outWriter(String fileName) throws IOException { + File dir; + if (packageName != null && packageName.isBlank() == false) { + dir = new File(rootPath.toFile(), packageName.replace('.', File.separatorChar)); + } else { + dir = rootPath.toFile(); + } + return new FileWriter(new File(dir, mapFilenameToClassName(fileName) + ".java"), false); + } - String packageName; + public static Configuration parse(String[] args) throws IOException { + CliConfig cliConfig = new CliConfig(); - public Builder setTarget(String target) { - this.target = target; - return this; + Builder builder = new Builder(); + cliConfig.fill(builder, args); + return builder.build(); } - public Builder addPropFile(String propFile) { - this.propFiles.add(propFile); - return this; - } + public static class Builder { + String target; - public Builder setPackageName(String packageName) { - this.packageName = packageName; - return this; - } + List propFiles = new ArrayList<>(); + + String packageName; + + public Builder setTarget(String target) { + this.target = target; + return this; + } + + public Builder addPropFile(String propFile) { + this.propFiles.add(propFile); + return this; + } + + public Builder setPackageName(String packageName) { + this.packageName = packageName; + return this; + } - public Configuration build() throws IOException { - return new ArgumentConfiguration(this); + public Configuration build() throws IOException { + return new ArgumentConfiguration(this); + } } - } } diff --git a/src/main/java/de/kreth/property2java/cli/CliConfig.java b/src/main/java/de/kreth/property2java/cli/CliConfig.java index 6906fc5..a5a12d3 100644 --- a/src/main/java/de/kreth/property2java/cli/CliConfig.java +++ b/src/main/java/de/kreth/property2java/cli/CliConfig.java @@ -35,12 +35,10 @@ public class CliConfig { for (String value : cmd.getOptionValues("f")) { builder.addPropFile(value); } - } - catch (MissingOptionException e) { + } catch (MissingOptionException e) { printHelp(); throw new IllegalStateException(e); - } - catch (ParseException e) { + } catch (ParseException e) { throw new IOException("Unable to parse Arguments", e); } } diff --git a/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java b/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java index f994279..557396a 100644 --- a/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java +++ b/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java @@ -8,30 +8,30 @@ 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."); - } + 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"); - } + private FreemarkerConfig() { + cfg = new Configuration(Configuration.VERSION_2_3_28); + cfg.setClassForTemplateLoading(this.getClass(), "/template/"); + cfg.setDefaultEncoding("UTF-8"); + } } diff --git a/src/main/java/de/kreth/property2java/processor/Format.java b/src/main/java/de/kreth/property2java/processor/Format.java index 659b579..12a80dd 100644 --- a/src/main/java/de/kreth/property2java/processor/Format.java +++ b/src/main/java/de/kreth/property2java/processor/Format.java @@ -4,24 +4,24 @@ import java.util.PropertyResourceBundle; public enum Format { - /** - * Offers a getString(UnaryOperator 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 resourceFunction) method to offer a - * getText() method. The init method must be called before any getText() call. - */ - WithInitializer + /** + * Offers a getString(UnaryOperator 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 resourceFunction) method to offer a + * getText() method. The init method must be called before any getText() call. + */ + WithInitializer } diff --git a/src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java b/src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java index 358deb2..fcc2409 100644 --- a/src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java +++ b/src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java @@ -36,7 +36,7 @@ import java.lang.annotation.Target; * */ public @interface GenerateProperty2Java { - String[] resources(); + String[] resources(); - Format format() default Format.WithUnaryOperatorParameter; + Format format() default Format.WithUnaryOperatorParameter; } diff --git a/src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Java.java b/src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Java.java index 69c7275..765c3b8 100644 --- a/src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Java.java +++ b/src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Java.java @@ -40,7 +40,7 @@ import java.lang.annotation.Target; * */ public @interface GenerateResourceBundleProperty2Java { - String resource(); + String resource(); - Format format(); + Format format(); } diff --git a/src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Javas.java b/src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Javas.java index d879f60..84af0bf 100644 --- a/src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Javas.java +++ b/src/main/java/de/kreth/property2java/processor/GenerateResourceBundleProperty2Javas.java @@ -15,7 +15,7 @@ import java.lang.annotation.Target; */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) -public @interface GenerateResourceBundleProperty2Javas { +@interface GenerateResourceBundleProperty2Javas { - GenerateResourceBundleProperty2Java[] value(); + GenerateResourceBundleProperty2Java[] value(); } diff --git a/src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java b/src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java index a6f5de2..6ef0eef 100644 --- a/src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java +++ b/src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java @@ -24,93 +24,93 @@ import de.kreth.property2java.GeneratorException; public class ProcessorConfiguration implements Configuration { - private final Filer filer; - private final Element element; - private final Map 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)); + private final Filer filer; + private final Element element; + private final Map 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() { + @Override + public String getPackage() { - String packageName = ""; - if (element instanceof TypeElement) { - TypeElement typeElement = (TypeElement) element; - PackageElement packageElement = (PackageElement) typeElement.getEnclosingElement(); - packageName = packageElement.getQualifiedName().toString(); + String packageName = ""; + if (element instanceof TypeElement) { + TypeElement typeElement = (TypeElement) element; + PackageElement packageElement = (PackageElement) typeElement.getEnclosingElement(); + packageName = packageElement.getQualifiedName().toString(); + } + return packageName; } - return packageName; - } - - @Override - public Format getFormat() { - return format; - } - - @Override - public Map 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 resourcenames; - private Format format = Format.WithUnaryOperatorParameter; + @Override + public Format getFormat() { + return format; + } - private Builder(Filer filer, Element element) { - this.filer = filer; - this.element = element; - this.resourcenames = new ArrayList<>(); + @Override + public Map getInput() { + return input; } - public Builder withFormat(Format format) { - this.format = format; - return this; + @Override + public Path getRootPath() { + throw new UnsupportedOperationException( + "For Annotation Processor this is not supported as outWriter is overwritten."); } - public Builder addAll(String[] resourceNames) { - this.resourcenames.addAll(Arrays.asList(resourceNames)); - return this; + @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(); } - public Builder addAll(List resourceNames) { - this.resourcenames.addAll(resourceNames); - return this; + static Builder builder(Filer filer, Element element) { + return new Builder(filer, element); } - public void startGeneration() throws IOException, GeneratorException { - Generator g = new Generator(new ProcessorConfiguration(this)); - g.start(); + static class Builder { + private final Filer filer; + private final Element element; + private final List 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 resourceNames) { + this.resourcenames.addAll(resourceNames); + return this; + } + + public void startGeneration() throws IOException, GeneratorException { + Generator g = new Generator(new ProcessorConfiguration(this)); + g.start(); + } } - } } diff --git a/src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java b/src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java index 6460644..de55ee1 100644 --- a/src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java +++ b/src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java @@ -19,73 +19,65 @@ 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" }) + "de.kreth.property2java.processor.GenerateResourceBundleProperty2Javas", + "de.kreth.property2java.processor.GenerateResourceBundleProperty2Java" }) @SupportedSourceVersion(SourceVersion.RELEASE_8) public class Property2JavaGenerator extends AbstractProcessor { - @Override - public boolean process(Set annotations, RoundEnvironment roundEnv) { + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { - if (!roundEnv.processingOver()) { - processGenerateProperty2Java(roundEnv); - processGenerateResourceBundleProperty2Javas(roundEnv); - } else { - processingEnv.getMessager().printMessage(Kind.NOTE, - "finished working on annotation " + annotations); + if (!roundEnv.processingOver()) { + processGenerateProperty2Java(roundEnv); + processGenerateResourceBundleProperty2Javas(roundEnv); + } else { + processingEnv.getMessager().printMessage(Kind.NOTE, "finished working on annotation " + annotations); + } + return true; } - return true; - } - private void processGenerateProperty2Java(RoundEnvironment roundEnv) { - processingEnv.getMessager().printMessage(Kind.NOTE, - "Processing annotation " + GenerateProperty2Java.class); + private void processGenerateProperty2Java(RoundEnvironment roundEnv) { + processingEnv.getMessager().printMessage(Kind.NOTE, "Processing annotation " + GenerateProperty2Java.class); - Set elementsAnnotatedWith = roundEnv - .getElementsAnnotatedWith(GenerateProperty2Java.class); + Set 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); + 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) { + private void processGenerateResourceBundleProperty2Javas(RoundEnvironment roundEnv) { - processingEnv.getMessager().printMessage(Kind.NOTE, - "Processing annotation " + GenerateResourceBundleProperty2Javas.class); + processingEnv.getMessager().printMessage(Kind.NOTE, + "Processing annotation " + GenerateResourceBundleProperty2Javas.class); - Set elementsAnnotatedWith = roundEnv - .getElementsAnnotatedWith(GenerateResourceBundleProperty2Javas.class); + Set elementsAnnotatedWith = roundEnv + .getElementsAnnotatedWith(GenerateResourceBundleProperty2Javas.class); - for (Element element : elementsAnnotatedWith) { - GenerateResourceBundleProperty2Java[] value = element - .getAnnotation(GenerateResourceBundleProperty2Javas.class).value(); - for (GenerateResourceBundleProperty2Java generateResourceBundleProperty2Java : value) { - List resources = Arrays.asList(generateResourceBundleProperty2Java.resource()); - generateElementProperties(element, resources, generateResourceBundleProperty2Java.format()); - } + for (Element element : elementsAnnotatedWith) { + GenerateResourceBundleProperty2Java[] value = element + .getAnnotation(GenerateResourceBundleProperty2Javas.class).value(); + for (GenerateResourceBundleProperty2Java generateResourceBundleProperty2Java : value) { + List resources = Arrays.asList(generateResourceBundleProperty2Java.resource()); + generateElementProperties(element, resources, generateResourceBundleProperty2Java.format()); + } + } } - } - private void generateElementProperties(Element element, List 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); + private void generateElementProperties(Element element, List 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); + } } - } }