gnodet-bot commented on code in PR #26709:
URL: https://github.com/apache/camel/pull/26709#discussion_r4066274893


##########
core/camel-support/src/main/java/org/apache/camel/support/compile/BindToRegistryCompilePostProcessor.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.support.compile;
+
+import java.util.function.Supplier;
+
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.CamelConfiguration;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Configuration;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.spi.CamelBeanPostProcessor;
+import org.apache.camel.spi.CompilePostProcessor;
+import org.apache.camel.support.PluginHelper;
+import org.apache.camel.util.ObjectHelper;
+
+/**
+ * Binds a compiled class annotated with {@link BindToRegistry} or {@link 
Configuration} (or that is a
+ * {@link CamelConfiguration}) to the registry, using the {@link 
CamelBeanPostProcessor} so the Camel dependency
+ * injection annotations on the class are processed as well. As the class may 
be compiled again (live reload) the
+ * previous bean is unbound first.
+ */
+public class BindToRegistryCompilePostProcessor implements 
CompilePostProcessor {
+
+    private final boolean lazyBean;
+
+    public BindToRegistryCompilePostProcessor() {
+        this(false);
+    }
+
+    /**
+     * @param lazyBean whether to bind every {@link BindToRegistry} bean 
lazily (created on first use), as if
+     *                 {@link BindToRegistry#lazy()} was set
+     */
+    public BindToRegistryCompilePostProcessor(boolean lazyBean) {
+        this.lazyBean = lazyBean;
+    }
+
+    @Override
+    public void postCompile(CamelContext camelContext, String name, Class<?> 
clazz, byte[] byteCode, Object instance)
+            throws Exception {
+
+        BindToRegistry bir = clazz.getAnnotation(BindToRegistry.class);
+        Configuration cfg = clazz.getAnnotation(Configuration.class);
+
+        // special for lazy beans which we must create on-demand
+        if (instance == null && bir != null && (lazyBean || bir.lazy())) {
+            final String beanName = bir.value();

Review Comment:
   ⚠️ **Empty `bir.value()` in lazy path — wrong bean unbound on live reload**
   
   `bir.value()` defaults to `""` when `@BindToRegistry` is used without an 
explicit name. When `beanName` is `""`, the `unbind(beanName)` call on line 75 
unregisters the empty-string key (a no-op in practice), not the old bean that 
was bound under the class name. On the next reload, the previous lazy supplier 
stays in the registry and the new one is added alongside it.
   
   `PackageScanHelper` handles the same case explicitly:
   ```java
   // PackageScanHelper.java:87
   if (isEmpty(name)) {
       name = c.getName();
   }
   String beanName = c.getName();
   ```
   
   Same fix needed here:
   
   ```suggestion
               final String beanName = ObjectHelper.isNotEmpty(bir.value()) ? 
bir.value() : clazz.getName();
   ```
   
   And on line 75, the `unbind` already uses `beanName`, so it will 
automatically unbind the right key after this fix.



-- 
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]

Reply via email to