This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2f22c70e605e285888cbbc1472561650ec529840 Author: Otavio Rodolfo Piske <[email protected]> AuthorDate: Thu Apr 14 11:28:44 2022 +0200 CAMEL-17894: cache known Java classes Avoid a complex flow that only loads certain Java classes after they thrown multiple NoClassDefFoundError. This should provide a small improvement to the build time --- .../maven/packaging/AbstractGeneratorMojo.java | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGeneratorMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGeneratorMojo.java index 457ecefb4d7..07e00ce7e95 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGeneratorMojo.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/AbstractGeneratorMojo.java @@ -22,7 +22,11 @@ import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; +import java.time.Duration; import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; import java.util.function.Supplier; import org.apache.camel.tooling.util.FileUtil; @@ -44,6 +48,8 @@ public abstract class AbstractGeneratorMojo extends AbstractMojo { public static final String GENERATED_MSG = "Generated by camel build tools - do NOT edit this file!"; public static final String NL = "\n"; + private static final Map<String, Class<?>> KNOWN_CLASSES_CACHE = new HashMap<>(); + /** * The maven project. */ @@ -64,6 +70,23 @@ public abstract class AbstractGeneratorMojo extends AbstractMojo { private DynamicClassLoader projectClassLoader; + static { + KNOWN_CLASSES_CACHE.put("Byte", Byte.class); + KNOWN_CLASSES_CACHE.put("Boolean", Boolean.class); + KNOWN_CLASSES_CACHE.put("Date", Date.class); + KNOWN_CLASSES_CACHE.put("Double", Double.class); + KNOWN_CLASSES_CACHE.put("Duration", Duration.class); + KNOWN_CLASSES_CACHE.put("String", String.class); + KNOWN_CLASSES_CACHE.put("Integer", Integer.class); + KNOWN_CLASSES_CACHE.put("Long", Long.class); + KNOWN_CLASSES_CACHE.put("File", File.class); + KNOWN_CLASSES_CACHE.put("Object", Object.class); + KNOWN_CLASSES_CACHE.put("int", int.class); + KNOWN_CLASSES_CACHE.put("long", long.class); + KNOWN_CLASSES_CACHE.put("boolean", boolean.class); + + } + public void execute( MavenProject project, MavenProjectHelper projectHelper, @@ -165,6 +188,15 @@ public abstract class AbstractGeneratorMojo extends AbstractMojo { } protected Class<?> loadClass(String loadClassName) { + final Class<?> ret = KNOWN_CLASSES_CACHE.get(loadClassName); + if (ret != null) { + return ret; + } + + return doLoadClass(loadClassName); + } + + private Class<?> doLoadClass(String loadClassName) { Class<?> optionClass; String org = loadClassName; while (true) { @@ -174,9 +206,16 @@ public abstract class AbstractGeneratorMojo extends AbstractMojo { } catch (ClassNotFoundException e) { int dotIndex = loadClassName.lastIndexOf('.'); if (dotIndex == -1) { + if (getLog().isDebugEnabled()) { + getLog().debug("Failed to load class: " + loadClassName); + } + throw new NoClassDefFoundError(org); } else { loadClassName = loadClassName.substring(0, dotIndex) + "$" + loadClassName.substring(dotIndex + 1); + if (getLog().isDebugEnabled()) { + getLog().debug("Relocating previous class name for loading as: " + loadClassName); + } } } }
