davsclaus commented on code in PR #26709:
URL: https://github.com/apache/camel/pull/26709#discussion_r4066124592
##########
components/camel-groovy/src/main/java/org/apache/camel/language/groovy/DefaultGroovyScriptCompiler.java:
##########
@@ -68,6 +71,7 @@ public class DefaultGroovyScriptCompiler extends
ServiceSupport
private static final Logger LOG =
LoggerFactory.getLogger(DefaultGroovyScriptCompiler.class);
private GroovyPreCompiledClassLoader groovyPreCompiledClassLoader;
+ private List<CompilePostProcessor> defaultPostProcessors;
Review Comment:
Suggest making this eager and `final`: the three processors are trivial to
construct, it removes the null-check branch in `postCompile`, and it avoids the
(edge) case of two `EventNotifierCompilePostProcessor` instances if a startup
compile and a reload compile ever overlap — the second one would not know about
the notifier the first one added, so it would not be removed on the next reload.
```suggestion
private final List<CompilePostProcessor> defaultPostProcessors = List.of(
new TypeConverterCompilePostProcessor(),
new EventNotifierCompilePostProcessor(),
new BindToRegistryCompilePostProcessor());
```
##########
components/camel-groovy/src/main/java/org/apache/camel/language/groovy/DefaultGroovyScriptCompiler.java:
##########
@@ -374,21 +378,29 @@ private void doCompileCode(Map<String, String> codes,
List<String> cps) throws E
}
/**
- * Runs the registered {@link CompilePostProcessor}s on a compiled class,
as the Java DSL loader does for
- * {@code .java} sources, so annotations such as {@link BindToRegistry}
and {@link org.apache.camel.Converter} (and
- * the Spring and Quarkus equivalents camel-jbang registers) work in
Groovy sources as well. On a recompile (live
- * reload) the bean is created and bound again, replacing the previous one.
+ * Runs the {@link CompilePostProcessor}s on a compiled class, as the Java
DSL loader does for {@code .java}
+ * sources, so annotations such as {@link BindToRegistry} and {@link
org.apache.camel.Converter} work in Groovy
+ * sources as well. The processors in the registry are used when there are
any (camel-jbang registers processors
+ * that also handle the Spring and Quarkus annotations); otherwise the
built-in processors for the Camel annotations
+ * are used, so a Groovy source works the same in every runtime. On a
recompile (live reload) the bean is created
+ * and bound again, replacing the previous one.
*/
private void postCompile(Class<?> clazz, byte[] byteCode) throws Exception
{
- Set<CompilePostProcessor> posts =
camelContext.getRegistry().findByType(CompilePostProcessor.class);
- if (posts == null || posts.isEmpty()) {
- return;
- }
// only annotated classes are instantiated: a plain groovy class or
script is a DTO or a
// function library, and creating it here would only run its
constructor for nothing
if (clazz.getAnnotations().length == 0 ||
Script.class.isAssignableFrom(clazz)) {
return;
}
+ Collection<CompilePostProcessor> posts =
camelContext.getRegistry().findByType(CompilePostProcessor.class);
+ if (posts == null || posts.isEmpty()) {
+ if (defaultPostProcessors == null) {
+ defaultPostProcessors = List.of(
+ new TypeConverterCompilePostProcessor(),
+ new EventNotifierCompilePostProcessor(),
+ new BindToRegistryCompilePostProcessor());
+ }
+ posts = defaultPostProcessors;
+ }
Review Comment:
…and with the field initialised eagerly this collapses to:
```suggestion
if (posts == null || posts.isEmpty()) {
posts = defaultPostProcessors;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]