/
BeanMethod.java
108 lines (87 loc) · 3.53 KB
/
BeanMethod.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.annotation;
import java.util.Map;
import org.springframework.beans.factory.parsing.Problem;
import org.springframework.beans.factory.parsing.ProblemReporter;
import org.springframework.core.type.MethodMetadata;
import org.springframework.lang.Nullable;
/**
* Represents a {@link Configuration @Configuration} class method annotated with
* {@link Bean @Bean}.
*
* @author Chris Beams
* @author Juergen Hoeller
* @author Sam Brannen
* @since 3.0
* @see ConfigurationClass
* @see ConfigurationClassParser
* @see ConfigurationClassBeanDefinitionReader
*/
final class BeanMethod extends ConfigurationMethod {
BeanMethod(MethodMetadata metadata, ConfigurationClass configurationClass) {
super(metadata, configurationClass);
}
@Override
public void validate(ProblemReporter problemReporter) {
if ("void".equals(getMetadata().getReturnTypeName())) {
// declared as void: potential misuse of @Bean, maybe meant as init method instead?
problemReporter.error(new VoidDeclaredMethodError());
}
if (getMetadata().isStatic()) {
// static @Bean methods have no further constraints to validate -> return immediately
return;
}
Map<String, Object> attributes =
getConfigurationClass().getMetadata().getAnnotationAttributes(Configuration.class.getName());
if (attributes != null && (Boolean) attributes.get("proxyBeanMethods") && !getMetadata().isOverridable()) {
// instance @Bean methods within @Configuration classes must be overridable to accommodate CGLIB
problemReporter.error(new NonOverridableMethodError());
}
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof BeanMethod that &&
this.configurationClass.equals(that.configurationClass) &&
getLocalMethodIdentifier(this.metadata).equals(getLocalMethodIdentifier(that.metadata))));
}
@Override
public int hashCode() {
return this.configurationClass.hashCode() * 31 + getLocalMethodIdentifier(this.metadata).hashCode();
}
@Override
public String toString() {
return "BeanMethod: " + this.metadata;
}
private static String getLocalMethodIdentifier(MethodMetadata metadata) {
String metadataString = metadata.toString();
int index = metadataString.indexOf(metadata.getDeclaringClassName());
return (index >= 0 ? metadataString.substring(index + metadata.getDeclaringClassName().length()) :
metadataString);
}
private class VoidDeclaredMethodError extends Problem {
VoidDeclaredMethodError() {
super("@Bean method '%s' must not be declared as void; change the method's return type or its annotation."
.formatted(getMetadata().getMethodName()), getResourceLocation());
}
}
private class NonOverridableMethodError extends Problem {
NonOverridableMethodError() {
super("@Bean method '%s' must not be private or final; change the method's modifiers to continue."
.formatted(getMetadata().getMethodName()), getResourceLocation());
}
}
}