-
Notifications
You must be signed in to change notification settings - Fork 63
fix(#OWB-1450): Interceptor proxy memory leak #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import org.apache.webbeans.exception.ProxyGenerationException; | ||
| import org.apache.webbeans.exception.WebBeansConfigurationException; | ||
| import org.apache.webbeans.intercept.InterceptorResolutionService; | ||
| import org.apache.webbeans.portable.AnnotatedTypeImpl; | ||
| import org.apache.webbeans.util.Asserts; | ||
| import org.apache.webbeans.util.ExceptionUtil; | ||
| import org.apache.xbean.asm9.ClassWriter; | ||
|
|
@@ -39,12 +40,13 @@ | |
| import java.lang.reflect.Method; | ||
| import java.lang.reflect.Modifier; | ||
| import java.util.Collection; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
|
|
||
| /** | ||
| * Generate a dynamic subclass which has exactly 1 delegation point instance | ||
| * which get's set in the Constructor of the proxy. | ||
| * which gets set in the Constructor of the proxy. | ||
| * Any non-intercepted or decorated method will get delegated natively, | ||
| * All intercepted and decorated methods will get invoked via an InvocationHandler chain. | ||
| * | ||
|
|
@@ -67,7 +69,7 @@ public class InterceptorDecoratorProxyFactory extends AbstractProxyFactory | |
| * We need this to prevent filling up the ClassLoaders by | ||
| */ | ||
| private ConcurrentMap<Bean<?>, Class<?>> cachedProxyClasses = new ConcurrentHashMap<>(); | ||
| private ConcurrentMap<AnnotatedType<?>, Class<?>> cachedProxyClassesByAt = new ConcurrentHashMap<>(); | ||
| private ConcurrentMap<AnnotationTypeKey, Class<?>> cachedProxyClassesByAt = new ConcurrentHashMap<>(); | ||
|
|
||
|
|
||
| public InterceptorDecoratorProxyFactory(WebBeansContext webBeansContext) | ||
|
|
@@ -191,7 +193,7 @@ public synchronized <T> Class<T> createProxyClass(InterceptorResolutionService.B | |
| Class<T> proxyClass = createProxyClass( | ||
| classLoader, at.getJavaClass(), | ||
| intercepted.toArray(new Method[intercepted.size()]), others.toArray(new Method[others.size()])); | ||
| cachedProxyClassesByAt.put(at, proxyClass); | ||
| cachedProxyClassesByAt.put(new AnnotationTypeKey(at), proxyClass); | ||
| return proxyClass; | ||
| } | ||
|
|
||
|
|
@@ -224,7 +226,7 @@ private <T> Class<T> createProxyClass(ClassLoader classLoader, Class<T> classToP | |
| public <T> Class<T> getCachedProxyClass(InterceptorResolutionService.BeanInterceptorInfo interceptorInfo, | ||
| AnnotatedType<T> at, ClassLoader classLoader) | ||
| { | ||
| Class<T> value = (Class<T>) cachedProxyClassesByAt.get(at); | ||
| Class<T> value = (Class<T>) cachedProxyClassesByAt.get(new AnnotationTypeKey(at)); | ||
| if (value == null) | ||
| { | ||
| value = createProxyClass(interceptorInfo, at, classLoader); | ||
|
|
@@ -613,5 +615,55 @@ else if (methodIndex < 32267) | |
| mv.visitEnd(); | ||
| } | ||
|
|
||
| public static class AnnotationTypeKey<T> | ||
| { | ||
| private final AnnotatedType<T> annotatedType; | ||
|
|
||
|
|
||
| public AnnotationTypeKey(final AnnotatedType<T> annotatedType) | ||
| { | ||
| Asserts.assertNotNull(annotatedType, "annotatedType"); | ||
| this.annotatedType = annotatedType; | ||
| } | ||
|
|
||
| @Override | ||
| public final boolean equals(final Object o) | ||
| { | ||
| if (!(o instanceof AnnotationTypeKey)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| final AnnotatedType<?> at = ((AnnotationTypeKey<?>) o).annotatedType; | ||
| if (annotatedType == at) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (annotatedType.equals(at)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (annotatedType.getJavaClass() == at.getJavaClass()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. think it is not sufficient, misses annotations and other parts of the model no? |
||
| { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() | ||
| { | ||
| if (annotatedType instanceof AnnotatedTypeImpl) | ||
| { | ||
| final var at = (AnnotatedTypeImpl<T>) annotatedType; | ||
| return at.getJavaClass().hashCode(); | ||
|
|
||
| } | ||
| return Objects.hashCode(annotatedType); | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,11 +28,15 @@ | |
| import java.net.URL; | ||
| import java.net.URLClassLoader; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import org.apache.webbeans.config.WebBeansContext; | ||
| import org.apache.webbeans.configurator.AnnotatedTypeConfiguratorImpl; | ||
| import org.apache.webbeans.exception.WebBeansException; | ||
| import org.apache.webbeans.intercept.InterceptorResolutionService; | ||
| import org.apache.webbeans.portable.AnnotatedTypeImpl; | ||
| import org.apache.webbeans.test.AbstractUnitTest; | ||
| import org.apache.webbeans.test.component.intercept.webbeans.TransactionalInterceptor; | ||
| import org.apache.webbeans.test.interceptors.factory.beans.ClassInterceptedClass; | ||
|
|
@@ -41,22 +45,59 @@ | |
| import org.apache.webbeans.proxy.InterceptorHandler; | ||
| import org.apache.webbeans.proxy.OwbInterceptorProxy; | ||
| import org.apache.webbeans.test.interceptors.factory.beans.TonsOfMethodsInterceptedClass; | ||
| import org.apache.webbeans.util.Asserts; | ||
| import org.apache.webbeans.util.ClassUtil; | ||
| import org.apache.webbeans.test.util.CustomBaseType; | ||
| import org.apache.webbeans.test.util.CustomType; | ||
| import org.apache.webbeans.test.util.ExtendedSpecificClass; | ||
| import org.apache.webbeans.test.util.GenericInterface; | ||
| import org.apache.webbeans.test.util.SpecificClass; | ||
| import org.apache.webbeans.util.WebBeansUtil; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.assertSame; | ||
|
|
||
|
|
||
| /** | ||
| * Test the {@link org.apache.webbeans.proxy.InterceptorDecoratorProxyFactory} | ||
| */ | ||
| public class InterceptorDecoratorProxyFactoryTest extends AbstractUnitTest | ||
| { | ||
|
|
||
| @Test | ||
| public void testEnsureOneProxyPerAT() | ||
| { | ||
| // we take a fresh URLClassLoader to not blur the test classpath with synthetic classes. | ||
| ClassLoader classLoader = new URLClassLoader(new URL[0]); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try()? |
||
|
|
||
| final WebBeansContext context = WebBeansContext.currentInstance(); | ||
| final InterceptorDecoratorProxyFactory factory = context.getInterceptorDecoratorProxyFactory(); | ||
|
|
||
| // create first annotated type and it's associated proxy | ||
| final var at = new AnnotatedTypeImpl<DummyBean>(context, context.getBeanManagerImpl().createAnnotatedType(DummyBean.class)); | ||
| final var configurator = new AnnotatedTypeConfiguratorImpl<DummyBean>(context, at); | ||
| final AnnotatedTypeImpl<DummyBean> newAnnotatedType = configurator.getNewAnnotatedType(); | ||
| final InterceptorResolutionService.BeanInterceptorInfo interceptorInfo = context.getInterceptorResolutionService() | ||
| .calculateInterceptorInfo(newAnnotatedType.getTypeClosure(), Collections.emptySet(), newAnnotatedType, true); | ||
| final Class<DummyBean> subClass = factory.getCachedProxyClass(interceptorInfo, newAnnotatedType, classLoader); | ||
|
|
||
| Asserts.assertNotNull(subClass); | ||
|
|
||
| // create second annotated type and it's associated proxy | ||
| final var at2 = new AnnotatedTypeImpl<DummyBean>(context, context.getBeanManagerImpl().createAnnotatedType(DummyBean.class)); | ||
| final var configurator2 = new AnnotatedTypeConfiguratorImpl<DummyBean>(context, at2); | ||
| final AnnotatedTypeImpl<DummyBean> newAnnotatedType2 = configurator2.getNewAnnotatedType(); | ||
| final InterceptorResolutionService.BeanInterceptorInfo interceptorInfo2 = context.getInterceptorResolutionService() | ||
| .calculateInterceptorInfo(newAnnotatedType2.getTypeClosure(), Collections.emptySet(), newAnnotatedType2, true); | ||
| final Class<DummyBean> subClass2 = factory.getCachedProxyClass(interceptorInfo2, newAnnotatedType2, classLoader); | ||
|
|
||
| Asserts.assertNotNull(subClass2); | ||
|
|
||
| // the 2 proxy instances should be the same and the cache in the factory should be filed in with one proxy only | ||
| assertSame(subClass, subClass2); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSimpleProxyCreation() throws Exception | ||
| { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'd cache (=precompute in the constructor) the hashcode as in beancachekey since it is hash structure oriented