Skip to content

Commit

Permalink
weld-579
Browse files Browse the repository at this point in the history
  • Loading branch information
mbogoevici authored and pmuir committed Oct 14, 2010
1 parent e39c8ba commit 8a7e6c7
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 28 deletions.
Expand Up @@ -17,12 +17,14 @@

package org.jboss.weld.bean;

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

import javax.enterprise.inject.spi.Decorator;

import org.jboss.weld.introspector.MethodSignature;
import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.introspector.WeldMethod;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.util.Decorators;
Expand All @@ -38,7 +40,7 @@ public class CustomDecoratorWrapper<T> extends ForwardingDecorator<T> implements
private Decorator<T> delegate;
private WeldClass<T> weldClass;

private Set<MethodSignature> decoratedMethodSignatures;
private Map<MethodSignature, WeldMethod<?,?>> decoratorMethods;

public static <T> CustomDecoratorWrapper<T> of(Decorator<T> delegate, BeanManagerImpl beanManager)
{
Expand All @@ -49,7 +51,7 @@ private CustomDecoratorWrapper(Decorator<T> delegate, BeanManagerImpl beanManage
{
this.delegate = delegate;
this.weldClass = beanManager.getServices().get(ClassTransformer.class).loadClass((Class<T>) delegate.getBeanClass());
this.decoratedMethodSignatures = Decorators.getDecoratedMethodSignatures(beanManager, delegate.getDecoratedTypes());
this.decoratorMethods = Decorators.getDecoratorMethods(beanManager, delegate.getDecoratedTypes(), this.weldClass);
}

@Override
Expand All @@ -63,9 +65,8 @@ public WeldClass<?> getWeldAnnotated()
return weldClass;
}

public Set<MethodSignature> getDecoratedMethodSignatures()
public WeldMethod<?,?> getDecoratorMethod(Method method)
{
return decoratedMethodSignatures;
return Decorators.findDecoratorMethod(this, decoratorMethods, method);
}

}
17 changes: 8 additions & 9 deletions impl/src/main/java/org/jboss/weld/bean/DecoratorImpl.java
Expand Up @@ -26,11 +26,10 @@

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.*;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedMethod;
Expand All @@ -47,6 +46,7 @@
import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.introspector.WeldConstructor;
import org.jboss.weld.introspector.WeldMethod;
import org.jboss.weld.introspector.jlr.MethodSignatureImpl;
import org.jboss.weld.introspector.jlr.WeldConstructorImpl;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.resources.ClassTransformer;
Expand Down Expand Up @@ -100,7 +100,7 @@ public static <T> DecoratorImpl<T> of(WeldClass<T> clazz, BeanManagerImpl beanMa
private WeldClass<?> annotatedDelegateItem;
private WeldClass<T> proxyClassForAbstractDecorators;
private WeldConstructor<T> constructorForAbstractDecorator;
private Set<MethodSignature> decoratedMethodSignatures;
private Map<MethodSignature, WeldMethod<?,?>> decoratorMethods;
private WeldInjectionPoint<?, ?> delegateInjectionPoint;
private Set<Annotation> delegateBindings;
private Type delegateType;
Expand Down Expand Up @@ -132,8 +132,7 @@ protected void initDecoratedTypes()
this.decoratedTypes = new HashSet<Type>();
this.decoratedTypes.addAll(getWeldAnnotated().getInterfaceClosure());
this.decoratedTypes.remove(Serializable.class);

this.decoratedMethodSignatures = Decorators.getDecoratedMethodSignatures(getBeanManager(), this.decoratedTypes);
this.decoratorMethods = Decorators.getDecoratorMethods(beanManager, decoratedTypes, getWeldAnnotated());
}

protected void initDelegateInjectionPoint()
Expand Down Expand Up @@ -277,11 +276,11 @@ protected T createInstance(CreationalContext<T> ctx)
}
}

public Set<MethodSignature> getDecoratedMethodSignatures()
public WeldMethod<?,?> getDecoratorMethod(Method method)
{
return decoratedMethodSignatures;
return Decorators.findDecoratorMethod(this, decoratorMethods, method);
}

@Override
public String toString()
{
Expand Down
12 changes: 10 additions & 2 deletions impl/src/main/java/org/jboss/weld/bean/WeldDecorator.java
Expand Up @@ -17,13 +17,14 @@

package org.jboss.weld.bean;

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

import javax.enterprise.inject.spi.Decorator;

import org.jboss.weld.introspector.MethodSignature;
import org.jboss.weld.introspector.WeldClass;

import org.jboss.weld.introspector.WeldMethod;


/**
Expand All @@ -37,6 +38,13 @@ public interface WeldDecorator<T> extends Decorator<T>

public WeldClass<?> getWeldAnnotated();

public Set<MethodSignature> getDecoratedMethodSignatures();
/**
* Returns the decorated method that can decorate a particular method, if one exists
*
* Such a method must be implement one of the decorated type methods, and can be parametrized
* @param method
* @return
*/
public WeldMethod<?,?> getDecoratorMethod(Method method);

}
Expand Up @@ -85,10 +85,9 @@ protected Object doInvoke(Object self, Method method, Method proceed, Object[] a
if (beanInstance.getContextual().get() instanceof WeldDecorator<?>)
{
WeldDecorator<?> decorator = (WeldDecorator<?>) beanInstance.getContextual().get();
if (decorator.getDecoratedMethodSignatures().contains(methodSignature)
&& !method.isAnnotationPresent(Inject.class))
if (!method.isAnnotationPresent(Inject.class))
{
WeldMethod<?, ?> decoratorMethod = decorator.getWeldAnnotated().getWeldMethod(methodSignature);
WeldMethod<?, ?> decoratorMethod = decorator.getDecoratorMethod(method);
if (decoratorMethod != null)
{
return decoratorMethod.invokeOnInstance(beanInstance.getInstance(), args);
Expand Down
64 changes: 57 additions & 7 deletions impl/src/main/java/org/jboss/weld/util/Decorators.java
Expand Up @@ -17,16 +17,18 @@

package org.jboss.weld.util;

import org.jboss.weld.bean.WeldDecorator;
import org.jboss.weld.exceptions.IllegalStateException;
import org.jboss.weld.introspector.MethodSignature;
import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.introspector.WeldMethod;
import org.jboss.weld.introspector.jlr.MethodSignatureImpl;
import org.jboss.weld.manager.BeanManagerImpl;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;
import java.util.*;

import static org.jboss.weld.logging.messages.BeanMessage.UNABLE_TO_PROCESS;

Expand All @@ -39,21 +41,40 @@
*/
public class Decorators
{
public static Set<MethodSignature> getDecoratedMethodSignatures(BeanManagerImpl beanManager, Set<Type> decoratedTypes)

public static Map<MethodSignature, WeldMethod<?, ?>> getDecoratorMethods(BeanManagerImpl beanManager, Set<Type> decoratedTypes, WeldClass<?> decoratorClass)
{
List<WeldMethod<?, ?>> decoratedMethods = Decorators.getDecoratedMethods(beanManager, decoratedTypes);
Map<MethodSignature, WeldMethod<?, ?>> decoratorMethods = new HashMap<MethodSignature, WeldMethod<?, ?>>();
for (WeldMethod<?, ?> method : decoratorClass.getWeldMethods())
{
MethodSignatureImpl methodSignature = new MethodSignatureImpl(method);
for (WeldMethod<?, ?> decoratedMethod : decoratedMethods)
{
if (new MethodSignatureImpl(decoratedMethod).equals(methodSignature))
{
decoratorMethods.put(methodSignature, method);
}
}
}
return decoratorMethods;
}

public static List<WeldMethod<?,?>> getDecoratedMethods(BeanManagerImpl beanManager, Set<Type> decoratedTypes)
{
Set<MethodSignature> methodSignatures = new HashSet<MethodSignature>();
List<WeldMethod<?,?>> methods = new ArrayList<WeldMethod<?,?>>();
for (Type type: decoratedTypes)
{
WeldClass<?> weldClass = getWeldClassOfDecoratedType(beanManager, type);
for (WeldMethod<?, ?> method : weldClass.getWeldMethods())
{
if (!methodSignatures.contains(method.getSignature()))
if (!methods.contains(method))
{
methodSignatures.add(method.getSignature());
methods.add(method);
}
}
}
return methodSignatures;
return methods;
}

public static WeldClass<?> getWeldClassOfDecoratedType(BeanManagerImpl beanManager, Type type)
Expand All @@ -68,4 +89,33 @@ public static WeldClass<?> getWeldClassOfDecoratedType(BeanManagerImpl beanManag
}
throw new IllegalStateException(UNABLE_TO_PROCESS, type);
}

public static <T> WeldMethod<?, ?> findDecoratorMethod(WeldDecorator<T> decorator, Map<MethodSignature, WeldMethod<?, ?>> decoratorMethods, Method method)
{
// try the signature first, might be simpler
MethodSignature key = new MethodSignatureImpl(method);
if (decoratorMethods.containsKey(key))
{
return decoratorMethods.get(key);
}
// try all methods
for (WeldMethod<?, ?> decoratorMethod : decoratorMethods.values())
{
if (method.getParameterTypes().length == decoratorMethod.getParameters().size()
&& method.getName().equals(decoratorMethod.getName()))
{
boolean parameterMatch = true;
for (int i=0; parameterMatch && i < method.getParameterTypes().length; i++)
{
parameterMatch = parameterMatch && decoratorMethod.getParameterTypesAsArray()[i].isAssignableFrom(method.getParameterTypes()[i]);
}
if (parameterMatch)
{
return decoratorMethod;
}
}
}

return null;
}
}
Expand Up @@ -58,7 +58,6 @@ public static Archive<?> deploy()
/*
* description = "WELD-579"
*/
@Category(Broken.class)
@Test
public void shouldInvokeDecoratorsWhenObservingGenericEvents()
{
Expand Down

0 comments on commit 8a7e6c7

Please sign in to comment.