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

TAKETODAY / today-infrastructure / 20298495350

17 Dec 2025 09:45AM UTC coverage: 84.405% (+0.007%) from 84.398%
20298495350

push

github

TAKETODAY
:zap: 优化注解数据处理逻辑

- 将 Nullable 注解放置在返回值类型之前以符合新的语法规范
- 使用 MergedAnnotation 替代 Map 来获取注解属性,提升类型安全性
- 简化条件判断逻辑,通过 isPresent() 方法检查注解是否存在
- 更新注解属性访问方式,使用 getEnum() 等类型安全的方法
- 统一注解元数据处理接口的实现方式

61877 of 78357 branches covered (78.97%)

Branch coverage included in aggregate %.

145923 of 167837 relevant lines covered (86.94%)

3.71 hits per line

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

89.66
today-context/src/main/java/infra/context/annotation/ComponentMethod.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 infra.beans.factory.annotation.Autowired;
23
import infra.beans.factory.parsing.Location;
24
import infra.beans.factory.parsing.Problem;
25
import infra.beans.factory.parsing.ProblemReporter;
26
import infra.core.type.MethodMetadata;
27
import infra.stereotype.Component;
28

29
/**
30
 * Represents a {@link Configuration @Configuration} class method annotated with
31
 * {@link Component @Component}.
32
 *
33
 * @author Chris Beams
34
 * @author Juergen Hoeller
35
 * @author Sam Brannen
36
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
37
 * @see ConfigurationClass
38
 * @see ConfigurationClassParser
39
 * @see ConfigurationClassBeanDefinitionReader
40
 * @since 4.0
41
 */
42
final class ComponentMethod {
43

44
  public final MethodMetadata metadata;
45

46
  public final ConfigurationClass configurationClass;
47

48
  public ComponentMethod(MethodMetadata metadata, ConfigurationClass configurationClass) {
2✔
49
    this.metadata = metadata;
3✔
50
    this.configurationClass = configurationClass;
3✔
51
  }
1✔
52

53
  public Location getResourceLocation() {
54
    return new Location(this.configurationClass.resource, this.metadata);
9✔
55
  }
56

57
  @SuppressWarnings("NullAway")
58
  public void validate(ProblemReporter problemReporter) {
59
    if (metadata.isAnnotated(Autowired.class)) {
5✔
60
      // declared as @Autowired: semantic mismatch since @Component method arguments are autowired
61
      // in any case whereas @Autowired methods are setter-like methods on the containing class
62
      problemReporter.error(new AutowiredDeclaredMethodError());
×
63
    }
64

65
    if ("void".equals(metadata.getReturnTypeName())) {
6✔
66
      // declared as void: potential misuse of @Bean, maybe meant as init method instead?
67
      problemReporter.error(new VoidDeclaredMethodError());
×
68
    }
69

70
    if (metadata.isStatic()) {
4✔
71
      // static @Component methods have no further constraints to validate -> return immediately
72
      return;
1✔
73
    }
74

75
    var annotation = configurationClass.metadata.getAnnotation(Configuration.class);
6✔
76
    if (annotation.isPresent() && annotation.getBoolean("proxyBeanMethods") && !metadata.isOverridable()) {
11✔
77
      // instance @Bean methods within @Configuration classes must be overridable to accommodate CGLIB
78
      problemReporter.error(new NonOverridableMethodError());
×
79
    }
80

81
  }
1✔
82

83
  @Override
84
  public boolean equals(@Nullable Object other) {
85
    return (this == other || (other instanceof ComponentMethod that &&
14✔
86
            this.configurationClass.equals(that.configurationClass) &&
4✔
87
            getLocalMethodIdentifier(this.metadata).equals(getLocalMethodIdentifier(that.metadata))));
9!
88
  }
89

90
  @Override
91
  public int hashCode() {
92
    return this.configurationClass.hashCode() * 31 + getLocalMethodIdentifier(this.metadata).hashCode();
11✔
93
  }
94

95
  private static String getLocalMethodIdentifier(MethodMetadata metadata) {
96
    String metadataString = metadata.toString();
3✔
97
    int index = metadataString.indexOf(metadata.getDeclaringClassName());
5✔
98
    return (index >= 0 ? metadataString.substring(index + metadata.getDeclaringClassName().length()) :
11!
99
            metadataString);
×
100
  }
101

102
  @Override
103
  public String toString() {
104
    return "ComponentMethod: " + this.metadata;
5✔
105
  }
106

107
  private class AutowiredDeclaredMethodError extends Problem {
108

109
    AutowiredDeclaredMethodError() {
3✔
110
      super("@Component method '%s' must not be declared as autowired; remove the method-level @Autowired annotation."
9✔
111
              .formatted(metadata.getMethodName()), getResourceLocation());
5✔
112
    }
1✔
113
  }
114

115
  private class VoidDeclaredMethodError extends Problem {
116

117
    VoidDeclaredMethodError() {
3✔
118
      super("@Component method '%s' must not be declared as void; change the method's return type or its annotation."
9✔
119
              .formatted(metadata.getMethodName()), getResourceLocation());
5✔
120
    }
1✔
121
  }
122

123
  private class NonOverridableMethodError extends Problem {
124

125
    NonOverridableMethodError() {
3✔
126
      super(String.format("@Component method '%s' must not be private or final; change the method's modifiers to continue",
11✔
127
              metadata.getMethodName()), getResourceLocation());
3✔
128
    }
1✔
129
  }
130

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

© 2025 Coveralls, Inc