Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Copy link
Contributor

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

}

@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())
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
Copy link
Contributor

Choose a reason for hiding this comment

The 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
{
Expand Down