• 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

75.68
today-context/src/main/java/infra/context/annotation/Jsr330ScopeMetadataResolver.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.util.HashMap;
23
import java.util.Set;
24

25
import infra.beans.factory.annotation.AnnotatedBeanDefinition;
26
import infra.beans.factory.config.BeanDefinition;
27

28
/**
29
 * Simple {@link ScopeMetadataResolver} implementation that follows JSR-330 scoping rules:
30
 * defaulting to prototype scope unless {@link jakarta.inject.Singleton} is present.
31
 *
32
 * <p>This scope resolver can be used with {@link ClassPathBeanDefinitionScanner} and
33
 * {@link AnnotatedBeanDefinitionReader} for standard JSR-330 compliance. However,
34
 * in practice, you will typically use Framework's rich default scoping instead - or extend
35
 * this resolver with custom scoping annotations that point to extended Framework scopes.
36
 *
37
 * @author Juergen Hoeller
38
 * @see #registerScope
39
 * @see #resolveScopeName
40
 * @see ClassPathBeanDefinitionScanner#setScopeMetadataResolver
41
 * @see AnnotatedBeanDefinitionReader#setScopeMetadataResolver
42
 * @since 4.0
43
 */
44
public class Jsr330ScopeMetadataResolver implements ScopeMetadataResolver {
45

46
  private final HashMap<String, String> scopeMap = new HashMap<>();
5✔
47

48
  public Jsr330ScopeMetadataResolver() {
2✔
49
    registerScope("jakarta.inject.Singleton", BeanDefinition.SCOPE_SINGLETON);
4✔
50
  }
1✔
51

52
  /**
53
   * Register an extended JSR-330 scope annotation, mapping it onto a
54
   * specific Frameworkscope by name.
55
   *
56
   * @param annotationType the JSR-330 annotation type as a Class
57
   * @param scopeName the Framework scope name
58
   */
59
  public final void registerScope(Class<?> annotationType, String scopeName) {
60
    this.scopeMap.put(annotationType.getName(), scopeName);
×
61
  }
×
62

63
  /**
64
   * Register an extended JSR-330 scope annotation, mapping it onto a
65
   * specific Framework scope by name.
66
   *
67
   * @param annotationType the JSR-330 annotation type by name
68
   * @param scopeName the Framework scope name
69
   */
70
  public final void registerScope(String annotationType, String scopeName) {
71
    this.scopeMap.put(annotationType, scopeName);
6✔
72
  }
1✔
73

74
  /**
75
   * Resolve the given annotation type into a named Framework scope.
76
   * <p>The default implementation simply checks against registered scopes.
77
   * Can be overridden for custom mapping rules, e.g. naming conventions.
78
   *
79
   * @param annotationType the JSR-330 annotation type
80
   * @return the Framework scope name
81
   */
82
  @Nullable
83
  protected String resolveScopeName(String annotationType) {
84
    return this.scopeMap.get(annotationType);
6✔
85
  }
86

87
  @Override
88
  public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
89
    ScopeMetadata metadata = new ScopeMetadata();
4✔
90
    metadata.setScopeName(BeanDefinition.SCOPE_PROTOTYPE);
3✔
91
    if (definition instanceof AnnotatedBeanDefinition annDef) {
6!
92
      Set<String> annTypes = annDef.getMetadata().getAnnotationTypes();
4✔
93
      String found = null;
2✔
94
      for (String annType : annTypes) {
10✔
95
        Set<String> metaAnns = annDef.getMetadata().getMetaAnnotationTypes(annType);
5✔
96
        if (metaAnns.contains("jakarta.inject.Scope")) {
4!
97
          if (found != null) {
2!
98
            throw new IllegalStateException("Found ambiguous scope annotations on bean class [%s]: %s, %s"
×
99
                    .formatted(definition.getBeanClassName(), found, annType));
×
100
          }
101
          found = annType;
2✔
102
          String scopeName = resolveScopeName(annType);
4✔
103
          if (scopeName == null) {
2!
104
            throw new IllegalStateException(
×
105
                    "Unsupported scope annotation - not mapped onto Framework scope name: " + annType);
106
          }
107
          metadata.setScopeName(scopeName);
3✔
108
        }
109
      }
1✔
110
    }
111
    return metadata;
2✔
112
  }
113

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