1111import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
1212import org .springframework .context .annotation .ImportBeanDefinitionRegistrar ;
1313import org .springframework .core .type .AnnotationMetadata ;
14- import org .springframework .lang .NonNull ;
1514
1615import java .util .HashSet ;
1716import java .util .Map ;
@@ -36,14 +35,8 @@ public class ToolCallbackAutoRegistrar implements ImportBeanDefinitionRegistrar
3635 @ Override
3736 public void registerBeanDefinitions (AnnotationMetadata importingClassMetadata , BeanDefinitionRegistry registry ,
3837 BeanNameGenerator importBeanNameGenerator ) {
39- Map <String , Object > attributes = importingClassMetadata
40- .getAnnotationAttributes (EnableToolCallbackAutoRegistration .class .getName ());
41-
42- if (attributes == null ) {
43- return ;
44- }
4538
46- Set <String > basePackages = getBasePackages (attributes );
39+ Set <String > basePackages = getBasePackages (importingClassMetadata );
4740
4841 GenericBeanDefinition beanDefinition = new GenericBeanDefinition ();
4942 beanDefinition .setBeanClass (ToolAnnotatedBeanProcessor .class );
@@ -57,20 +50,17 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
5750 }
5851
5952 /**
60- * Extracts the base packages to scan from the
61- * {@code @EnableToolCallbackAutoRegistration} annotation attributes.
62- *
63- * <p>
64- * Supports the following attributes:
65- * <ul>
66- * <li>{@code value} - Shorthand for base packages</li>
67- * <li>{@code basePackages} - Explicit list of packages</li>
68- * <li>{@code basePackageClasses} - Infers packages from class types</li>
69- * </ul>
70- * @param attributes the annotation attributes
71- * @return a set of base package names to scan
53+ * Extracts the base packages to scan from the attributes of
54+ * {@link EnableToolCallbackAutoRegistration}. If no base package is specified through
55+ * 'value', 'basePackages', or 'basePackageClasses', it falls back to the package of
56+ * the class where {@code @EnableToolCallbackAutoRegistration} is declared.
57+ * @param importingClassMetadata metadata of the importing configuration class
58+ * @return set of base packages to scan
7259 */
73- private Set <String > getBasePackages (@ NonNull Map <String , Object > attributes ) {
60+ private Set <String > getBasePackages (AnnotationMetadata importingClassMetadata ) {
61+ Map <String , Object > attributes = importingClassMetadata
62+ .getAnnotationAttributes (EnableToolCallbackAutoRegistration .class .getName ());
63+
7464 Set <String > basePackages = new HashSet <>();
7565
7666 Object [] valuePackages = (Object []) attributes .get ("value" );
@@ -107,6 +97,22 @@ private Set<String> getBasePackages(@NonNull Map<String, Object> attributes) {
10797 }
10898 }
10999
100+ // If no base packages are specified, use the package of the class annotated with
101+ // @EnableToolCallbackAutoRegistration
102+ if (basePackages .isEmpty ()) {
103+ String className = importingClassMetadata .getClassName ();
104+ try {
105+ Class <?> importingClass = Class .forName (className );
106+ Package pkg = importingClass .getPackage ();
107+ if (pkg != null ) {
108+ basePackages .add (pkg .getName ());
109+ }
110+ }
111+ catch (ClassNotFoundException e ) {
112+ throw new IllegalStateException ("Could not resolve base package from importing class: " + className , e );
113+ }
114+ }
115+
110116 return basePackages ;
111117 }
112118
0 commit comments