• 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

78.26
today-context/src/main/java/infra/context/annotation/ImportAwareAotBeanPostProcessor.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 org.jspecify.annotations.Nullable;
21

22
import java.io.IOException;
23
import java.util.Map;
24

25
import infra.beans.factory.InitializationBeanPostProcessor;
26
import infra.beans.factory.config.BeanPostProcessor;
27
import infra.core.Ordered;
28
import infra.core.PriorityOrdered;
29
import infra.core.type.classreading.CachingMetadataReaderFactory;
30
import infra.core.type.classreading.MetadataReader;
31
import infra.core.type.classreading.MetadataReaderFactory;
32
import infra.util.ClassUtils;
33

34
/**
35
 * A {@link BeanPostProcessor} that honours {@link ImportAware} callback using
36
 * a mapping computed at build time.
37
 *
38
 * @author Stephane Nicoll
39
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
40
 * @since 4.0
41
 */
42
public final class ImportAwareAotBeanPostProcessor implements InitializationBeanPostProcessor, PriorityOrdered {
43

44
  private final MetadataReaderFactory metadataReaderFactory;
45

46
  private final Map<String, String> importsMapping;
47

48
  public ImportAwareAotBeanPostProcessor(Map<String, String> importsMapping) {
2✔
49
    this.metadataReaderFactory = new CachingMetadataReaderFactory();
5✔
50
    this.importsMapping = Map.copyOf(importsMapping);
4✔
51
  }
1✔
52

53
  @Override
54
  public Object postProcessBeforeInitialization(Object bean, String beanName) {
55
    if (bean instanceof ImportAware importAware) {
6✔
56
      setAnnotationMetadata(importAware);
3✔
57
    }
58
    return bean;
2✔
59
  }
60

61
  @Override
62
  public int getOrder() {
63
    return Ordered.LOWEST_PRECEDENCE;  // match priority of original post processor
×
64
  }
65

66
  private void setAnnotationMetadata(ImportAware instance) {
67
    String importingClass = getImportingClassFor(instance);
4✔
68
    if (importingClass == null) {
2!
69
      return; // import aware configuration class not imported
×
70
    }
71
    try {
72
      MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(importingClass);
5✔
73
      instance.setImportMetadata(metadataReader.getAnnotationMetadata());
4✔
74
    }
75
    catch (IOException ex) {
×
76
      throw new IllegalStateException(String.format("Failed to read metadata for '%s'", importingClass), ex);
×
77
    }
1✔
78
  }
1✔
79

80
  @Nullable
81
  private String getImportingClassFor(ImportAware instance) {
82
    String target = ClassUtils.getUserClass(instance).getName();
4✔
83
    return this.importsMapping.get(target);
6✔
84
  }
85

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