Skip to content

Commit 587877c

Browse files
committed
Add MemorySafeUndeclaredThrowableStrategy
Port the latest unreleased UndeclaredThrowableStrategy implementation from cglib to fix a memory-leak present in v3. Issue: SPR-10709
1 parent d0d670c commit 587877c

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

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

Lines changed: 3 additions & 3 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-2013 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.
@@ -34,7 +34,7 @@
3434
import org.springframework.cglib.proxy.MethodInterceptor;
3535
import org.springframework.cglib.proxy.MethodProxy;
3636
import org.springframework.cglib.proxy.NoOp;
37-
import org.springframework.cglib.transform.impl.UndeclaredThrowableStrategy;
37+
import org.springframework.cglib.transform.impl.MemorySafeUndeclaredThrowableStrategy;
3838

3939
import org.aopalliance.aop.Advice;
4040
import org.aopalliance.intercept.MethodInvocation;
@@ -183,7 +183,7 @@ public Object getProxy(ClassLoader classLoader) {
183183
}
184184
}
185185
enhancer.setSuperclass(proxySuperClass);
186-
enhancer.setStrategy(new UndeclaredThrowableStrategy(UndeclaredThrowableException.class));
186+
enhancer.setStrategy(new MemorySafeUndeclaredThrowableStrategy(UndeclaredThrowableException.class));
187187
enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
188188
enhancer.setInterceptDuringConstruction(false);
189189

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2002-2013 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.transform.impl;
18+
19+
import org.springframework.cglib.core.ClassGenerator;
20+
import org.springframework.cglib.core.DefaultGeneratorStrategy;
21+
import org.springframework.cglib.core.TypeUtils;
22+
import org.springframework.cglib.transform.ClassTransformer;
23+
import org.springframework.cglib.transform.MethodFilter;
24+
import org.springframework.cglib.transform.MethodFilterTransformer;
25+
import org.springframework.cglib.transform.TransformingClassGenerator;
26+
27+
/**
28+
* Memory safe variant of {@link UndeclaredThrowableStrategy} ported from the latest
29+
* as yet unreleased cglib code.
30+
*/
31+
public class MemorySafeUndeclaredThrowableStrategy extends DefaultGeneratorStrategy {
32+
33+
private static final MethodFilter TRANSFORM_FILTER = new MethodFilter() {
34+
public boolean accept(int access, String name, String desc, String signature,
35+
String[] exceptions) {
36+
return !TypeUtils.isPrivate(access) && name.indexOf('$') < 0;
37+
}
38+
};
39+
40+
41+
private Class wrapper;
42+
43+
44+
public MemorySafeUndeclaredThrowableStrategy(Class wrapper) {
45+
this.wrapper = wrapper;
46+
}
47+
48+
49+
protected ClassGenerator transform(ClassGenerator cg) throws Exception {
50+
ClassTransformer tr = new UndeclaredThrowableTransformer(wrapper);
51+
tr = new MethodFilterTransformer(TRANSFORM_FILTER, tr);
52+
return new TransformingClassGenerator(cg, tr);
53+
}
54+
}

0 commit comments

Comments
 (0)