• 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

90.0
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.parsing.Location;
25
import infra.beans.factory.parsing.Problem;
26
import infra.beans.factory.parsing.ProblemReporter;
27
import infra.core.type.MethodMetadata;
28
import infra.stereotype.Component;
29

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

45
  public final MethodMetadata metadata;
46

47
  public final ConfigurationClass configurationClass;
48

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

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

58
  @SuppressWarnings("NullAway")
59
  public void validate(ProblemReporter problemReporter) {
60
    if ("void".equals(metadata.getReturnTypeName())) {
6✔
61
      // declared as void: potential misuse of @Bean, maybe meant as init method instead?
62
      problemReporter.error(new VoidDeclaredMethodError());
×
63
    }
64

65
    if (metadata.isStatic()) {
4✔
66
      // static @Component methods have no further constraints to validate -> return immediately
67
      return;
1✔
68
    }
69

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

76
  }
1✔
77

78
  @Override
79
  public boolean equals(@Nullable Object other) {
80
    return (this == other || (other instanceof ComponentMethod that &&
14✔
81
            this.configurationClass.equals(that.configurationClass) &&
4✔
82
            getLocalMethodIdentifier(this.metadata).equals(getLocalMethodIdentifier(that.metadata))));
9!
83
  }
84

85
  @Override
86
  public int hashCode() {
87
    return this.configurationClass.hashCode() * 31 + getLocalMethodIdentifier(this.metadata).hashCode();
11✔
88
  }
89

90
  private static String getLocalMethodIdentifier(MethodMetadata metadata) {
91
    String metadataString = metadata.toString();
3✔
92
    int index = metadataString.indexOf(metadata.getDeclaringClassName());
5✔
93
    return (index >= 0 ? metadataString.substring(index + metadata.getDeclaringClassName().length()) :
11!
94
            metadataString);
×
95
  }
96

97
  @Override
98
  public String toString() {
99
    return "ComponentMethod: " + this.metadata;
5✔
100
  }
101

102
  private class VoidDeclaredMethodError extends Problem {
103

104
    VoidDeclaredMethodError() {
3✔
105
      super("@Bean method '%s' must not be declared as void; change the method's return type or its annotation."
9✔
106
              .formatted(metadata.getMethodName()), getResourceLocation());
5✔
107
    }
1✔
108
  }
109

110
  private class NonOverridableMethodError extends Problem {
111

112
    NonOverridableMethodError() {
3✔
113
      super(String.format("@Component method '%s' must not be private or final; change the method's modifiers to continue",
11✔
114
              metadata.getMethodName()), getResourceLocation());
3✔
115
    }
1✔
116
  }
117

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