This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit ac1c9c1e289fa824a6a334c89da92b60b86023a4 Author: Claus Ibsen <[email protected]> AuthorDate: Sun Jun 26 14:53:53 2022 +0200 CAMEL-18229: camel-maven-plugin - GenerateConfigurer to support builder pattern --- .../packaging/AbstractGenerateConfigurerMojo.java | 45 ++++++++++++++++++---- .../packaging/PropertyConfigurerGenerator.java | 14 ++++--- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGenerateConfigurerMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGenerateConfigurerMojo.java index 13da6783601..3d428cd3b53 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGenerateConfigurerMojo.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGenerateConfigurerMojo.java @@ -73,6 +73,18 @@ public abstract class AbstractGenerateConfigurerMojo extends AbstractGeneratorMo @Parameter(defaultValue = "true") protected boolean discoverClasses = true; + /** + * Whether to also allow using fluent builder style as configurer (getXXX and withXXX style). + */ + @Parameter(defaultValue = "false") + protected boolean allowBuilderPattern; + + /** + * Whether to skip deprecated methods. + */ + @Parameter(defaultValue = "false") + protected boolean skipDeprecated; + @Component private ArtifactFactory artifactFactory; @@ -80,7 +92,9 @@ public abstract class AbstractGenerateConfigurerMojo extends AbstractGeneratorMo public static class ConfigurerOption extends BaseOptionModel { - public ConfigurerOption(String name, Class type, String getter) { + private boolean builderMethod; + + public ConfigurerOption(String name, Class type, String getter, boolean builderMethod) { // we just use name, type setName(name); if (byte[].class == type) { @@ -97,8 +111,12 @@ public abstract class AbstractGenerateConfigurerMojo extends AbstractGeneratorMo setJavaType(type.getName()); } setGetterMethod(getter); + this.builderMethod = builderMethod; } + public boolean isBuilderMethod() { + return builderMethod; + } } public AbstractGenerateConfigurerMojo() { @@ -325,12 +343,21 @@ public abstract class AbstractGenerateConfigurerMojo extends AbstractGeneratorMo Class clazz = projectClassLoader.loadClass(fqn); // find all public setters doWithMethods(clazz, m -> { + boolean deprecated = m.isAnnotationPresent(Deprecated.class); + if (skipDeprecated && deprecated) { + return; + } + boolean setter = m.getName().length() >= 4 && m.getName().startsWith("set") && Character.isUpperCase(m.getName().charAt(3)); setter &= Modifier.isPublic(m.getModifiers()) && m.getParameterCount() == 1; setter &= filterSetter(m); - if (setter) { - String getter = "get" + Character.toUpperCase(m.getName().charAt(3)) + m.getName().substring(4); + boolean builder = allowBuilderPattern && m.getName().length() >= 5 && m.getName().startsWith("with") + && Character.isUpperCase(m.getName().charAt(4)); + builder &= Modifier.isPublic(m.getModifiers()) && m.getParameterCount() == 1; + builder &= filterSetter(m); + if (setter || builder) { + String getter = "get" + (builder ? Character.toUpperCase(m.getName().charAt(4)) + m.getName().substring(5) : Character.toUpperCase(m.getName().charAt(3)) + m.getName().substring(4)); Class type = m.getParameterTypes()[0]; if (boolean.class == type || Boolean.class == type) { try { @@ -343,9 +370,11 @@ public abstract class AbstractGenerateConfigurerMojo extends AbstractGeneratorMo } ConfigurerOption option = null; - String t = Character.toUpperCase(m.getName().charAt(3)) + m.getName().substring(3 + 1); + String t = builder + ? Character.toUpperCase(m.getName().charAt(4)) + m.getName().substring(4 + 1) + : Character.toUpperCase(m.getName().charAt(3)) + m.getName().substring(3 + 1); if (names.add(t)) { - option = new ConfigurerOption(t, type, getter); + option = new ConfigurerOption(t, type, getter, builder); answer.add(option); } else { boolean replace = false; @@ -357,7 +386,7 @@ public abstract class AbstractGenerateConfigurerMojo extends AbstractGeneratorMo } if (replace) { answer.removeIf(o -> o.getName().equals(t)); - option = new ConfigurerOption(t, type, getter); + option = new ConfigurerOption(t, type, getter, builder); answer.add(option); } } @@ -378,8 +407,8 @@ public abstract class AbstractGenerateConfigurerMojo extends AbstractGeneratorMo } desc = desc.replace('$', '.'); desc = desc.trim(); - // skip if the type is generic or a wildcard - if (!desc.isEmpty() && desc.indexOf('?') == -1 && !desc.contains(" extends ")) { + // skip if the type is generic, or a wildcard (a single letter is regarded as unknown) + if (desc.length() > 1 && desc.indexOf('?') == -1 && !desc.contains(" extends ")) { option.setNestedType(desc); } } diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PropertyConfigurerGenerator.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PropertyConfigurerGenerator.java index 9b0e10466c3..32851a42695 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PropertyConfigurerGenerator.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PropertyConfigurerGenerator.java @@ -102,8 +102,10 @@ public final class PropertyConfigurerGenerator { for (BaseOptionModel option : options) { String getOrSet = option.getName(); getOrSet = Character.toUpperCase(getOrSet.charAt(0)) + getOrSet.substring(1); + boolean builder = option instanceof AbstractGenerateConfigurerMojo.ConfigurerOption + && ((AbstractGenerateConfigurerMojo.ConfigurerOption) option).isBuilderMethod(); String setterLambda = setterLambda(getOrSet, option.getJavaType(), option.getSetterMethod(), - option.getConfigurationField(), component, option.getType()); + option.getConfigurationField(), component, option.getType(), builder); if (!option.getName().toLowerCase().equals(option.getName())) { w.append(String.format(" case \"%s\":\n", option.getName().toLowerCase())); } @@ -337,26 +339,26 @@ public final class PropertyConfigurerGenerator { private static String setterLambda( String getOrSet, String type, String setterMethod, String configurationField, boolean component, - String optionKind) { + String optionKind, boolean builder) { // type may contain generics so remove those if (type.indexOf('<') != -1) { type = type.substring(0, type.indexOf('<')); } type = type.replace('$', '.'); + String prefix = builder ? "with" : "set"; if (configurationField != null) { if (component) { String methodName = "getOrCreate" + Character.toUpperCase(configurationField.charAt(0)) + configurationField.substring(1); - getOrSet = methodName + "(target).set" + getOrSet; + getOrSet = methodName + "(target)." + prefix + getOrSet; } else { getOrSet = "target.get" + Character.toUpperCase(configurationField.charAt(0)) + configurationField.substring(1) - + "().set" + getOrSet; + + "()." + prefix + getOrSet; } } else { - getOrSet = "target.set" + getOrSet; + getOrSet = "target." + prefix + getOrSet; } - // target.setGroupSize(property(camelContext, java.lang.Integer.class, value)) String rv; if ("duration".equals(optionKind) && "long".equals(type)) { rv = "property(camelContext, java.time.Duration.class, value).toMillis()";
