• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.3
today-context/src/main/java/infra/context/annotation/ConfigurationClassBeanDefinitionReader.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.context.annotation;
19

20
import java.io.Serial;
21
import java.lang.reflect.Method;
22
import java.util.HashMap;
23
import java.util.Map;
24
import java.util.Set;
25

26
import infra.beans.factory.BeanDefinitionStoreException;
27
import infra.beans.factory.BeanRegistrar;
28
import infra.beans.factory.annotation.AnnotatedBeanDefinition;
29
import infra.beans.factory.annotation.AnnotatedGenericBeanDefinition;
30
import infra.beans.factory.annotation.DisableAllDependencyInjection;
31
import infra.beans.factory.annotation.DisableDependencyInjection;
32
import infra.beans.factory.annotation.EnableDependencyInjection;
33
import infra.beans.factory.config.BeanDefinition;
34
import infra.beans.factory.config.BeanDefinitionHolder;
35
import infra.beans.factory.support.AbstractBeanDefinition;
36
import infra.beans.factory.support.AbstractBeanDefinitionReader;
37
import infra.beans.factory.support.BeanDefinitionOverrideException;
38
import infra.beans.factory.support.BeanDefinitionReader;
39
import infra.beans.factory.support.BeanDefinitionRegistry;
40
import infra.beans.factory.support.BeanNameGenerator;
41
import infra.beans.factory.support.BeanRegistryAdapter;
42
import infra.beans.factory.support.RootBeanDefinition;
43
import infra.beans.factory.xml.XmlBeanDefinitionReader;
44
import infra.context.BootstrapContext;
45
import infra.core.annotation.MergedAnnotation;
46
import infra.core.annotation.MergedAnnotations;
47
import infra.core.type.AnnotationMetadata;
48
import infra.core.type.MethodMetadata;
49
import infra.core.type.StandardAnnotationMetadata;
50
import infra.core.type.StandardMethodMetadata;
51
import infra.lang.Assert;
52
import infra.logging.Logger;
53
import infra.logging.LoggerFactory;
54
import infra.stereotype.Component;
55
import infra.util.ObjectUtils;
56
import infra.util.ReflectionUtils;
57
import infra.util.StringUtils;
58

59
/**
60
 * Reads a given fully-populated set of ConfigurationClass instances, registering bean
61
 * definitions with the given {@link BeanDefinitionRegistry} based on its contents.
62
 *
63
 * @author Chris Beams
64
 * @author Juergen Hoeller
65
 * @author Phillip Webb
66
 * @author Sam Brannen
67
 * @author Sebastien Deleuze
68
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
69
 * @see ConfigurationClassParser
70
 * @since 4.0
71
 */
72
class ConfigurationClassBeanDefinitionReader {
73

74
  private static final Logger logger = LoggerFactory.getLogger(ConfigurationClassBeanDefinitionReader.class);
3✔
75

76
  private static final ScopeMetadataResolver scopeMetadataResolver = new AnnotationScopeMetadataResolver();
5✔
77

78
  private final ImportRegistry importRegistry;
79

80
  private final BootstrapContext bootstrapContext;
81

82
  private final BeanNameGenerator importBeanNameGenerator;
83

84
  /**
85
   * Create a new {@link ConfigurationClassBeanDefinitionReader} instance
86
   * that will be used to populate the given {@link BeanDefinitionRegistry}.
87
   */
88
  ConfigurationClassBeanDefinitionReader(BootstrapContext bootstrapContext,
89
          BeanNameGenerator importBeanNameGenerator, ImportRegistry importRegistry) {
2✔
90

91
    this.bootstrapContext = bootstrapContext;
3✔
92
    this.importRegistry = importRegistry;
3✔
93
    this.importBeanNameGenerator = importBeanNameGenerator;
3✔
94
  }
1✔
95

96
  /**
97
   * Read {@code configurationModel}, registering bean definitions
98
   * with the registry based on its contents.
99
   */
100
  public void loadBeanDefinitions(Set<ConfigurationClass> configurationModel) {
101
    TrackedConditionEvaluator trackedConditionEvaluator = new TrackedConditionEvaluator();
5✔
102
    for (ConfigurationClass configClass : configurationModel) {
10✔
103
      loadBeanDefinitionsForConfigurationClass(configClass, trackedConditionEvaluator);
4✔
104
    }
1✔
105
  }
1✔
106

107
  /**
108
   * Read a particular {@link ConfigurationClass}, registering bean definitions
109
   * for the class itself and all of its {@link Component} methods.
110
   */
111
  private void loadBeanDefinitionsForConfigurationClass(
112
          ConfigurationClass configClass, TrackedConditionEvaluator trackedConditionEvaluator) {
113

114
    if (trackedConditionEvaluator.shouldSkip(configClass)) {
4✔
115
      String beanName = configClass.beanName;
3✔
116
      // TODO annotated with both @Component and @ConditionalOnMissingBean,condition matching error
117
      if (StringUtils.isNotEmpty(beanName) && bootstrapContext.containsBeanDefinition(beanName)) {
8!
118
        bootstrapContext.removeBeanDefinition(beanName);
4✔
119
      }
120
      importRegistry.removeImportingClass(configClass.metadata.getClassName());
6✔
121
    }
1✔
122
    else {
123
      boolean disableAllDependencyInjection = isDisableAllDependencyInjection(configClass);
4✔
124
      if (configClass.isImported()) {
3✔
125
        registerBeanDefinitionForImportedConfigurationClass(configClass, disableAllDependencyInjection);
4✔
126
      }
127

128
      for (ComponentMethod componentMethod : configClass.componentMethods) {
11✔
129
        loadBeanDefinitionsForComponentMethod(componentMethod, disableAllDependencyInjection);
4✔
130
      }
1✔
131

132
      loadBeanDefinitionsFromImportedResources(configClass.importedResources);
4✔
133
      loadBeanDefinitionsFromImportBeanDefinitionRegistrars(configClass.importBeanDefinitionRegistrars);
4✔
134
      loadBeanDefinitionsFromBeanRegistrars(configClass.beanRegistrars);
4✔
135
    }
136
  }
1✔
137

138
  /**
139
   * Register the {@link Configuration} class itself as a bean definition.
140
   */
141
  private void registerBeanDefinitionForImportedConfigurationClass(ConfigurationClass configClass, boolean disableAllDependencyInjection) {
142
    var configBeanDef = new AnnotatedGenericBeanDefinition(configClass.metadata);
6✔
143

144
    ScopeMetadata scopeMetadata = scopeMetadataResolver.resolveScopeMetadata(configBeanDef);
4✔
145
    configBeanDef.setScope(scopeMetadata.getScopeName());
4✔
146
    // import bean name
147
    String configBeanName = importBeanNameGenerator.generateBeanName(configBeanDef, bootstrapContext.getRegistry());
8✔
148
    AnnotationConfigUtils.applyAnnotationMetadata(configBeanDef, false);
3✔
149

150
    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(configBeanDef, configBeanName);
6✔
151
    definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, bootstrapContext.getRegistry());
7✔
152
    bootstrapContext.registerBeanDefinition(definitionHolder.getBeanName(), definitionHolder.getBeanDefinition());
7✔
153
    configClass.setBeanName(configBeanName);
3✔
154

155
    boolean enableDependencyInjection = isEnableDependencyInjection(configClass.metadata.getAnnotations(), disableAllDependencyInjection);
7✔
156
    configBeanDef.setEnableDependencyInjection(enableDependencyInjection);
3✔
157

158
    if (logger.isTraceEnabled()) {
3!
159
      logger.trace("Registered bean definition for imported class '{}'", configBeanName);
×
160
    }
161
  }
1✔
162

163
  /**
164
   * Read the given {@link ComponentMethod}, registering bean definitions
165
   * with the BeanDefinitionRegistry based on its contents.
166
   */
167
  private void loadBeanDefinitionsForComponentMethod(ComponentMethod componentMethod, boolean disableAllDependencyInjection) {
168
    ConfigurationClass configClass = componentMethod.configurationClass;
3✔
169
    MethodMetadata metadata = componentMethod.metadata;
3✔
170
    String methodName = metadata.getMethodName();
3✔
171

172
    // Do we need to mark the bean as skipped by its condition?
173
    if (bootstrapContext.shouldSkip(metadata, ConfigurationCondition.ConfigurationPhase.REGISTER_BEAN)) {
6✔
174
      configClass.skippedComponentMethods.add(methodName);
5✔
175
      return;
1✔
176
    }
177
    if (configClass.skippedComponentMethods.contains(methodName)) {
5!
178
      return;
×
179
    }
180
    MergedAnnotations annotations = metadata.getAnnotations();
3✔
181
    MergedAnnotation<Component> component = annotations.get(Component.class);
4✔
182
    Assert.state(component.isPresent(), "No @Component annotation attributes");
4✔
183

184
    // Consider name and any aliases.
185
    String[] explicitNames = component.getStringArray("name");
4✔
186
    String beanName;
187
    String localBeanName;
188
    if (explicitNames.length > 0 && StringUtils.hasText(explicitNames[0])) {
8!
189
      beanName = explicitNames[0];
4✔
190
      localBeanName = beanName;
2✔
191
      // Register aliases even when overridden below.
192
      for (int i = 1; i < explicitNames.length; i++) {
9✔
193
        bootstrapContext.registerAlias(beanName, explicitNames[i]);
7✔
194
      }
195
    }
196
    else {
197
      // Default bean name derived from method name.
198
      beanName = this.importBeanNameGenerator instanceof ConfigurationBeanNameGenerator cbng
9✔
199
              ? cbng.deriveBeanName(metadata) : methodName;
6✔
200
      localBeanName = methodName;
2✔
201
    }
202

203
    var beanDef = new ConfigurationClassBeanDefinition(configClass, metadata, localBeanName);
7✔
204
    beanDef.setSource(configClass.resource);
4✔
205
    beanDef.setResource(configClass.resource);
4✔
206

207
    // Has this effectively been overridden before (e.g. via XML)?
208
    if (isOverriddenByExistingDefinition(componentMethod, beanName, beanDef)) {
6✔
209
      if (beanName.equals(componentMethod.configurationClass.beanName)) {
6✔
210
        throw new BeanDefinitionStoreException(componentMethod.configurationClass.resource.toString(),
14✔
211
                beanName, ("Bean name derived from @Component method '%s' clashes with bean name for containing configuration class; " +
212
                "please make those names unique!").formatted(componentMethod.metadata.getMethodName()));
5✔
213
      }
214
      return;
1✔
215
    }
216

217
    boolean enableDI = isEnableDependencyInjection(annotations, disableAllDependencyInjection);
5✔
218
    beanDef.setEnableDependencyInjection(enableDI);
3✔
219

220
    AnnotationMetadata configClassMetadata = configClass.metadata;
3✔
221
    if (metadata.isStatic()) {
3✔
222
      // static @Component method
223
      if (configClassMetadata instanceof StandardAnnotationMetadata) {
3✔
224
        beanDef.setBeanClass(((StandardAnnotationMetadata) configClassMetadata).getIntrospectedClass());
6✔
225
      }
226
      else {
227
        beanDef.setBeanClassName(configClassMetadata.getClassName());
4✔
228
      }
229
      beanDef.setUniqueFactoryMethodName(methodName);
4✔
230
    }
231
    else {
232
      // instance @Component method
233
      beanDef.setFactoryBeanName(configClass.beanName);
4✔
234
      beanDef.setUniqueFactoryMethodName(methodName);
3✔
235
    }
236

237
    if (metadata instanceof StandardMethodMetadata smm && configClass.metadata instanceof StandardAnnotationMetadata sam) {
15!
238
      Method method = ReflectionUtils.getMostSpecificMethod(smm.getIntrospectedMethod(), sam.getIntrospectedClass());
6✔
239
      if (method == smm.getIntrospectedMethod()) {
4✔
240
        beanDef.setResolvedFactoryMethod(method);
3✔
241
      }
242
    }
243

244
    beanDef.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
3✔
245
    AnnotationConfigUtils.applyAnnotationMetadata(beanDef, false);
3✔
246

247
    if (!component.getBoolean("autowireCandidate")) {
4✔
248
      beanDef.setAutowireCandidate(false);
3✔
249
    }
250

251
    if (!component.getBoolean("defaultCandidate")) {
4✔
252
      beanDef.setDefaultCandidate(false);
3✔
253
    }
254

255
    var instantiation = component.getEnum("bootstrap", Component.Bootstrap.class);
6✔
256
    if (instantiation == Component.Bootstrap.BACKGROUND) {
3✔
257
      beanDef.setBackgroundInit(true);
3✔
258
    }
259

260
    String[] initMethodName = component.getStringArray("initMethods");
4✔
261
    if (ObjectUtils.isNotEmpty(initMethodName)) {
3✔
262
      beanDef.setInitMethodNames(initMethodName);
3✔
263
    }
264

265
    String destroyMethodName = component.getString("destroyMethod");
4✔
266
    beanDef.setDestroyMethodName(destroyMethodName);
3✔
267

268
    // Consider scoping
269
    ScopedProxyMode proxyMode = ScopedProxyMode.NO;
2✔
270
    MergedAnnotation<Scope> scope = annotations.get(Scope.class);
4✔
271
    if (scope.isPresent()) {
3✔
272
      beanDef.setScope(scope.getString("value"));
5✔
273
      proxyMode = scope.getEnum("proxyMode", ScopedProxyMode.class);
6✔
274
      if (proxyMode == ScopedProxyMode.DEFAULT) {
3✔
275
        proxyMode = ScopedProxyMode.NO;
2✔
276
      }
277
    }
278

279
    // Replace the original bean definition with the target one, if necessary
280
    BeanDefinition beanDefToRegister;
281
    if (proxyMode != ScopedProxyMode.NO) {
3✔
282
      BeanDefinitionHolder proxyDef = ScopedProxyCreator.createScopedProxy(new BeanDefinitionHolder(beanDef, beanName),
9✔
283
              bootstrapContext.getRegistry(), proxyMode == ScopedProxyMode.TARGET_CLASS);
7✔
284
      beanDefToRegister = new ConfigurationClassBeanDefinition(
3✔
285
              (RootBeanDefinition) proxyDef.getBeanDefinition(), configClass, metadata, localBeanName);
7✔
286
    }
1✔
287
    else {
288
      beanDefToRegister = beanDef;
2✔
289
    }
290

291
    // Replace the original bean definition with the target one, if necessary
292
    if (logger.isTraceEnabled()) {
3!
293
      logger.trace("Registering bean definition for @Component method {}.{}()",
×
294
              configClassMetadata.getClassName(), beanName);
×
295
    }
296

297
    bootstrapContext.registerBeanDefinition(beanName, beanDefToRegister);
5✔
298
  }
1✔
299

300
  /**
301
   * disable all member class DI
302
   *
303
   * @param configClass current config class
304
   */
305
  private boolean isDisableAllDependencyInjection(ConfigurationClass configClass) {
306
    if (!configClass.metadata.isAnnotated(DisableAllDependencyInjection.class)) {
5✔
307
      try {
308
        String enclosingClassName = configClass.metadata.getEnclosingClassName();
4✔
309
        while (enclosingClassName != null) {
2✔
310
          AnnotationMetadata enclosingMetadata = bootstrapContext.getAnnotationMetadata(enclosingClassName);
5✔
311
          if (enclosingMetadata.isAnnotated(DisableAllDependencyInjection.class)) {
4✔
312
            return true;
2✔
313
          }
314
          enclosingClassName = enclosingMetadata.getEnclosingClassName();
3✔
315
        }
1✔
316
      }
317
      catch (Exception e) {
1✔
318
        logger.error("Failed to read class file via ASM for determining DI status order", e);
4✔
319
      }
1✔
320
      return false;
2✔
321
    }
322
    return true;
2✔
323
  }
324

325
  private boolean isEnableDependencyInjection(MergedAnnotations annotations, boolean disableAllDependencyInjection) {
326
    return annotations.isPresent(EnableDependencyInjection.class)
9✔
327
            || !(disableAllDependencyInjection || annotations.isPresent(DisableDependencyInjection.class));
5✔
328
  }
329

330
  @SuppressWarnings("NullAway")
331
  private boolean isOverriddenByExistingDefinition(ComponentMethod componentMethod, String beanName, ConfigurationClassBeanDefinition newBeanDef) {
332
    if (!bootstrapContext.containsBeanDefinition(beanName)) {
5✔
333
      return false;
2✔
334
    }
335
    BeanDefinition existingBeanDef = bootstrapContext.getBeanDefinition(beanName);
5✔
336
    ConfigurationClass configClass = componentMethod.configurationClass;
3✔
337

338
    // If the bean method is an overloaded case on the same configuration class,
339
    // preserve the existing bean definition and mark it as overloaded.
340
    if (existingBeanDef instanceof ConfigurationClassBeanDefinition ccbd) {
6✔
341
      if (!ccbd.getMetadata().getClassName().equals(configClass.metadata.getClassName())) {
8✔
342
        return false;
2✔
343
      }
344
      if (ccbd.getFactoryMethodMetadata().getMethodName().equals(componentMethod.metadata.getMethodName())) {
8✔
345
        ccbd.setNonUniqueFactoryMethodName(ccbd.getFactoryMethodMetadata().getMethodName());
5✔
346
        return true;
2✔
347
      }
348
      Map<String, Object> attributes = configClass.metadata.getAnnotationAttributes(Configuration.class);
5✔
349
      if ((attributes != null && (Boolean) attributes.get("enforceUniqueMethods"))
10!
350
              || !bootstrapContext.getRegistry().isBeanDefinitionOverridable(beanName)) {
4✔
351
        throw new BeanDefinitionOverrideException(beanName, newBeanDef, existingBeanDef,
10✔
352
                "@Bean method override with same bean name but different method name: " + existingBeanDef);
353
      }
354
      return true;
2✔
355
    }
356

357
    // A bean definition resulting from a component scan can be silently overridden
358
    // by an @Component method, even when general overriding is disabled
359
    // as long as the bean class is the same.
360
    if (existingBeanDef instanceof ScannedGenericBeanDefinition scannedBeanDef) {
6✔
361
      if (componentMethod.metadata.getReturnTypeName().equals(scannedBeanDef.getBeanClassName())) {
7!
362
        bootstrapContext.removeBeanDefinition(beanName);
4✔
363
      }
364
      return false;
2✔
365
    }
366

367
    // Has the existing bean definition bean marked as a framework-generated bean?
368
    // -> allow the current bean method to override it, since it is application-level
369
    if (existingBeanDef.getRole() > BeanDefinition.ROLE_APPLICATION) {
3✔
370
      return false;
2✔
371
    }
372

373
    // At this point, it's a top-level override (probably XML), just having been parsed
374
    // before configuration class processing kicks in...
375
    if (!bootstrapContext.getRegistry().isBeanDefinitionOverridable(beanName)) {
6✔
376
      throw new BeanDefinitionOverrideException(beanName, newBeanDef,
10✔
377
              existingBeanDef, "@Component definition illegally overridden by existing bean definition: " + existingBeanDef);
378
    }
379
    if (logger.isDebugEnabled()) {
3!
380
      logger.debug("Skipping bean definition for {}: a definition for bean '{}' " +
×
381
              "already exists. This top-level bean definition is considered as an override.", componentMethod, beanName);
382
    }
383
    return true;
2✔
384
  }
385

386
  private void loadBeanDefinitionsFromImportedResources(Map<String, Class<? extends BeanDefinitionReader>> importedResources) {
387
    HashMap<Class<?>, BeanDefinitionReader> readerInstanceCache = new HashMap<>();
4✔
388

389
    for (Map.Entry<String, Class<? extends BeanDefinitionReader>> entry : importedResources.entrySet()) {
11✔
390
      String resource = entry.getKey();
4✔
391
      Class<? extends BeanDefinitionReader> readerClass = entry.getValue();
4✔
392
      // Default reader selection necessary?
393
      if (BeanDefinitionReader.class == readerClass) {
3!
394
        // Primarily ".xml" files but for any other extension as well
395
        readerClass = XmlBeanDefinitionReader.class;
2✔
396
      }
397

398
      BeanDefinitionReader reader = readerInstanceCache.get(readerClass);
5✔
399
      if (reader == null) {
2✔
400
        try {
401
          // Instantiate the specified BeanDefinitionReader
402
          reader = readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(bootstrapContext.getRegistry());
19✔
403
          // Delegate the current ResourceLoader to it if possible
404
          if (reader instanceof AbstractBeanDefinitionReader abdr) {
6!
405
            abdr.setEnvironment(bootstrapContext.getEnvironment());
5✔
406
            abdr.setResourceLoader(bootstrapContext.getResourceLoader());
5✔
407
          }
408
          readerInstanceCache.put(readerClass, reader);
5✔
409
        }
410
        catch (Throwable ex) {
×
411
          throw new IllegalStateException("Could not instantiate BeanDefinitionReader class [%s]"
×
412
                  .formatted(readerClass.getName()), ex);
×
413
        }
1✔
414
      }
415

416
      reader.loadBeanDefinitions(resource);
4✔
417
    }
1✔
418

419
  }
1✔
420

421
  private void loadBeanDefinitionsFromImportBeanDefinitionRegistrars(Map<ImportBeanDefinitionRegistrar, AnnotationMetadata> registrars) {
422
    for (var entry : registrars.entrySet()) {
11✔
423
      entry.getKey().registerBeanDefinitions(entry.getValue(), bootstrapContext, importBeanNameGenerator);
11✔
424
    }
1✔
425
  }
1✔
426

427
  private void loadBeanDefinitionsFromBeanRegistrars(Map<String, BeanRegistrar> registrars) {
428
    for (BeanRegistrar registrar : registrars.values()) {
11✔
429
      registrar.register(new BeanRegistryAdapter(bootstrapContext.getRegistry(),
9✔
430
              bootstrapContext.getBeanFactory(), bootstrapContext.getEnvironment(), registrar.getClass()), bootstrapContext.getEnvironment());
10✔
431
    }
1✔
432
  }
1✔
433

434
  /**
435
   * Evaluate {@code @Conditional} annotations, tracking results and taking into
436
   * account 'imported by'.
437
   */
438
  private final class TrackedConditionEvaluator {
5✔
439

440
    private final HashMap<ConfigurationClass, Boolean> skipped = new HashMap<>();
6✔
441

442
    public boolean shouldSkip(ConfigurationClass configClass) {
443
      Boolean skip = this.skipped.get(configClass);
6✔
444
      if (skip == null) {
2✔
445
        if (configClass.isImported()) {
3✔
446
          boolean allSkipped = true;
2✔
447
          for (ConfigurationClass importedBy : configClass.importedBy) {
11✔
448
            if (!shouldSkip(importedBy)) {
4✔
449
              allSkipped = false;
2✔
450
              break;
1✔
451
            }
452
          }
1✔
453
          if (allSkipped) {
2✔
454
            // The config classes that imported this one were all skipped, therefore we are skipped...
455
            skip = true;
3✔
456
          }
457
        }
458
        if (skip == null) {
2✔
459
          skip = bootstrapContext.shouldSkip(configClass.metadata, ConfigurationCondition.ConfigurationPhase.REGISTER_BEAN);
9✔
460
        }
461
        this.skipped.put(configClass, skip);
6✔
462
      }
463
      return skip;
3✔
464
    }
465
  }
466

467
  /**
468
   * {@link RootBeanDefinition} marker subclass used to signify that a bean definition
469
   * was created from a configuration class as opposed to any other configuration source.
470
   * Used in bean overriding cases where it's necessary to determine whether the bean
471
   * definition was created externally.
472
   */
473
  private static final class ConfigurationClassBeanDefinition extends RootBeanDefinition
474
          implements AnnotatedBeanDefinition {
475

476
    @Serial
477
    private static final long serialVersionUID = 1L;
478

479
    private final AnnotationMetadata annotationMetadata;
480

481
    private final MethodMetadata factoryMethodMetadata;
482

483
    private final String localBeanName;
484

485
    public ConfigurationClassBeanDefinition(ConfigurationClass configClass,
486
            MethodMetadata beanMethodMetadata, String localBeanName) {
2✔
487

488
      this.annotationMetadata = configClass.metadata;
4✔
489
      this.factoryMethodMetadata = beanMethodMetadata;
3✔
490
      this.localBeanName = localBeanName;
3✔
491
      setResource(configClass.resource);
4✔
492
      setLenientConstructorResolution(false);
3✔
493
    }
1✔
494

495
    public ConfigurationClassBeanDefinition(RootBeanDefinition original,
496
            ConfigurationClass configClass, MethodMetadata beanMethodMetadata, String localBeanName) {
497
      super(original);
3✔
498
      this.annotationMetadata = configClass.metadata;
4✔
499
      this.factoryMethodMetadata = beanMethodMetadata;
3✔
500
      this.localBeanName = localBeanName;
3✔
501
    }
1✔
502

503
    private ConfigurationClassBeanDefinition(ConfigurationClassBeanDefinition original) {
504
      super(original);
3✔
505
      this.annotationMetadata = original.annotationMetadata;
4✔
506
      this.factoryMethodMetadata = original.factoryMethodMetadata;
4✔
507
      this.localBeanName = original.localBeanName;
4✔
508
    }
1✔
509

510
    @Override
511
    public AnnotationMetadata getMetadata() {
512
      return this.annotationMetadata;
3✔
513
    }
514

515
    @Override
516
    public MethodMetadata getFactoryMethodMetadata() {
517
      return this.factoryMethodMetadata;
3✔
518
    }
519

520
    @Override
521
    public boolean isFactoryMethod(Method candidate) {
522
      return super.isFactoryMethod(candidate)
6✔
523
              && BeanAnnotationHelper.isBeanAnnotated(candidate)
3✔
524
              && BeanAnnotationHelper.determineBeanNameFor(candidate).equals(localBeanName);
8✔
525
    }
526

527
    @Override
528
    public ConfigurationClassBeanDefinition cloneBeanDefinition() {
529
      return new ConfigurationClassBeanDefinition(this);
5✔
530
    }
531
  }
532

533
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc