package picocli.spring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import picocli.CommandLine;

import java.util.Objects;

/**
 * PicocliSpringFactory is a {@link picocli.CommandLine.IFactory} implementation that
 * looks up classes in a given {@code org.springframework.context.ApplicationContext}.
 * This allows picocli subcommands (and other objects that are instantiated as needed)
 * to have {@code javax.inject} annotations that will be populated
 * by Spring's dependency injection framework.
 *
 * @author Thibaud Leprêtre
 * @since 4.0.0
 */
public class PicocliSpringFactory implements CommandLine.IFactory {
    private static final Logger logger = LoggerFactory.getLogger(PicocliSpringFactory.class);

    private final ApplicationContext applicationContext;
    private final CommandLine.IFactory fallbackFactory;

    /**
     * Constructs a PicocliSpringFactory with the specified Application context,
     * and picocli's {@link CommandLine#defaultFactory() default factory} as the fallback factory
     * for classes not found in the application context.
     * @param applicationContext the application context to look up classes in; must be non-{@code null}
     */
    public PicocliSpringFactory(ApplicationContext applicationContext) {
        this(applicationContext, CommandLine.defaultFactory());
    }

    /**
     * Constructs a PicocliSpringFactory with the specified Application context,
     * and the specified fallback factory
     * for classes not found in the application context.
     * @param applicationContext the application context to look up classes in; must be non-{@code null}
     * @param fallbackFactory the factory used to instantiate classes that are
     *                        not found in the specified application context; must be non-{@code null}
     * @since 4.7.0
     */
    public PicocliSpringFactory(ApplicationContext applicationContext, CommandLine.IFactory fallbackFactory) {
        this.applicationContext = Objects.requireNonNull(applicationContext, "applicationContext");
        this.fallbackFactory = Objects.requireNonNull(fallbackFactory, "fallbackFactory");
    }

    @Override
    public <K> K create(Class<K> clazz) throws Exception {
        try {
            return getBeanOrCreate(clazz);
        } catch (Exception e) {
            logger.warn("Unable to get bean of class {}, using fallback factory {}",
                clazz, fallbackFactory.getClass().getName());
            return fallbackFactory.create(clazz);
        }
    }

    private <K> K getBeanOrCreate(Class<K> clazz) {
        try {
            return applicationContext.getBean(clazz);
        } catch (Exception e) {
            return applicationContext.getAutowireCapableBeanFactory().createBean(clazz);
        }
    }
}

@@ -2449,16 +2449,21 @@
 ctory %7B%7D
+ (%7B%7D)
 %22,%0A     
@@ -2516,16 +2516,30 @@
 etName()
+, e.toString()
 );%0A     

improve logging: show exception