• 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

91.18
today-context/src/main/java/infra/cache/annotation/AbstractCachingConfiguration.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.cache.annotation;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.util.function.Function;
23
import java.util.function.Supplier;
24

25
import infra.beans.BeansException;
26
import infra.beans.factory.BeanFactory;
27
import infra.beans.factory.BeanFactoryAware;
28
import infra.beans.factory.annotation.DisableDependencyInjection;
29
import infra.cache.CacheManager;
30
import infra.cache.interceptor.CacheErrorHandler;
31
import infra.cache.interceptor.CacheResolver;
32
import infra.cache.interceptor.KeyGenerator;
33
import infra.context.annotation.Configuration;
34
import infra.context.annotation.ImportAware;
35
import infra.core.annotation.AnnotationAttributes;
36
import infra.core.type.AnnotationMetadata;
37
import infra.util.ObjectUtils;
38
import infra.util.function.SingletonSupplier;
39

40
/**
41
 * Abstract base {@code @Configuration} class providing common structure
42
 * for enabling Infra annotation-driven cache management capability.
43
 *
44
 * @author Chris Beams
45
 * @author Stephane Nicoll
46
 * @author Juergen Hoeller
47
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
48
 * @see EnableCaching
49
 * @since 4.0
50
 */
51
@DisableDependencyInjection
52
@Configuration(proxyBeanMethods = false)
53
public abstract class AbstractCachingConfiguration implements ImportAware, BeanFactoryAware {
3✔
54

55
  @Nullable
56
  protected AnnotationAttributes enableCaching;
57

58
  @Nullable
59
  protected Supplier<CacheManager> cacheManager;
60

61
  @Nullable
62
  protected Supplier<CacheResolver> cacheResolver;
63

64
  @Nullable
65
  protected Supplier<KeyGenerator> keyGenerator;
66

67
  @Nullable
68
  protected Supplier<CacheErrorHandler> errorHandler;
69

70
  @Override
71
  public void setImportMetadata(AnnotationMetadata importMetadata) {
72
    this.enableCaching = AnnotationAttributes.fromMap(
5✔
73
            importMetadata.getAnnotationAttributes(EnableCaching.class));
1✔
74
    if (this.enableCaching == null) {
3!
75
      throw new IllegalArgumentException(
×
76
              "@EnableCaching is not present on importing class " + importMetadata.getClassName());
×
77
    }
78
  }
1✔
79

80
  @Override
81
  public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
82
    useCachingConfigurer(new CachingConfigurerSupplier((Supplier<@Nullable CachingConfigurer>) () -> {
7✔
83
      var candidates = beanFactory.getBeanNamesForType(CachingConfigurer.class);
4✔
84
      if (ObjectUtils.isEmpty(candidates)) {
3✔
85
        return null;
2✔
86
      }
87
      if (candidates.length > 1) {
4✔
88
        throw new IllegalStateException(candidates.length + " implementations of " +
7✔
89
                "CachingConfigurer were found when only 1 was expected. " +
90
                "Refactor the configuration such that CachingConfigurer is " +
91
                "implemented only once or not at all.");
92
      }
93
      return beanFactory.getBean(candidates[0], CachingConfigurer.class);
8✔
94
    }));
95
  }
1✔
96

97
  /**
98
   * Extract the configuration from the nominated {@link CachingConfigurer}.
99
   */
100
  @SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1128
101
  protected void useCachingConfigurer(CachingConfigurerSupplier supplier) {
102
    this.cacheManager = supplier.adapt(CachingConfigurer::cacheManager);
5✔
103
    this.cacheResolver = supplier.adapt(CachingConfigurer::cacheResolver);
5✔
104
    this.keyGenerator = supplier.adapt(CachingConfigurer::keyGenerator);
5✔
105
    this.errorHandler = supplier.adapt(CachingConfigurer::errorHandler);
5✔
106
  }
1✔
107

108
  protected static class CachingConfigurerSupplier {
109

110
    private final SingletonSupplier<CachingConfigurer> supplier;
111

112
    @SuppressWarnings("NullAway")
113
    public CachingConfigurerSupplier(Supplier<@Nullable CachingConfigurer> supplier) {
2✔
114
      this.supplier = SingletonSupplier.ofNullable(supplier);
4✔
115
    }
1✔
116

117
    /**
118
     * Adapt the {@link CachingConfigurer} supplier to another supplier
119
     * provided by the specified mapping function. If the underlying
120
     * {@link CachingConfigurer} is {@code null}, {@code null} is returned
121
     * and the mapping function is not invoked.
122
     *
123
     * @param provider the provider to use to adapt the supplier
124
     * @param <T> the type of the supplier
125
     * @return another supplier mapped by the specified function
126
     */
127
    public <T> Supplier<@Nullable T> adapt(Function<CachingConfigurer, @Nullable T> provider) {
128
      return () -> {
4✔
129
        CachingConfigurer cachingConfigurer = this.supplier.get();
5✔
130
        return (cachingConfigurer != null ? provider.apply(cachingConfigurer) : null);
8✔
131
      };
132
    }
133

134
  }
135

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