davsclaus commented on code in PR #26611:
URL: https://github.com/apache/camel/pull/26611#discussion_r4052692940


##########
core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java:
##########
@@ -2645,6 +2647,48 @@ private void setAiObservabilityProperties(
         }
     }
 
+    /**
+     * Creates a bean declared as <tt>#class:</tt> whose class has no public 
no-arg constructor but a builder (such as a
+     * LangChain4j model or a Lombok class), by setting the properties of the 
bean (dot style) on the builder before the
+     * bean is built, as such a bean cannot be configured after it is created.
+     *
+     * @return the created bean, or <tt>null</tt> if the bean is not created 
via an inferred builder
+     */
+    private static Object createBeanViaInferredBuilder(
+            CamelContext camelContext, String name, Object value, 
OrderedLocationProperties properties,
+            String optionPrefix, boolean failIfNotSet, boolean ignoreCase,
+            OrderedLocationProperties autoConfiguredProperties)
+            throws Exception {
+        if (!(value instanceof String text) || !text.startsWith("#class:")) {
+            return null;
+        }
+        String className = 
camelContext.resolvePropertyPlaceholders(text.substring(7));
+        if (className.indexOf('#') != -1 || className.indexOf('(') != -1) {
+            // a factory method or constructor arguments say how to create the 
bean
+            return null;
+        }
+        Class<?> type = 
camelContext.getClassResolver().resolveMandatoryClass(className);
+        Object builder = PropertyBindingSupport.newBuilderInstance(type);
+        if (builder == null) {
+            return null;
+        }
+        String bm = PropertyBindingSupport.findBuilderMethod(builder, type, 
null);
+        OrderedLocationProperties config = 
MainHelper.extractProperties(properties, name + ".");
+        if (!config.isEmpty()) {
+            // the properties are set on the builder (and reported as 
configured on the bean)
+            try {
+                MainHelper.setPropertiesOnTarget(camelContext, builder, 
config, optionPrefix + name + ".", failIfNotSet,
+                        ignoreCase, autoConfiguredProperties);
+            } catch (PropertyBindingException e) {
+                // the property names of a builder are not those of the bean, 
so name what the builder accepts
+                throw new IllegalArgumentException(
+                        e.getMessage() + ". " + 
BeanModelHelper.builderPropertiesHint(builder, type), e);

Review Comment:
   Good catch, fixed in 64218f0f2928: the builder is bound non-mandatory, what 
is left goes on the created bean with the configured fail-fast (setter-only 
properties work, unknown ones are reported with the builder hint, or ignored 
when not fail-fast). MainBeansInferredBuilderTest now covers the setter-only 
property (label) and the non-fail-fast case.



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