25
25
import org .springframework .beans .BeanInstantiationException ;
26
26
import org .springframework .beans .BeanUtils ;
27
27
import org .springframework .beans .factory .BeanFactory ;
28
-
29
28
import org .springframework .cglib .core .SpringNamingPolicy ;
30
29
import org .springframework .cglib .proxy .Callback ;
31
30
import org .springframework .cglib .proxy .CallbackFilter ;
@@ -89,14 +88,13 @@ protected Object instantiateWithMethodInjection(RootBeanDefinition beanDefinitio
89
88
*/
90
89
private static class CglibSubclassCreator {
91
90
92
- private static final Class <?>[] CALLBACK_TYPES = new Class <?>[] { NoOp . class ,
93
- LookupOverrideMethodInterceptor .class , ReplaceOverrideMethodInterceptor .class };
91
+ private static final Class <?>[] CALLBACK_TYPES = new Class <?>[]
92
+ { NoOp . class , LookupOverrideMethodInterceptor .class , ReplaceOverrideMethodInterceptor .class };
94
93
95
94
private final RootBeanDefinition beanDefinition ;
96
95
97
96
private final BeanFactory owner ;
98
97
99
-
100
98
CglibSubclassCreator (RootBeanDefinition beanDefinition , BeanFactory owner ) {
101
99
this .beanDefinition = beanDefinition ;
102
100
this .owner = owner ;
@@ -113,7 +111,6 @@ private static class CglibSubclassCreator {
113
111
*/
114
112
Object instantiate (Constructor <?> ctor , Object [] args ) {
115
113
Class <?> subclass = createEnhancedSubclass (this .beanDefinition );
116
-
117
114
Object instance ;
118
115
if (ctor == null ) {
119
116
instance = BeanUtils .instantiate (subclass );
@@ -123,19 +120,17 @@ Object instantiate(Constructor<?> ctor, Object[] args) {
123
120
Constructor <?> enhancedSubclassConstructor = subclass .getConstructor (ctor .getParameterTypes ());
124
121
instance = enhancedSubclassConstructor .newInstance (args );
125
122
}
126
- catch (Exception e ) {
123
+ catch (Exception ex ) {
127
124
throw new BeanInstantiationException (this .beanDefinition .getBeanClass (), String .format (
128
- "Failed to invoke construcor for CGLIB enhanced subclass [%s]" , subclass .getName ()), e );
125
+ "Failed to invoke constructor for CGLIB enhanced subclass [%s]" , subclass .getName ()), ex );
129
126
}
130
127
}
131
-
132
128
// SPR-10785: set callbacks directly on the instance instead of in the
133
129
// enhanced class (via the Enhancer) in order to avoid memory leaks.
134
130
Factory factory = (Factory ) instance ;
135
- factory .setCallbacks (new Callback [] { NoOp .INSTANCE ,//
136
- new LookupOverrideMethodInterceptor (beanDefinition , owner ),//
137
- new ReplaceOverrideMethodInterceptor (beanDefinition , owner ) });
138
-
131
+ factory .setCallbacks (new Callback [] {NoOp .INSTANCE ,
132
+ new LookupOverrideMethodInterceptor (this .beanDefinition , this .owner ),
133
+ new ReplaceOverrideMethodInterceptor (this .beanDefinition , this .owner )});
139
134
return instance ;
140
135
}
141
136
@@ -153,6 +148,7 @@ private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
153
148
}
154
149
}
155
150
151
+
156
152
/**
157
153
* Class providing hashCode and equals methods required by CGLIB to
158
154
* ensure that CGLIB doesn't generate a distinct class per bean.
@@ -162,7 +158,6 @@ private static class CglibIdentitySupport {
162
158
163
159
private final RootBeanDefinition beanDefinition ;
164
160
165
-
166
161
CglibIdentitySupport (RootBeanDefinition beanDefinition ) {
167
162
this .beanDefinition = beanDefinition ;
168
163
}
@@ -173,8 +168,8 @@ RootBeanDefinition getBeanDefinition() {
173
168
174
169
@ Override
175
170
public boolean equals (Object other ) {
176
- return other . getClass ().equals (this .getClass ())
177
- && (( CglibIdentitySupport ) other ).getBeanDefinition (). equals ( this . getBeanDefinition ( ));
171
+ return ( getClass ().equals (other .getClass ()) &&
172
+ this . beanDefinition . equals ((( CglibIdentitySupport ) other ).beanDefinition ));
178
173
}
179
174
180
175
@ Override
@@ -183,14 +178,14 @@ public int hashCode() {
183
178
}
184
179
}
185
180
181
+
186
182
/**
187
183
* CGLIB callback for filtering method interception behavior.
188
184
*/
189
185
private static class MethodOverrideCallbackFilter extends CglibIdentitySupport implements CallbackFilter {
190
186
191
187
private static final Log logger = LogFactory .getLog (MethodOverrideCallbackFilter .class );
192
188
193
-
194
189
MethodOverrideCallbackFilter (RootBeanDefinition beanDefinition ) {
195
190
super (beanDefinition );
196
191
}
@@ -210,11 +205,12 @@ else if (methodOverride instanceof LookupOverride) {
210
205
else if (methodOverride instanceof ReplaceOverride ) {
211
206
return METHOD_REPLACER ;
212
207
}
213
- throw new UnsupportedOperationException ("Unexpected MethodOverride subclass: "
214
- + methodOverride .getClass ().getName ());
208
+ throw new UnsupportedOperationException ("Unexpected MethodOverride subclass: " +
209
+ methodOverride .getClass ().getName ());
215
210
}
216
211
}
217
212
213
+
218
214
/**
219
215
* CGLIB MethodInterceptor to override methods, replacing them with an
220
216
* implementation that returns a bean looked up in the container.
@@ -223,7 +219,6 @@ private static class LookupOverrideMethodInterceptor extends CglibIdentitySuppor
223
219
224
220
private final BeanFactory owner ;
225
221
226
-
227
222
LookupOverrideMethodInterceptor (RootBeanDefinition beanDefinition , BeanFactory owner ) {
228
223
super (beanDefinition );
229
224
this .owner = owner ;
@@ -245,7 +240,6 @@ private static class ReplaceOverrideMethodInterceptor extends CglibIdentitySuppo
245
240
246
241
private final BeanFactory owner ;
247
242
248
-
249
243
ReplaceOverrideMethodInterceptor (RootBeanDefinition beanDefinition , BeanFactory owner ) {
250
244
super (beanDefinition );
251
245
this .owner = owner ;
@@ -255,7 +249,7 @@ private static class ReplaceOverrideMethodInterceptor extends CglibIdentitySuppo
255
249
public Object intercept (Object obj , Method method , Object [] args , MethodProxy mp ) throws Throwable {
256
250
ReplaceOverride ro = (ReplaceOverride ) getBeanDefinition ().getMethodOverrides ().getOverride (method );
257
251
// TODO could cache if a singleton for minor performance optimization
258
- MethodReplacer mr = owner .getBean (ro .getMethodReplacerBeanName (), MethodReplacer .class );
252
+ MethodReplacer mr = this . owner .getBean (ro .getMethodReplacerBeanName (), MethodReplacer .class );
259
253
return mr .reimplement (obj , method , args );
260
254
}
261
255
}
0 commit comments