Skip to content

Commit

Permalink
Conversion of exceptions to use localized messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
drallen committed Nov 9, 2009
1 parent aa9f95d commit b198dfc
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 25 deletions.
42 changes: 42 additions & 0 deletions impl/src/main/java/org/jboss/weld/IllegalProductException.java
@@ -0,0 +1,42 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc. and/or its affiliates, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.weld;

import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import ch.qos.cal10n.IMessageConveyor;

/**
* An {@link javax.enterprise.inject.IllegalProductException} with support for
* localized messages in Weld.
*
* @author David Allen
*
*/
public class IllegalProductException extends javax.enterprise.inject.IllegalProductException
{

private static final long serialVersionUID = -8200417008899252835L;

// Exception messages
private static final IMessageConveyor messageConveyer = loggerFactory().getMessageConveyor();

public <E extends Enum<?>> IllegalProductException(E key, Object... args)
{
super(messageConveyer.getMessage(key, args));
}
}
40 changes: 24 additions & 16 deletions impl/src/main/java/org/jboss/weld/bean/AbstractProducerBean.java
Expand Up @@ -18,6 +18,16 @@

import static org.jboss.weld.logging.Category.BEAN;
import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import static org.jboss.weld.logging.messages.BeanMessage.NON_SERIALIZABLE_CONSTRUCTOR_PARAM_INJECTION_ERROR;
import static org.jboss.weld.logging.messages.BeanMessage.NON_SERIALIZABLE_FIELD_INJECTION_ERROR;
import static org.jboss.weld.logging.messages.BeanMessage.NON_SERIALIZABLE_INITIALIZER_PARAM_INJECTION_ERROR;
import static org.jboss.weld.logging.messages.BeanMessage.NON_SERIALIZABLE_PRODUCER_PARAM_INJECTION_ERROR;
import static org.jboss.weld.logging.messages.BeanMessage.NON_SERIALIZABLE_PRODUCT_ERROR;
import static org.jboss.weld.logging.messages.BeanMessage.NULL_NOT_ALLOWED_FROM_PRODUCER;
import static org.jboss.weld.logging.messages.BeanMessage.ONLY_ONE_SCOPE_ALLOWED;
import static org.jboss.weld.logging.messages.BeanMessage.PRODUCER_CAST_ERROR;
import static org.jboss.weld.logging.messages.BeanMessage.RETURN_TYPE_MUST_BE_CONCRETE;
import static org.jboss.weld.logging.messages.BeanMessage.TYPE_PARAMETER_MUST_BE_CONCRETE;
import static org.jboss.weld.logging.messages.BeanMessage.USING_DEFAULT_SCOPE;
import static org.jboss.weld.logging.messages.BeanMessage.USING_SCOPE;

Expand All @@ -36,7 +46,6 @@
import javax.enterprise.context.Dependent;
import javax.enterprise.context.NormalScope;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.IllegalProductException;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.Producer;
Expand All @@ -45,6 +54,8 @@

import org.jboss.weld.BeanManagerImpl;
import org.jboss.weld.DefinitionException;
import org.jboss.weld.IllegalProductException;
import org.jboss.weld.WeldException;
import org.jboss.weld.bootstrap.BeanDeployerEnvironment;
import org.jboss.weld.introspector.WeldMember;
import org.jboss.weld.metadata.cache.MetaAnnotationStore;
Expand Down Expand Up @@ -119,7 +130,7 @@ protected void initType()
catch (ClassCastException e)
{
Type type = Beans.getDeclaredBeanType(getClass());
throw new RuntimeException(" Cannot cast producer type " + getAnnotatedItem().getJavaClass() + " to bean type " + (type == null ? " unknown " : type), e);
throw new WeldException(PRODUCER_CAST_ERROR, e, getAnnotatedItem().getJavaClass(), (type == null ? " unknown " : type));
}
}

Expand All @@ -128,19 +139,16 @@ protected void initType()
*/
protected void checkProducerReturnType()
{
if (getAnnotatedItem().getBaseType() instanceof TypeVariable<?>)
if ((getAnnotatedItem().getBaseType() instanceof TypeVariable<?>) ||
(getAnnotatedItem().getBaseType() instanceof WildcardType))
{
throw new DefinitionException("Return type must be concrete " + getAnnotatedItem().getBaseType());
}
if (getAnnotatedItem().getBaseType() instanceof WildcardType)
{
throw new DefinitionException("Return type must be concrete " + getAnnotatedItem().getBaseType());
throw new DefinitionException(RETURN_TYPE_MUST_BE_CONCRETE, getAnnotatedItem().getBaseType());
}
for (Type type : getAnnotatedItem().getActualTypeArguments())
{
if (!(type instanceof Class))
{
throw new DefinitionException("Producer type cannot be parameterized with type parameter or wildcard:\n" + this.getAnnotatedItem());
throw new DefinitionException(TYPE_PARAMETER_MUST_BE_CONCRETE, this.getAnnotatedItem());
}
}
}
Expand Down Expand Up @@ -190,14 +198,14 @@ protected void checkReturnValue(T instance)
{
if (instance == null && !isDependent())
{
throw new IllegalProductException("Cannot return null from a non-dependent producer method");
throw new IllegalProductException(NULL_NOT_ALLOWED_FROM_PRODUCER, getProducer());
}
else if (instance != null)
{
boolean passivating = manager.getServices().get(MetaAnnotationStore.class).getScopeModel(getScope()).isPassivating();
if (passivating && !Reflections.isSerializable(instance.getClass()))
{
throw new IllegalProductException("Producers cannot declare passivating scope and return a non-serializable class");
throw new IllegalProductException(NON_SERIALIZABLE_PRODUCT_ERROR, getProducer());
}
InjectionPoint injectionPoint = manager.getCurrentInjectionPoint();
if (injectionPoint == null || injectionPoint.equals(BeanManagerImpl.DUMMY_INJECTION_POINT))
Expand All @@ -210,24 +218,24 @@ else if (instance != null)
{
if (!injectionPoint.isTransient() && instance != null && !Reflections.isSerializable(instance.getClass()))
{
throw new IllegalProductException("Producers cannot produce non-serializable instances for injection into non-transient fields of passivating beans\n\nProducer: " + this.toString() + "\nInjection Point: " + injectionPoint.toString());
throw new IllegalProductException(NON_SERIALIZABLE_FIELD_INJECTION_ERROR, this, injectionPoint);
}
}
else if (injectionPoint.getMember() instanceof Method)
{
Method method = (Method) injectionPoint.getMember();
if (method.isAnnotationPresent(Inject.class))
{
throw new IllegalProductException("Producers cannot produce non-serializable instances for injection into parameters of intializers of beans declaring passivating scope. Bean " + toString() + " being injected into " + injectionPoint.toString());
throw new IllegalProductException(NON_SERIALIZABLE_INITIALIZER_PARAM_INJECTION_ERROR, this, injectionPoint);
}
if (method.isAnnotationPresent(Produces.class))
{
throw new IllegalProductException("Producers cannot produce non-serializable instances for injection into parameters of producer methods declaring passivating scope. Bean " + toString() + " being injected into " + injectionPoint.toString());
throw new IllegalProductException(NON_SERIALIZABLE_PRODUCER_PARAM_INJECTION_ERROR, this, injectionPoint);
}
}
else if (injectionPoint.getMember() instanceof Constructor)
{
throw new IllegalProductException("Producers cannot produce non-serializable instances for injection into parameters of constructors of beans declaring passivating scope. Bean " + toString() + " being injected into " + injectionPoint.toString());
throw new IllegalProductException(NON_SERIALIZABLE_CONSTRUCTOR_PARAM_INJECTION_ERROR, this, injectionPoint);
}
}
}
Expand All @@ -241,7 +249,7 @@ protected void initScopeType()
scopeAnnotations.addAll(getAnnotatedItem().getMetaAnnotations(NormalScope.class));
if (scopeAnnotations.size() > 1)
{
throw new DefinitionException("At most one scope may be specified");
throw new DefinitionException(ONLY_ONE_SCOPE_ALLOWED, getProducer());
}
if (scopeAnnotations.size() == 1)
{
Expand Down
25 changes: 17 additions & 8 deletions impl/src/main/java/org/jboss/weld/bean/DecoratorImpl.java
Expand Up @@ -16,6 +16,14 @@
*/
package org.jboss.weld.bean;

import static org.jboss.weld.logging.messages.BeanMessage.DECORATED_TYPE_PARAMETERIZED_DELEGATE_NOT;
import static org.jboss.weld.logging.messages.BeanMessage.DELEGATE_MUST_SUPPORT_EVERY_DECORATED_TYPE;
import static org.jboss.weld.logging.messages.BeanMessage.DELEGATE_ON_NON_INITIALIZER_METHOD;
import static org.jboss.weld.logging.messages.BeanMessage.DELEGATE_TYPE_PARAMETER_MISMATCH;
import static org.jboss.weld.logging.messages.BeanMessage.NO_DELEGATE_FOR_DECORATOR;
import static org.jboss.weld.logging.messages.BeanMessage.TOO_MANY_DELEGATES_FOR_DECORATOR;
import static org.jboss.weld.logging.messages.BeanMessage.UNABLE_TO_PROCESS;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
Expand All @@ -29,6 +37,7 @@

import org.jboss.weld.BeanManagerImpl;
import org.jboss.weld.DefinitionException;
import org.jboss.weld.ForbiddenStateException;
import org.jboss.weld.bootstrap.BeanDeployerEnvironment;
import org.jboss.weld.injection.MethodInjectionPoint;
import org.jboss.weld.injection.WeldInjectionPoint;
Expand Down Expand Up @@ -120,16 +129,16 @@ protected void checkDelegateInjectionPoints()
{
if (injectionPoint instanceof MethodInjectionPoint<?, ?> && !injectionPoint.isAnnotationPresent(Inject.class))
{
throw new DefinitionException("Method with @Decorates parameter must be an initializer method " + injectionPoint);
throw new DefinitionException(DELEGATE_ON_NON_INITIALIZER_METHOD, injectionPoint);
}
}
if (getDelegateInjectionPoints().size() == 0)
{
throw new DefinitionException("No delegate injection points defined " + this);
throw new DefinitionException(NO_DELEGATE_FOR_DECORATOR, this);
}
else if (getDelegateInjectionPoints().size() > 1)
{
throw new DefinitionException("Too many delegate injection point defined " + this);
throw new DefinitionException(TOO_MANY_DELEGATES_FOR_DECORATOR, this);
}
}

Expand All @@ -154,28 +163,28 @@ protected void checkDelegateType()
{
if (!((Class<?>) decoratedType).isAssignableFrom(delegateInjectionPoint.getJavaClass()))
{
throw new DefinitionException("The delegate type must extend or implement every decorated type. Decorated type " + decoratedType + "." + this );
throw new DefinitionException(DELEGATE_MUST_SUPPORT_EVERY_DECORATED_TYPE, decoratedType, this );
}
}
else if (decoratedType instanceof ParameterizedType)
{
ParameterizedType parameterizedType = (ParameterizedType) decoratedType;
if (!delegateInjectionPoint.isParameterizedType())
{
throw new DefinitionException("The decorated type is parameterized, but the delegate type isn't. Delegate type " + delegateType + "." + this);
throw new DefinitionException(DECORATED_TYPE_PARAMETERIZED_DELEGATE_NOT, delegateType, this);
}
if (!Arrays.equals(delegateInjectionPoint.getActualTypeArguments(), parameterizedType.getActualTypeArguments()))
{
throw new DefinitionException("The delegate type must have exactly the same type parameters as the decorated type. Decorated type " + decoratedType + "." + this );
throw new DefinitionException(DELEGATE_TYPE_PARAMETER_MISMATCH, decoratedType, this );
}
Type rawType = ((ParameterizedType) decoratedType).getRawType();
if (rawType instanceof Class && !((Class<?>) rawType).isAssignableFrom(delegateInjectionPoint.getJavaClass()))
{
throw new DefinitionException("The delegate type must extend or implement every decorated type. Decorated type " + decoratedType + "." + this );
throw new DefinitionException(DELEGATE_MUST_SUPPORT_EVERY_DECORATED_TYPE, decoratedType, this );
}
else
{
throw new IllegalStateException("Unable to process " + decoratedType);
throw new ForbiddenStateException(UNABLE_TO_PROCESS, decoratedType);
}

}
Expand Down
Expand Up @@ -67,6 +67,21 @@ public enum BeanMessage
@MessageId("000046") ONLY_ONE_SCOPE_ALLOWED,
@MessageId("000047") SPECIALIZING_BEAN_MUST_EXTEND_A_BEAN,
@MessageId("000048") CONFLICTING_INTERCEPTOR_BINDINGS,
@MessageId("000049") INVOCATION_ERROR
@MessageId("000049") INVOCATION_ERROR,
@MessageId("000050") PRODUCER_CAST_ERROR,
@MessageId("000051") RETURN_TYPE_MUST_BE_CONCRETE,
@MessageId("000052") NULL_NOT_ALLOWED_FROM_PRODUCER,
@MessageId("000053") NON_SERIALIZABLE_PRODUCT_ERROR,
@MessageId("000054") NON_SERIALIZABLE_FIELD_INJECTION_ERROR,
@MessageId("000055") NON_SERIALIZABLE_INITIALIZER_PARAM_INJECTION_ERROR,
@MessageId("000056") NON_SERIALIZABLE_PRODUCER_PARAM_INJECTION_ERROR,
@MessageId("000057") NON_SERIALIZABLE_CONSTRUCTOR_PARAM_INJECTION_ERROR,
@MessageId("000058") DELEGATE_ON_NON_INITIALIZER_METHOD,
@MessageId("000059") NO_DELEGATE_FOR_DECORATOR,
@MessageId("000060") TOO_MANY_DELEGATES_FOR_DECORATOR,
@MessageId("000061") DELEGATE_MUST_SUPPORT_EVERY_DECORATED_TYPE,
@MessageId("000062") DECORATED_TYPE_PARAMETERIZED_DELEGATE_NOT,
@MessageId("000063") DELEGATE_TYPE_PARAMETER_MISMATCH,
@MessageId("000064") UNABLE_TO_PROCESS

}
15 changes: 15 additions & 0 deletions impl/src/main/resources/org/jboss/weld/messages/bean_en.properties
Expand Up @@ -48,3 +48,18 @@ ONLY_ONE_SCOPE_ALLOWED=At most one scope may be specified on {0}
SPECIALIZING_BEAN_MUST_EXTEND_A_BEAN=Specializing bean must extend another bean\: {0}
CONFLICTING_INTERCEPTOR_BINDINGS=Conflicting interceptor bindings found on {0}
INVOCATION_ERROR=Unable to invoke {0} on {1}
PRODUCER_CAST_ERROR=Cannot cast producer type {0} to bean type {1}
RETURN_TYPE_MUST_BE_CONCRETE=Return type must be concrete\: {0}
NULL_NOT_ALLOWED_FROM_PRODUCER=Cannot return null from a non-dependent producer method\: {0}
NON_SERIALIZABLE_PRODUCT_ERROR=Producers cannot declare passivating scope and return a non-serializable class\: {0}
NON_SERIALIZABLE_INJECTION_ERROR=Producers cannot produce non-serializable instances for injection into non-transient fields of passivating beans\\n\\nProducer\: {0}\\nInjection Point\: {1}
NON_SERIALIZABLE_INITIALIZER_PARAM_INJECTION_ERROR=Producers cannot produce non-serializable instances for injection into parameters of initializers of beans declaring passivating scope.\\n\\nBean\: {0}\\nInjection Point\: {1}
NON_SERIALIZABLE_PRODUCER_PARAM_INJECTION_ERROR=Producers cannot produce non-serializable instances for injection into parameters of producer methods declaring passivating scope.\\n\\nBean\: {0}\\nInjection Point\: {1}
NON_SERIALIZABLE_CONSTRUCTOR_PARAM_INJECTION_ERROR=Producers cannot produce non-serializable instances for injection into parameters of constructors of beans declaring passivating scope.\\n\\nBean\: {0}\\nInjection Point\: {1}
DECORATES_ON_NON_INITIALIZER_METHOD=Method with @Delegate parameter must be an initializer method\: {0}
NO_DELEGATE_FOR_DECORATOR=No delegate injection points defined for {0}
TOO_MANY_DELEGATES_FOR_DECORATOR=Too many delegate injection points defined for {0}
DELEGATE_MUST_SUPPORT_EVERY_DECORATED_TYPE=The delegate type must extend or implement every decorated type. Decorated type {0} on {1}
DECORATED_TYPE_PARAMETERIZED_DELEGATE_NOT=The decorated type is parameterized, but the delegate type isn't. Delegate type {0} on {1}
DELEGATE_TYPE_PARAMETER_MISMATCH=The delegate type must have exactly the same type parameters as the decorated type. Decorated type {0} on decorator {1}
UNABLE_TO_PROCESS=Unable to process {0}

0 comments on commit b198dfc

Please sign in to comment.