Skip to content

Commit

Permalink
WELD-1871 Revised EventLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba authored and jharting committed Jul 22, 2015
1 parent b8d3e1f commit 5de2c52
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion impl/src/main/java/org/jboss/weld/event/EventImpl.java
Expand Up @@ -188,7 +188,7 @@ private Object writeReplace() throws ObjectStreamException {
}

private void readObject(ObjectInputStream stream) throws InvalidObjectException {
throw EventLogger.LOG.proxyRequired();
throw EventLogger.LOG.serializationProxyRequired();
}

private static class SerializationProxy<T> extends AbstractFacadeSerializationProxy<T, Event<T>> {
Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.jboss.weld.logging.EventLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.collections.ImmutableSet;
import org.jboss.weld.util.reflection.Formats;
import org.jboss.weld.util.reflection.Reflections;

/**
Expand Down Expand Up @@ -76,7 +77,8 @@ protected void checkRequiredTypeAnnotations(EnhancedAnnotatedParameter<?, ?> eve
Class<?> rawObserverType = Reflections.getRawType(getObservedType());
boolean isProcessAnnotatedType = rawObserverType.equals(ProcessAnnotatedType.class) || rawObserverType.equals(ProcessSyntheticAnnotatedType.class);
if (!isProcessAnnotatedType && !requiredTypeAnnotations.isEmpty()) {
throw EventLogger.LOG.invalidWithAnnotations(this);
throw EventLogger.LOG
.invalidWithAnnotations(this, Formats.formatAsStackTraceElement(eventParameter.getDeclaringEnhancedCallable().getJavaMember()));
}
if (isProcessAnnotatedType && requiredTypeAnnotations.isEmpty()) {
Type[] typeArguments = eventParameter.getActualTypeArguments();
Expand Down
16 changes: 9 additions & 7 deletions impl/src/main/java/org/jboss/weld/event/ObserverMethodImpl.java
Expand Up @@ -59,6 +59,7 @@
import org.jboss.weld.resources.SharedObjectCache;
import org.jboss.weld.util.Observers;
import org.jboss.weld.util.collections.ImmutableSet;
import org.jboss.weld.util.reflection.Formats;
import org.jboss.weld.util.reflection.HierarchyDiscovery;

/**
Expand Down Expand Up @@ -205,39 +206,40 @@ private <Y> void checkObserverMethod(EnhancedAnnotatedMethod<T, Y> annotated) {
List<EnhancedAnnotatedParameter<?, Y>> eventObjects = annotated.getEnhancedParameters(Observes.class);
eventObjects.addAll(annotated.getEnhancedParameters(ObservesAsync.class));
if (this.reception.equals(Reception.IF_EXISTS) && declaringBean.getScope().equals(Dependent.class)) {
throw EventLogger.LOG.invalidScopedConditionalObserver(this);
throw EventLogger.LOG.invalidScopedConditionalObserver(this, Formats.formatAsStackTraceElement(annotated.getJavaMember()));
}
if (eventObjects.size() > 1) {
throw EventLogger.LOG.multipleEventParameters(this);
throw EventLogger.LOG.multipleEventParameters(this, Formats.formatAsStackTraceElement(annotated.getJavaMember()));
}
EnhancedAnnotatedParameter<?, Y> eventParameter = eventObjects.iterator().next();
checkRequiredTypeAnnotations(eventParameter);
// Check for parameters annotated with @Disposes
List<?> disposeParams = annotated.getEnhancedParameters(Disposes.class);
if (disposeParams.size() > 0) {
throw EventLogger.LOG.invalidDisposesParameter(this);
throw EventLogger.LOG.invalidDisposesParameter(this, Formats.formatAsStackTraceElement(annotated.getJavaMember()));
}
// Check annotations on the method to make sure this is not a producer
// method, initializer method, or destructor method.
if (this.observerMethod.getAnnotated().isAnnotationPresent(Produces.class)) {
throw EventLogger.LOG.invalidProducer(this);
throw EventLogger.LOG.invalidProducer(this, Formats.formatAsStackTraceElement(annotated.getJavaMember()));
}
if (this.observerMethod.getAnnotated().isAnnotationPresent(Inject.class)) {
throw EventLogger.LOG.invalidInitializer(this);
throw EventLogger.LOG.invalidInitializer(this, Formats.formatAsStackTraceElement(annotated.getJavaMember()));
}
boolean containerLifecycleObserverMethod = Observers.isContainerLifecycleObserverMethod(this);
for (EnhancedAnnotatedParameter<?, ?> parameter : annotated.getEnhancedParameters()) {
// if this is an observer method for container lifecycle event, it must not inject anything besides BeanManager
if (containerLifecycleObserverMethod && !parameter.isAnnotationPresent(Observes.class) && !BeanManager.class.equals(parameter.getBaseType())) {
throw EventLogger.LOG.invalidInjectionPoint(this);
throw EventLogger.LOG.invalidInjectionPoint(this, Formats.formatAsStackTraceElement(annotated.getJavaMember()));
}
}

}

protected void checkRequiredTypeAnnotations(EnhancedAnnotatedParameter<?, ?> eventParameter) {
if (eventParameter.isAnnotationPresent(WithAnnotations.class)) {
throw EventLogger.LOG.invalidWithAnnotations(this);
throw EventLogger.LOG
.invalidWithAnnotations(this, Formats.formatAsStackTraceElement(eventParameter.getDeclaringEnhancedCallable().getJavaMember()));
}
}

Expand Down
30 changes: 15 additions & 15 deletions impl/src/main/java/org/jboss/weld/logging/EventLogger.java
Expand Up @@ -48,28 +48,28 @@ public interface EventLogger extends WeldLogger {
void asyncObserverFailure(Object param1);

@Message(id = 403, value = "Proxy required")
InvalidObjectException proxyRequired();
InvalidObjectException serializationProxyRequired();

@Message(id = 404, value = "Conditional observer method [{0}] cannot be declared by a @Dependent scoped bean", format = Format.MESSAGE_FORMAT)
DefinitionException invalidScopedConditionalObserver(Object param1);
@Message(id = 404, value = "Conditional observer method cannot be declared by a @Dependent scoped bean: {0}\n\tat {1}\n StackTrace:", format = Format.MESSAGE_FORMAT)
DefinitionException invalidScopedConditionalObserver(Object param1, Object stackElement);

@Message(id = 405, value = "Observer method [{0}] may only have one event parameter annotated with @Observes or @ObservesAsync", format = Format.MESSAGE_FORMAT)
DefinitionException multipleEventParameters(Object param1);
@Message(id = 405, value = "Observer method cannot have more than one event parameter annotated with @Observes or @ObservesAsync: {0}\n\tat {1}\n StackTrace:", format = Format.MESSAGE_FORMAT)
DefinitionException multipleEventParameters(Object param1, Object stackElement);

@Message(id = 406, value = "Observer method [{0}] cannot have a parameter annotated with @Disposes", format = Format.MESSAGE_FORMAT)
DefinitionException invalidDisposesParameter(Object param1);
@Message(id = 406, value = "Observer method cannot have a parameter annotated with @Disposes: {0}\n\tat {1}\n StackTrace:", format = Format.MESSAGE_FORMAT)
DefinitionException invalidDisposesParameter(Object param1, Object stackElement);

@Message(id = 407, value = "Observer method [{0}] cannot be annotated with @Produces", format = Format.MESSAGE_FORMAT)
DefinitionException invalidProducer(Object param1);
@Message(id = 407, value = "Observer method cannot be annotated with @Produces: {0}\n\tat {1}\n StackTrace:", format = Format.MESSAGE_FORMAT)
DefinitionException invalidProducer(Object param1, Object stackElement);

@Message(id = 408, value = "Observer method [{0}] cannot be annotated with @Inject; observer methods are automatically injection points", format = Format.MESSAGE_FORMAT)
DefinitionException invalidInitializer(Object param1);
@Message(id = 408, value = "Observer method cannot be annotated with @Inject, observer methods are automatically injection points: {0}\n\tat {1}\n StackTrace:", format = Format.MESSAGE_FORMAT)
DefinitionException invalidInitializer(Object param1, Object stackElement);

@Message(id = 409, value = "Observer method for container lifecycle event [{0}] can only inject BeanManager.", format = Format.MESSAGE_FORMAT)
DefinitionException invalidInjectionPoint(Object param1);
@Message(id = 409, value = "Observer method for container lifecycle event can only inject BeanManager: {0}\n\tat {1}\n StackTrace:", format = Format.MESSAGE_FORMAT)
DefinitionException invalidInjectionPoint(Object param1, Object stackElement);

@Message(id = 410, value = "Observer method {0} cannot define @WithAnnotations", format = Format.MESSAGE_FORMAT)
DefinitionException invalidWithAnnotations(Object param1);
@Message(id = 410, value = "Observer method cannot define @WithAnnotations: {0}\n\tat {1}\n StackTrace:", format = Format.MESSAGE_FORMAT)
DefinitionException invalidWithAnnotations(Object param1, Object stackElement);

@LogMessage(level=Level.INFO)
@Message(id = 411, value = "Observer method {0} receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.", format = Format.MESSAGE_FORMAT)
Expand Down
3 changes: 2 additions & 1 deletion impl/src/main/java/org/jboss/weld/util/BeanMethods.java
Expand Up @@ -51,6 +51,7 @@
import org.jboss.weld.security.SetAccessibleAction;
import org.jboss.weld.util.collections.ImmutableList;
import org.jboss.weld.util.collections.ImmutableSet;
import org.jboss.weld.util.reflection.Formats;

public class BeanMethods {

Expand Down Expand Up @@ -223,7 +224,7 @@ public void processMethod(EnhancedAnnotatedMethod<?, ? super T> method) {
} else if (method.getEnhancedParameters(Disposes.class).size() > 0) {
throw UtilLogger.LOG.initializerCannotBeDisposalMethod(method, type);
} else if (method.getEnhancedParameters(Observes.class).size() > 0) {
throw EventLogger.LOG.invalidInitializer(method);
throw EventLogger.LOG.invalidInitializer(method, Formats.formatAsStackTraceElement(method.getJavaMember()));
} else if (method.isGeneric()) {
throw UtilLogger.LOG.initializerMethodIsGeneric(method, type);
}
Expand Down

0 comments on commit 5de2c52

Please sign in to comment.