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

TAKETODAY / today-infrastructure / 20224960533

15 Dec 2025 08:11AM UTC coverage: 84.388% (-0.02%) from 84.404%
20224960533

push

github

TAKETODAY
:white_check_mark: 在测试中排除 jacoco 初始化方法以避免干扰

61869 of 78367 branches covered (78.95%)

Branch coverage included in aggregate %.

145916 of 167860 relevant lines covered (86.93%)

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 java.util.Map;
23

24
import infra.beans.factory.annotation.Autowired;
25
import infra.beans.factory.parsing.Location;
26
import infra.beans.factory.parsing.Problem;
27
import infra.beans.factory.parsing.ProblemReporter;
28
import infra.core.type.MethodMetadata;
29
import infra.stereotype.Component;
30

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

46
  public final MethodMetadata metadata;
47

48
  public final ConfigurationClass configurationClass;
49

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

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

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

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

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

77
    Map<String, Object> attributes = configurationClass.metadata.getAnnotationAttributes(Configuration.class);
6✔
78
    if (attributes != null && (Boolean) attributes.get("proxyBeanMethods") && !metadata.isOverridable()) {
12✔
79
      // instance @Bean methods within @Configuration classes must be overridable to accommodate CGLIB
80
      problemReporter.error(new NonOverridableMethodError());
×
81
    }
82

83
  }
1✔
84

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

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

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

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

109
  private class AutowiredDeclaredMethodError extends Problem {
110

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

117
  private class VoidDeclaredMethodError extends Problem {
118

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

125
  private class NonOverridableMethodError extends Problem {
126

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

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

© 2026 Coveralls, Inc