Skip to content

Commit

Permalink
WELD-468
Browse files Browse the repository at this point in the history
  • Loading branch information
nickarls committed Mar 12, 2010
1 parent 99cbe8a commit c7d435a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 34 deletions.
13 changes: 6 additions & 7 deletions impl/src/main/java/org/jboss/weld/bootstrap/Validator.java
Expand Up @@ -23,14 +23,11 @@
import static org.jboss.weld.logging.messages.ValidatorMessage.AMBIGUOUS_EL_NAME;
import static org.jboss.weld.logging.messages.ValidatorMessage.BEAN_NAME_IS_PREFIX;
import static org.jboss.weld.logging.messages.ValidatorMessage.BEAN_SPECIALIZED_TOO_MANY_TIMES;
import static org.jboss.weld.logging.messages.ValidatorMessage.DECORATOR_CLASS_NOT_BEAN_CLASS_OF_DECORATOR;
import static org.jboss.weld.logging.messages.ValidatorMessage.DECORATOR_SPECIFIED_TWICE;
import static org.jboss.weld.logging.messages.ValidatorMessage.DECORATORS_CANNOT_HAVE_DISPOSER_METHODS;
import static org.jboss.weld.logging.messages.ValidatorMessage.DECORATORS_CANNOT_HAVE_PRODUCER_FIELDS;
import static org.jboss.weld.logging.messages.ValidatorMessage.DECORATORS_CANNOT_HAVE_PRODUCER_METHODS;
import static org.jboss.weld.logging.messages.ValidatorMessage.INTERCEPTORS_CANNOT_HAVE_DISPOSER_METHODS;
import static org.jboss.weld.logging.messages.ValidatorMessage.INTERCEPTORS_CANNOT_HAVE_PRODUCER_FIELDS;
import static org.jboss.weld.logging.messages.ValidatorMessage.INTERCEPTORS_CANNOT_HAVE_PRODUCER_METHODS;
import static org.jboss.weld.logging.messages.ValidatorMessage.DECORATOR_CLASS_NOT_BEAN_CLASS_OF_DECORATOR;
import static org.jboss.weld.logging.messages.ValidatorMessage.DECORATOR_SPECIFIED_TWICE;
import static org.jboss.weld.logging.messages.ValidatorMessage.DISPOSAL_METHODS_WITHOUT_PRODUCER;
import static org.jboss.weld.logging.messages.ValidatorMessage.INJECTION_INTO_NON_BEAN;
import static org.jboss.weld.logging.messages.ValidatorMessage.INJECTION_INTO_NON_DEPENDENT_BEAN;
Expand All @@ -42,12 +39,14 @@
import static org.jboss.weld.logging.messages.ValidatorMessage.INJECTION_POINT_HAS_WILDCARD;
import static org.jboss.weld.logging.messages.ValidatorMessage.INJECTION_POINT_MUST_HAVE_TYPE_PARAMETER;
import static org.jboss.weld.logging.messages.ValidatorMessage.INJECTION_POINT_WITH_TYPE_VARIABLE;
import static org.jboss.weld.logging.messages.ValidatorMessage.INTERCEPTORS_CANNOT_HAVE_DISPOSER_METHODS;
import static org.jboss.weld.logging.messages.ValidatorMessage.INTERCEPTORS_CANNOT_HAVE_PRODUCER_FIELDS;
import static org.jboss.weld.logging.messages.ValidatorMessage.INTERCEPTORS_CANNOT_HAVE_PRODUCER_METHODS;
import static org.jboss.weld.logging.messages.ValidatorMessage.INTERCEPTOR_NOT_ANNOTATED_OR_REGISTERED;
import static org.jboss.weld.logging.messages.ValidatorMessage.INTERCEPTOR_SPECIFIED_TWICE;
import static org.jboss.weld.logging.messages.ValidatorMessage.NEW_WITH_QUALIFIERS;
import static org.jboss.weld.logging.messages.ValidatorMessage.NON_FIELD_INJECTION_POINT_CANNOT_USE_NAMED;
import static org.jboss.weld.logging.messages.ValidatorMessage.NON_SERIALIZABLE_BEAN_INJECTED_INTO_PASSIVATING_BEAN;
import static org.jboss.weld.logging.messages.ValidatorMessage.NOT_PROXYABLE;
import static org.jboss.weld.logging.messages.ValidatorMessage.PASSIVATING_BEAN_WITH_NONSERIALIZABLE_DECORATOR;
import static org.jboss.weld.logging.messages.ValidatorMessage.PASSIVATING_BEAN_WITH_NONSERIALIZABLE_INTERCEPTOR;

Expand Down Expand Up @@ -124,7 +123,7 @@ private void validateBean(Bean<?> bean, BeanManagerImpl beanManager)
boolean normalScoped = beanManager.getServices().get(MetaAnnotationStore.class).getScopeModel(bean.getScope()).isNormal();
if (normalScoped && !Beans.isBeanProxyable(bean))
{
throw new UnproxyableResolutionException(NOT_PROXYABLE, bean);
throw Proxies.getUnproxyableTypesException(bean.getTypes());
}
}

Expand Down
Expand Up @@ -17,7 +17,10 @@

package org.jboss.weld.logging.messages;

import java.lang.reflect.Modifier;

import org.jboss.weld.logging.MessageId;
import org.jboss.weld.util.reflection.Reflections;

import ch.qos.cal10n.BaseName;
import ch.qos.cal10n.Locale;
Expand All @@ -37,7 +40,7 @@
*/
public enum ValidatorMessage
{
@MessageId("001400") NOT_PROXYABLE,
// @MessageId("001400") NOT_PROXYABLE,
@MessageId("001401") BEAN_SPECIALIZED_TOO_MANY_TIMES,
@MessageId("001402") PASSIVATING_BEAN_WITH_NONSERIALIZABLE_INTERCEPTOR,
@MessageId("001403") PASSIVATING_BEAN_WITH_NONSERIALIZABLE_DECORATOR,
Expand Down Expand Up @@ -67,9 +70,14 @@ public enum ValidatorMessage
@MessageId("001427") NON_FIELD_INJECTION_POINT_CANNOT_USE_NAMED,
@MessageId("001428") DECORATORS_CANNOT_HAVE_PRODUCER_METHODS,
@MessageId("001429") DECORATORS_CANNOT_HAVE_PRODUCER_FIELDS,
@MessageId("001430")DECORATORS_CANNOT_HAVE_DISPOSER_METHODS,
@MessageId("001430") DECORATORS_CANNOT_HAVE_DISPOSER_METHODS,
@MessageId("001431") INTERCEPTORS_CANNOT_HAVE_PRODUCER_METHODS,
@MessageId("001432") INTERCEPTORS_CANNOT_HAVE_PRODUCER_FIELDS,
@MessageId("001433")INTERCEPTORS_CANNOT_HAVE_DISPOSER_METHODS;

@MessageId("001433") INTERCEPTORS_CANNOT_HAVE_DISPOSER_METHODS,
@MessageId("001434") NOT_PROXYABLE_UNKNOWN,
@MessageId("001435") NOT_PROXYABLE_NO_CONSTRUCTOR,
@MessageId("001436") NOT_PROXYABLE_PRIVATE_CONSTRUCTOR,
@MessageId("001437") NOT_PROXYABLE_FINAL_TYPE_OR_METHOD,
@MessageId("001438") NOT_PROXYABLE_PRIMITIVE,
@MessageId("001439") NOT_PROXYABLE_ARRAY_TYPE;
}
Expand Up @@ -21,7 +21,6 @@
import static org.jboss.weld.logging.messages.BeanManagerMessage.DUPLICATE_ACTIVE_CONTEXTS;
import static org.jboss.weld.logging.messages.BeanManagerMessage.NON_NORMAL_SCOPE;
import static org.jboss.weld.logging.messages.BeanManagerMessage.NOT_INTERCEPTOR_BINDING_TYPE;
import static org.jboss.weld.logging.messages.BeanManagerMessage.NOT_PROXYABLE;
import static org.jboss.weld.logging.messages.BeanManagerMessage.NOT_STEREOTYPE;
import static org.jboss.weld.logging.messages.BeanManagerMessage.NO_DECORATOR_TYPES;
import static org.jboss.weld.logging.messages.BeanManagerMessage.SPECIFIED_TYPE_NOT_BEAN_TYPE;
Expand Down Expand Up @@ -810,7 +809,7 @@ public <T> Bean<T> getBean(Resolvable resolvable)
boolean normalScoped = getServices().get(MetaAnnotationStore.class).getScopeModel(bean.getScope()).isNormal();
if (normalScoped && !Beans.isBeanProxyable(bean))
{
throw new UnproxyableResolutionException(NOT_PROXYABLE, bean);
throw Proxies.getUnproxyableTypesException(bean.getTypes());
}
return bean;
}
Expand Down
51 changes: 34 additions & 17 deletions impl/src/main/java/org/jboss/weld/util/Proxies.java
Expand Up @@ -18,6 +18,12 @@

import static org.jboss.weld.logging.messages.UtilMessage.CANNOT_PROXY_NON_CLASS_TYPE;
import static org.jboss.weld.logging.messages.UtilMessage.INSTANCE_NOT_A_PROXY;
import static org.jboss.weld.logging.messages.ValidatorMessage.NOT_PROXYABLE_ARRAY_TYPE;
import static org.jboss.weld.logging.messages.ValidatorMessage.NOT_PROXYABLE_FINAL_TYPE_OR_METHOD;
import static org.jboss.weld.logging.messages.ValidatorMessage.NOT_PROXYABLE_NO_CONSTRUCTOR;
import static org.jboss.weld.logging.messages.ValidatorMessage.NOT_PROXYABLE_PRIMITIVE;
import static org.jboss.weld.logging.messages.ValidatorMessage.NOT_PROXYABLE_PRIVATE_CONSTRUCTOR;
import static org.jboss.weld.logging.messages.ValidatorMessage.NOT_PROXYABLE_UNKNOWN;
import static org.jboss.weld.util.reflection.Reflections.EMPTY_CLASSES;

import java.io.Serializable;
Expand All @@ -36,6 +42,7 @@
import javassist.util.proxy.ProxyObject;

import org.jboss.weld.exceptions.ForbiddenArgumentException;
import org.jboss.weld.exceptions.UnproxyableResolutionException;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;

Expand All @@ -59,7 +66,6 @@ public boolean isHandled(Method m)

}


public static class TypeInfo
{

Expand Down Expand Up @@ -214,20 +220,25 @@ public static <T> Class<T> createProxyClass(MethodHandler methodHandler, TypeInf
* @return True if proxyable, false otherwise
*/
public static boolean isTypeProxyable(Type type)
{
return getUnproxyableTypeException(type) == null;
}

public static UnproxyableResolutionException getUnproxyableTypeException(Type type)
{
if (type instanceof Class<?>)
{
return isClassProxyable((Class<?>) type);
return getUnproxyableClassException((Class<?>) type);
}
else if (type instanceof ParameterizedType)
{
Type rawType = ((ParameterizedType) type).getRawType();
if (rawType instanceof Class<?>)
{
return isClassProxyable((Class<?>) rawType);
return getUnproxyableClassException((Class<?>) rawType);
}
}
return false;
return new UnproxyableResolutionException(NOT_PROXYABLE_UNKNOWN, type);
}


Expand All @@ -238,26 +249,32 @@ else if (type instanceof ParameterizedType)
* @return True if proxyable, false otherwise
*/
public static boolean isTypesProxyable(Iterable<? extends Type> types)
{
return getUnproxyableTypesException(types) == null;
}

public static UnproxyableResolutionException getUnproxyableTypesException(Iterable<? extends Type> types)
{
for (Type apiType : types)
{
if (Object.class.equals(apiType))
{
continue;
}
if (!isTypeProxyable(apiType))
UnproxyableResolutionException e = getUnproxyableTypeException(apiType);
if (e != null)
{
return false;
return e;
}
}
return true;
return null;
}
private static boolean isClassProxyable(Class<?> clazz)

private static UnproxyableResolutionException getUnproxyableClassException(Class<?> clazz)
{
if (clazz.isInterface())
{
return true;
return null;
}
else
{
Expand All @@ -268,31 +285,31 @@ private static boolean isClassProxyable(Class<?> clazz)
}
catch (NoSuchMethodException e)
{
return false;
return new UnproxyableResolutionException(NOT_PROXYABLE_NO_CONSTRUCTOR, clazz);
}
if (constructor == null)
{
return false;
return new UnproxyableResolutionException(NOT_PROXYABLE_NO_CONSTRUCTOR, clazz);
}
else if (Modifier.isPrivate(constructor.getModifiers()))
{
return false;
return new UnproxyableResolutionException(NOT_PROXYABLE_PRIVATE_CONSTRUCTOR, clazz, constructor);
}
else if (Reflections.isTypeOrAnyMethodFinal(clazz))
{
return false;
return new UnproxyableResolutionException(NOT_PROXYABLE_FINAL_TYPE_OR_METHOD, clazz, Reflections.getFinalMethodOrType(clazz));
}
else if (clazz.isPrimitive())
{
return false;
return new UnproxyableResolutionException(NOT_PROXYABLE_PRIMITIVE, clazz);
}
else if (Reflections.isArrayType(clazz))
{
return false;
return new UnproxyableResolutionException(NOT_PROXYABLE_ARRAY_TYPE, clazz);
}
else
{
return true;
return null;
}
}
}
Expand Down
Expand Up @@ -154,19 +154,24 @@ public static boolean isFinal(Member member)
* @return True if final, false otherwise
*/
public static boolean isTypeOrAnyMethodFinal(Class<?> type)
{
return getFinalMethodOrType(type) != null;
}

public static Object getFinalMethodOrType(Class<?> type)
{
if (isFinal(type))
{
return true;
return type;
}
for (Method method : type.getDeclaredMethods())
{
if (isFinal(method))
{
return true;
return method;
}
}
return false;
return null;
}

public static boolean isPackagePrivate(int mod)
Expand Down
@@ -1,4 +1,3 @@
NOT_PROXYABLE=Normal scoped bean {0} is not proxyable
BEAN_SPECIALIZED_TOO_MANY_TIMES=Two beans cannot specialize the same bean {0}
PASSIVATING_BEAN_WITH_NONSERIALIZABLE_INTERCEPTOR=The bean {0} declared a passivating scope but has a non-serializable interceptor {1}
PASSIVATING_BEAN_WITH_NONSERIALIZABLE_DECORATOR=The bean {0} declared a passivating scope but has non-serializable decorator {1}
Expand Down Expand Up @@ -32,3 +31,9 @@ DECORATORS_CANNOT_HAVE_DISPOSER_METHODS=A decorator cannot have disposer methods
INTERCEPTORS_CANNOT_HAVE_PRODUCER_METHODS=An interceptor cannot have producer methods, but at least one was found on {0}.
INTERCEPTORS_CANNOT_HAVE_PRODUCER_FIELDS=An interceptor cannot have producer fields, but at least one was found on {0}.
INTERCEPTORS_CANNOT_HAVE_DISPOSER_METHODS=An interceptor cannot disposer methods, but at least one was found on {0}.
NOT_PROXYABLE_UNKNOWN=Normal scoped bean {0} is not proxyable for an unknown reason.
NOT_PROXYABLE_NO_CONSTRUCTOR=Normal scoped bean {0} is not proxyable because it has no no-args constructor.
NOT_PROXYABLE_PRIVATE_CONSTRUCTOR=Normal scoped bean {0} is not proxyable because it has a private no-args constructor {1}.
NOT_PROXYABLE_FINAL_TYPE_OR_METHOD=Normal scoped bean {0} is not proxyable because it has a private field or method {1}.
NOT_PROXYABLE_PRIMITIVE=Normal scoped bean {0} is not proxyable because it is a primitive
NOT_PROXYABLE_ARRAY_TYPE=Normal scoped bean {0} is not proxyable because it is an array type

0 comments on commit c7d435a

Please sign in to comment.