Skip to content

Commit f558396

Browse files
committed
Introduced SpringNamingPolicy for CGLIB
Issue: SPR-11398 (cherry picked from commit 8c4e372)
1 parent a2ec8f8 commit f558396

File tree

6 files changed

+79
-37
lines changed

6 files changed

+79
-37
lines changed

spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@
3737
import org.springframework.aop.TargetSource;
3838
import org.springframework.aop.support.AopUtils;
3939
import org.springframework.cglib.core.CodeGenerationException;
40+
import org.springframework.cglib.core.SpringNamingPolicy;
4041
import org.springframework.cglib.proxy.Callback;
4142
import org.springframework.cglib.proxy.CallbackFilter;
4243
import org.springframework.cglib.proxy.Dispatcher;
@@ -181,20 +182,20 @@ public Object getProxy(ClassLoader classLoader) {
181182
}
182183
}
183184
enhancer.setSuperclass(proxySuperClass);
184-
enhancer.setStrategy(new MemorySafeUndeclaredThrowableStrategy(UndeclaredThrowableException.class));
185185
enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
186+
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
187+
enhancer.setStrategy(new MemorySafeUndeclaredThrowableStrategy(UndeclaredThrowableException.class));
186188
enhancer.setInterceptDuringConstruction(false);
187189

188190
Callback[] callbacks = getCallbacks(rootClass);
189-
enhancer.setCallbacks(callbacks);
190-
enhancer.setCallbackFilter(new ProxyCallbackFilter(
191-
this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
192-
193-
Class<?>[] types = new Class[callbacks.length];
191+
Class<?>[] types = new Class<?>[callbacks.length];
194192
for (int x = 0; x < types.length; x++) {
195193
types[x] = callbacks[x].getClass();
196194
}
195+
enhancer.setCallbackFilter(new ProxyCallbackFilter(
196+
this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
197197
enhancer.setCallbackTypes(types);
198+
enhancer.setCallbacks(callbacks);
198199

199200
// Generate the proxy class and create a proxy instance.
200201
Object proxy;
@@ -312,8 +313,7 @@ private Callback[] getCallbacks(Class<?> rootClass) throws Exception {
312313
Callback[] fixedCallbacks = new Callback[methods.length];
313314
this.fixedInterceptorMap = new HashMap<String, Integer>(methods.length);
314315

315-
// TODO: small memory optimisation here (can skip creation for
316-
// methods with no advice)
316+
// TODO: small memory optimisation here (can skip creation for methods with no advice)
317317
for (int x = 0; x < methods.length; x++) {
318318
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(methods[x], rootClass);
319319
fixedCallbacks[x] = new FixedChainStaticTargetInterceptor(
@@ -340,16 +340,15 @@ private Callback[] getCallbacks(Class<?> rootClass) throws Exception {
340340
*/
341341
private static Object processReturnType(Object proxy, Object target, Method method, Object retVal) {
342342
// Massage return value if necessary
343-
if (retVal != null && retVal == target &&
344-
!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
345-
// Special case: it returned "this".
346-
// Note that we can't help if the target sets a reference
347-
// to itself in another returned object.
343+
if (retVal != null && retVal == target && !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
344+
// Special case: it returned "this". Note that we can't help
345+
// if the target sets a reference to itself in another returned object.
348346
retVal = proxy;
349347
}
350348
Class<?> returnType = method.getReturnType();
351349
if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
352-
throw new AopInvocationException("Null return value from advice does not match primitive return type for: " + method);
350+
throw new AopInvocationException(
351+
"Null return value from advice does not match primitive return type for: " + method);
353352
}
354353
return retVal;
355354
}
@@ -851,7 +850,7 @@ else if (returnType.isPrimitive() || !returnType.isAssignableFrom(targetClass))
851850

852851
@Override
853852
public boolean equals(Object other) {
854-
if (other == this) {
853+
if (this == other) {
855854
return true;
856855
}
857856
if (!(other instanceof ProxyCallbackFilter)) {

spring-beans/src/main/java/org/springframework/beans/factory/support/CglibSubclassingInstantiationStrategy.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@
2323
import org.apache.commons.logging.LogFactory;
2424

2525
import org.springframework.beans.factory.BeanFactory;
26-
26+
import org.springframework.cglib.core.SpringNamingPolicy;
2727
import org.springframework.cglib.proxy.Callback;
2828
import org.springframework.cglib.proxy.CallbackFilter;
2929
import org.springframework.cglib.proxy.Enhancer;
@@ -72,7 +72,7 @@ protected Object instantiateWithMethodInjection(
7272
@Override
7373
protected Object instantiateWithMethodInjection(
7474
RootBeanDefinition beanDefinition, String beanName, BeanFactory owner,
75-
Constructor ctor, Object[] args) {
75+
Constructor<?> ctor, Object[] args) {
7676

7777
return new CglibSubclassCreator(beanDefinition, owner).instantiate(ctor, args);
7878
}
@@ -104,19 +104,18 @@ public CglibSubclassCreator(RootBeanDefinition beanDefinition, BeanFactory owner
104104
* Ignored if the ctor parameter is {@code null}.
105105
* @return new instance of the dynamically generated class
106106
*/
107-
public Object instantiate(Constructor ctor, Object[] args) {
107+
public Object instantiate(Constructor<?> ctor, Object[] args) {
108108
Enhancer enhancer = new Enhancer();
109109
enhancer.setSuperclass(this.beanDefinition.getBeanClass());
110+
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
110111
enhancer.setCallbackFilter(new CallbackFilterImpl());
111112
enhancer.setCallbacks(new Callback[] {
112113
NoOp.INSTANCE,
113114
new LookupOverrideMethodInterceptor(),
114115
new ReplaceOverrideMethodInterceptor()
115116
});
116117

117-
return (ctor == null) ?
118-
enhancer.create() :
119-
enhancer.create(ctor.getParameterTypes(), args);
118+
return (ctor != null ? enhancer.create(ctor.getParameterTypes(), args) : enhancer.create());
120119
}
121120

122121

spring-beans/src/main/java/org/springframework/beans/factory/support/SimpleInstantiationStrategy.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ public Object instantiate(RootBeanDefinition beanDefinition, String beanName, Be
6262
synchronized (beanDefinition.constructorArgumentLock) {
6363
constructorToUse = (Constructor<?>) beanDefinition.resolvedConstructorOrFactoryMethod;
6464
if (constructorToUse == null) {
65-
final Class clazz = beanDefinition.getBeanClass();
65+
final Class<?> clazz = beanDefinition.getBeanClass();
6666
if (clazz.isInterface()) {
6767
throw new BeanInstantiationException(clazz, "Specified class is an interface");
6868
}
6969
try {
7070
if (System.getSecurityManager() != null) {
7171
constructorToUse = AccessController.doPrivileged(new PrivilegedExceptionAction<Constructor>() {
72-
public Constructor run() throws Exception {
72+
public Constructor<?> run() throws Exception {
7373
return clazz.getDeclaredConstructor((Class[]) null);
7474
}
7575
});
@@ -132,7 +132,7 @@ public Object run() {
132132
* Instantiation should use the given constructor and parameters.
133133
*/
134134
protected Object instantiateWithMethodInjection(RootBeanDefinition beanDefinition,
135-
String beanName, BeanFactory owner, Constructor ctor, Object[] args) {
135+
String beanName, BeanFactory owner, Constructor<?> ctor, Object[] args) {
136136

137137
throw new UnsupportedOperationException(
138138
"Method Injection not supported in SimpleInstantiationStrategy");

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
3030
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
3131
import org.springframework.beans.factory.support.SimpleInstantiationStrategy;
32+
import org.springframework.cglib.core.SpringNamingPolicy;
3233
import org.springframework.cglib.proxy.Callback;
3334
import org.springframework.cglib.proxy.CallbackFilter;
3435
import org.springframework.cglib.proxy.Enhancer;
@@ -103,8 +104,9 @@ public Class<?> enhance(Class<?> configClass) {
103104
private Enhancer newEnhancer(Class<?> superclass) {
104105
Enhancer enhancer = new Enhancer();
105106
enhancer.setSuperclass(superclass);
106-
enhancer.setInterfaces(new Class<?>[] {EnhancedConfiguration.class});
107+
enhancer.setInterfaces(new Class<?>[]{EnhancedConfiguration.class});
107108
enhancer.setUseFactory(false);
109+
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
108110
enhancer.setCallbackFilter(CALLBACK_FILTER);
109111
enhancer.setCallbackTypes(CALLBACK_TYPES);
110112
return enhancer;
@@ -128,9 +130,9 @@ private Class<?> createClass(Enhancer enhancer) {
128130
* Facilitates idempotent behavior for {@link ConfigurationClassEnhancer#enhance(Class)}
129131
* through checking to see if candidate classes are already assignable to it, e.g.
130132
* have already been enhanced.
131-
* <p>Also extends {@link DisposableBean}, as all enhanced
132-
* {@code @Configuration} classes must de-register static CGLIB callbacks on
133-
* destruction, which is handled by the (private) {@code DisposableBeanMethodInterceptor}.
133+
* <p>Also extends {@link DisposableBean}, as all enhanced {@code @Configuration}
134+
* classes must de-register static CGLIB callbacks on destruction, which is handled
135+
* by the (private) {@code DisposableBeanMethodInterceptor}.
134136
* <p>Note that this interface is intended for framework-internal use only, however
135137
* must remain public in order to allow access to subclasses generated from other
136138
* packages (i.e. user code).
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2002-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cglib.core;
18+
19+
/**
20+
* Custom extension of CGLIB's {@link DefaultNamingPolicy}, modifying
21+
* the tag in generated class names from "ByCGLIB" to "BySpringCGLIB".
22+
*
23+
* <p>This is primarily designed to avoid clashes between a regular CGLIB
24+
* version (used by some other library) and Spring's embedded variant,
25+
* in case the same class happens to get proxied for different purposes.
26+
*
27+
* @author Juergen Hoeller
28+
* @since 3.2.8
29+
*/
30+
public class SpringNamingPolicy extends DefaultNamingPolicy {
31+
32+
public static final SpringNamingPolicy INSTANCE = new SpringNamingPolicy();
33+
34+
@Override
35+
protected String getTag() {
36+
return "BySpringCGLIB";
37+
}
38+
39+
}

spring-core/src/main/java/org/springframework/cglib/transform/impl/MemorySafeUndeclaredThrowableStrategy.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,20 +25,22 @@
2525
import org.springframework.cglib.transform.TransformingClassGenerator;
2626

2727
/**
28-
* Memory safe variant of {@link UndeclaredThrowableStrategy} ported from the latest
29-
* as yet unreleased cglib code.
28+
* Memory-safe variant of {@link UndeclaredThrowableStrategy} ported from the latest
29+
* as yet unreleased CGLIB code.
30+
*
31+
* @author Phillip Webb
32+
* @since 3.2.4
3033
*/
3134
public class MemorySafeUndeclaredThrowableStrategy extends DefaultGeneratorStrategy {
3235

3336
private static final MethodFilter TRANSFORM_FILTER = new MethodFilter() {
34-
public boolean accept(int access, String name, String desc, String signature,
35-
String[] exceptions) {
37+
public boolean accept(int access, String name, String desc, String signature, String[] exceptions) {
3638
return !TypeUtils.isPrivate(access) && name.indexOf('$') < 0;
3739
}
3840
};
3941

4042

41-
private Class wrapper;
43+
private Class<?> wrapper;
4244

4345

4446
public MemorySafeUndeclaredThrowableStrategy(Class wrapper) {
@@ -51,4 +53,5 @@ protected ClassGenerator transform(ClassGenerator cg) throws Exception {
5153
tr = new MethodFilterTransformer(TRANSFORM_FILTER, tr);
5254
return new TransformingClassGenerator(cg, tr);
5355
}
56+
5457
}

0 commit comments

Comments
 (0)