• 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

68.42
today-context/src/main/java/infra/context/annotation/AdviceModeImportSelector.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.lang.annotation.Annotation;
23

24
import infra.core.GenericTypeResolver;
25
import infra.core.annotation.MergedAnnotation;
26
import infra.core.type.AnnotationMetadata;
27
import infra.lang.Assert;
28

29
/**
30
 * Convenient base class for {@link ImportSelector} implementations that select imports
31
 * based on an {@link AdviceMode} value from an annotation (such as the {@code @Enable*}
32
 * annotations).
33
 *
34
 * @param <A> annotation containing {@linkplain #getAdviceModeAttributeName() AdviceMode attribute}
35
 * @author Chris Beams
36
 * @since 4.0
37
 */
38
public abstract class AdviceModeImportSelector<A extends Annotation> implements ImportSelector {
3✔
39

40
  /**
41
   * The default advice mode attribute name.
42
   */
43
  public static final String DEFAULT_ADVICE_MODE_ATTRIBUTE_NAME = "mode";
44

45
  /**
46
   * The name of the {@link AdviceMode} attribute for the annotation specified by the
47
   * generic type {@code A}. The default is {@value #DEFAULT_ADVICE_MODE_ATTRIBUTE_NAME},
48
   * but subclasses may override in order to customize.
49
   */
50
  protected String getAdviceModeAttributeName() {
51
    return DEFAULT_ADVICE_MODE_ATTRIBUTE_NAME;
2✔
52
  }
53

54
  /**
55
   * This implementation resolves the type of annotation from generic metadata and
56
   * validates that (a) the annotation is in fact present on the importing
57
   * {@code @Configuration} class and (b) that the given annotation has an
58
   * {@linkplain #getAdviceModeAttributeName() advice mode attribute} of type
59
   * {@link AdviceMode}.
60
   * <p>The {@link #selectImports(AdviceMode)} method is then invoked, allowing the
61
   * concrete implementation to choose imports in a safe and convenient fashion.
62
   *
63
   * @throws IllegalArgumentException if expected annotation {@code A} is not present
64
   * on the importing {@code @Configuration} class or if {@link #selectImports(AdviceMode)}
65
   * returns {@code null}
66
   */
67
  @Override
68
  public final String[] selectImports(AnnotationMetadata importMetadata) {
69
    Class<?> annType = GenericTypeResolver.resolveTypeArgument(getClass(), AdviceModeImportSelector.class);
5✔
70
    Assert.state(annType != null, "Unresolvable type argument for AdviceModeImportSelector");
6!
71

72
    MergedAnnotation<Annotation> attributes = importMetadata.getAnnotation(annType.getName());
5✔
73
    if (!attributes.isPresent()) {
3!
74
      throw new IllegalArgumentException(String.format(
×
75
              "@%s is not present on importing class '%s' as expected",
76
              annType.getSimpleName(), importMetadata.getClassName()));
×
77
    }
78

79
    AdviceMode adviceMode = attributes.getEnum(getAdviceModeAttributeName(), AdviceMode.class);
7✔
80
    String[] imports = selectImports(adviceMode);
4✔
81
    if (imports == null) {
2!
82
      throw new IllegalArgumentException("Unknown AdviceMode: " + adviceMode);
×
83
    }
84
    return imports;
2✔
85
  }
86

87
  /**
88
   * Determine which classes should be imported based on the given {@code AdviceMode}.
89
   * <p>Returning {@code null} from this method indicates that the {@code AdviceMode}
90
   * could not be handled or was unknown and that an {@code IllegalArgumentException}
91
   * should be thrown.
92
   *
93
   * @param adviceMode the value of the {@linkplain #getAdviceModeAttributeName()
94
   * advice mode attribute} for the annotation specified via generics.
95
   * @return array containing classes to import (empty array if none;
96
   * {@code null} if the given {@code AdviceMode} is unknown)
97
   */
98
  protected abstract String @Nullable [] selectImports(AdviceMode adviceMode);
99

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