Skip to content

Commit

Permalink
make proxies names well defined
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas authored and pmuir committed Aug 26, 2010
1 parent 39cde6d commit 7e3f240
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 24 deletions.
Expand Up @@ -201,7 +201,7 @@ protected T applyDecorators(T instance, CreationalContext<T> creationalContext,
{
T proxy = null;
TargetBeanInstance beanInstance = new TargetBeanInstance(this, instance);
ProxyFactory<T> proxyFactory = new ProxyFactory<T>(getType(), getTypes());
ProxyFactory<T> proxyFactory = new ProxyFactory<T>(getType(), getTypes(), this);
DecorationHelper<T> decorationHelper = new DecorationHelper<T>(beanInstance, proxyFactory.getProxyClass(), beanManager, decorators);

DecorationHelper.getHelperStack().push(decorationHelper);
Expand Down
2 changes: 1 addition & 1 deletion impl/src/main/java/org/jboss/weld/bean/DecoratorImpl.java
Expand Up @@ -141,7 +141,7 @@ protected void initDelegateInjectionPoint()
this.delegateInjectionPoint = getDelegateInjectionPoints().iterator().next();
if (getWeldAnnotated().isAbstract())
{
Class<T> clazz = new DecoratorProxyFactory<T>(getWeldAnnotated().getJavaClass(), delegateInjectionPoint).getProxyClass();
Class<T> clazz = new DecoratorProxyFactory<T>(getWeldAnnotated().getJavaClass(), delegateInjectionPoint, this).getProxyClass();
proxyClassForAbstractDecorators = beanManager.getServices().get(ClassTransformer.class).loadClass(clazz);
constructorForAbstractDecorator = WeldConstructorImpl.of(
proxyClassForAbstractDecorators.getDeclaredWeldConstructor(getConstructor().getSignature()),
Expand Down
2 changes: 1 addition & 1 deletion impl/src/main/java/org/jboss/weld/bean/ManagedBean.java
Expand Up @@ -591,7 +591,7 @@ protected T applyInterceptors(T instance, final CreationalContext<T> creationalC
MethodHandler methodHandler = interceptorProxyCreator.createMethodHandler(instance, getType(), getBeanManager().getServices().get(InterceptionMetadataService.class).getInterceptorMetadataRegistry().getInterceptorClassMetadata(WeldClassReference.of(getWeldAnnotated()), true));
TargetBeanInstance targetInstance = new TargetBeanInstance(this, instance);
targetInstance.setInterceptorsHandler(methodHandler);
instance = new ProxyFactory<T>(getType(), getTypes()).create(targetInstance);
instance = new ProxyFactory<T>(getType(), getTypes(), this).create(targetInstance);
}

}
Expand Down
2 changes: 1 addition & 1 deletion impl/src/main/java/org/jboss/weld/bean/SessionBean.java
Expand Up @@ -246,7 +246,7 @@ protected void initTypes()

protected void initProxyClass()
{
this.proxyClass = new EnterpriseProxyFactory<T>(getWeldAnnotated().getJavaClass(), getTypes()).getProxyClass();
this.proxyClass = new EnterpriseProxyFactory<T>(getWeldAnnotated().getJavaClass(), this).getProxyClass();
}

/**
Expand Down
Expand Up @@ -43,7 +43,7 @@ protected AbstractEEBean(Class<T> type, Callable<T> callable, BeanManagerImpl be
this.types = new HashSet<Type>();
this.types.add(Object.class);
this.types.add(type);
this.proxy = new ProxyFactory<T>(type, types).create(new EnterpriseTargetBeanInstance(type, new CallableMethodHandler(callable)));
this.proxy = new ProxyFactory<T>(type, types, this).create(new EnterpriseTargetBeanInstance(type, new CallableMethodHandler(callable)));
}

public T create(CreationalContext<T> creationalContext)
Expand Down
Expand Up @@ -153,7 +153,7 @@ public T create(CreationalContext<T> creationalContext)
else
{
BeanInstance proxyBeanInstance = new EnterpriseTargetBeanInstance(getTypes(), new CallableMethodHandler(new EEResourceCallable<T>(getBeanManager(), this, creationalContext)));
return new ProxyFactory<T>(getType(), getTypes()).create(proxyBeanInstance);
return new ProxyFactory<T>(getType(), getTypes(), this).create(proxyBeanInstance);
}
}

Expand Down
Expand Up @@ -86,7 +86,7 @@ private static <T> T createClientProxy(Bean<T> bean, String id) throws RuntimeEx
{
ContextBeanInstance<T> beanInstance = new ContextBeanInstance<T>(bean, id);
TypeInfo typeInfo = TypeInfo.of(bean.getTypes());
return new ProxyFactory<T>(typeInfo.getSuperClass(), bean.getTypes()).create(beanInstance);
return new ProxyFactory<T>(typeInfo.getSuperClass(), bean.getTypes(), bean).create(beanInstance);
}

/**
Expand Down
Expand Up @@ -30,6 +30,8 @@
import javassist.bytecode.Opcode;
import javassist.util.proxy.MethodHandler;

import javax.enterprise.inject.spi.Bean;

import org.jboss.interceptor.util.proxy.TargetInstanceProxy;
import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.injection.FieldInjectionPoint;
Expand All @@ -54,9 +56,9 @@ public class DecoratorProxyFactory<T> extends ProxyFactory<T>
private final WeldInjectionPoint<?, ?> delegateInjectionPoint;
private final Field delegateField;

public DecoratorProxyFactory(Class<T> proxyType, WeldInjectionPoint<?, ?> delegateInjectionPoint)
public DecoratorProxyFactory(Class<T> proxyType, WeldInjectionPoint<?, ?> delegateInjectionPoint, Bean<?> bean)
{
super(proxyType, Collections.EMPTY_SET);
super(proxyType, Collections.EMPTY_SET, bean);
this.delegateInjectionPoint = delegateInjectionPoint;
if (delegateInjectionPoint instanceof FieldInjectionPoint<?, ?>)
{
Expand Down
Expand Up @@ -18,12 +18,12 @@
package org.jboss.weld.bean.proxy;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Set;

import javassist.bytecode.AccessFlag;
import javassist.bytecode.ClassFile;

import javax.enterprise.inject.spi.Bean;

import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.util.bytecode.MethodUtils;

Expand All @@ -41,9 +41,9 @@ public class EnterpriseProxyFactory<T> extends ProxyFactory<T>
*
* @param proxiedBeanType the actual enterprise bean
*/
public EnterpriseProxyFactory(Class<T> proxiedBeanType, Set<Type> localBusinessInterfaces)
public EnterpriseProxyFactory(Class<T> proxiedBeanType, Bean<T> bean)
{
super(proxiedBeanType, localBusinessInterfaces);
super(proxiedBeanType, bean.getTypes(), bean);
}

@Override
Expand Down
51 changes: 43 additions & 8 deletions impl/src/main/java/org/jboss/weld/bean/proxy/ProxyFactory.java
Expand Up @@ -46,11 +46,14 @@
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyObject;

import javax.enterprise.inject.spi.Bean;

import org.jboss.interceptor.proxy.LifecycleMixin;
import org.jboss.interceptor.util.proxy.TargetInstanceProxy;
import org.jboss.weld.Container;
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.jboss.weld.serialization.spi.ProxyServices;
import org.jboss.weld.util.Proxies.TypeInfo;
import org.jboss.weld.util.bytecode.Boxing;
Expand Down Expand Up @@ -79,6 +82,7 @@ public class ProxyFactory<T>
protected static final LocLogger log = loggerFactory().getLogger(BEAN);
// Default proxy class name suffix
public static final String PROXY_SUFFIX = "Proxy";
public static final String DEFAULT_PROXY_PACKAGE = "org.jboss.weld.proxies";

private final Class<?> beanType;
private final Set<Class<?>> additionalInterfaces = new HashSet<Class<?>>();
Expand All @@ -88,13 +92,30 @@ public class ProxyFactory<T>
private static final String FIRST_SERIALIZATION_PHASE_COMPLETE_FIELD_NAME = "firstSerializationPhaseComplete";

/**
* Creates a new proxy factory with only the type of proxy specified.
* created a new proxy factory from a bean instance. The proxy name is
* generated from the bean id
*
* @param proxiedBeanType
* @param businessInterfaces
* @param bean
*/
public ProxyFactory(Class<?> proxiedBeanType, Set<? extends Type> typeClosure, Bean<?> bean)
{
this(proxiedBeanType, typeClosure, getProxyName(proxiedBeanType, typeClosure, bean));
}

/**
* Creates a new proxy factory when the name of the proxy class is already
* known, such as during de-serialization
*
* @param proxiedBeanType the super-class for this proxy class
* @param typeClosure the bean types of the bean
* @param the name of the proxy class
*
*/
public ProxyFactory(Class<?> proxiedBeanType, Set<? extends Type> businessInterfaces)
public ProxyFactory(Class<?> proxiedBeanType, Set<? extends Type> typeClosure, String proxyName)
{
for (Type type : businessInterfaces)
for (Type type : typeClosure)
{
Class<?> c = Reflections.getRawType(type);
// Ignore no-interface views, they are dealt with proxiedBeanType
Expand All @@ -104,7 +125,7 @@ public ProxyFactory(Class<?> proxiedBeanType, Set<? extends Type> businessInterf
addInterface(c);
}
}
TypeInfo typeInfo = TypeInfo.of(businessInterfaces);
TypeInfo typeInfo = TypeInfo.of(typeClosure);
Class<?> superClass = typeInfo.getSuperClass();
superClass = superClass == null ? Object.class : superClass;
if (superClass.equals(Object.class))
Expand All @@ -122,6 +143,13 @@ public ProxyFactory(Class<?> proxiedBeanType, Set<? extends Type> businessInterf
}
this.beanType = superClass;
addDefaultAdditionalInterfaces();
baseProxyName = proxyName;
}

private static String getProxyName(Class<?> proxiedBeanType, Set<? extends Type> typeClosure, Bean<?> bean)
{
TypeInfo typeInfo = TypeInfo.of(typeClosure);
String proxyPackage;
if (proxiedBeanType.equals(Object.class))
{
Class<?> superInterface = typeInfo.getSuperInterface();
Expand All @@ -131,13 +159,16 @@ public ProxyFactory(Class<?> proxiedBeanType, Set<? extends Type> businessInterf
}
else
{
baseProxyName = superInterface.getName();
proxyPackage=DEFAULT_PROXY_PACKAGE;
}
}
else
{
baseProxyName = proxiedBeanType.getName();
proxyPackage = proxiedBeanType.getPackage().getName();
}
String beanId = Container.instance().services().get(ContextualStore.class).putIfAbsent(bean);
String className = beanId.replace('.', '$').replace(' ', '_');
return proxyPackage + '.' + className;
}

/**
Expand Down Expand Up @@ -207,7 +238,12 @@ public T create(BeanInstance beanInstance)
@SuppressWarnings("unchecked")
public Class<T> getProxyClass()
{
String proxyClassName = getBaseProxyName() + "_$$_Weld" + getProxyNameSuffix();
String suffix = "_$$_Weld" + getProxyNameSuffix();
String proxyClassName = getBaseProxyName();
if (!proxyClassName.endsWith(suffix))
{
proxyClassName = proxyClassName + suffix;
}
if (proxyClassName.startsWith("java"))
{
proxyClassName = proxyClassName.replaceFirst("java", "org.jboss.weld");
Expand Down Expand Up @@ -538,7 +574,6 @@ protected void addMethodsFromClass(ClassFile proxyClassType)
{
// Add all methods from the class heirachy
Class<?> cls = beanType;
Set<Class> interfaces = new HashSet<Class>();
while (cls != null)
{
for (Method method : cls.getDeclaredMethods())
Expand Down
Expand Up @@ -107,7 +107,7 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
Class<?> proxyClass = null;
if (proxyClassName.endsWith(ProxyFactory.PROXY_SUFFIX))
{
proxyClass = generateClientProxyClass(proxyBeanType, proxyBeanInterfaces);
proxyClass = generateClientProxyClass(proxyBeanType, proxyBeanInterfaces, proxyClassName);
}
else
{
Expand Down Expand Up @@ -151,8 +151,8 @@ Object writeReplace() throws ObjectStreamException
return writeProxy ? proxyObject : this;
}

private <T> Class<?> generateClientProxyClass(Class<T> beanType, Set<Type> interfaces)
private <T> Class<?> generateClientProxyClass(Class<T> beanType, Set<Type> interfaces, String proxyClassName)
{
return new ProxyFactory<T>(beanType, interfaces).getProxyClass();
return new ProxyFactory<T>(beanType, interfaces, proxyClassName).getProxyClass();
}
}

0 comments on commit 7e3f240

Please sign in to comment.