Skip to content

Commit

Permalink
Finished changes to the container provided Event implementation to us…
Browse files Browse the repository at this point in the history
…e the new Injectables and a few other bug fixes with related classes.

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@125 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
drallen committed Oct 21, 2008
1 parent d49dddc commit 064d967
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 37 deletions.
Expand Up @@ -29,11 +29,16 @@ public class EventImpl<T> implements Event<T>
@Current
protected Manager webBeansManager;

public EventImpl(Annotation... eventBindings)
/**
* Used to set the event bindings for this type of event after it is constructed
* with the default constructor.
* @param eventBindings Annotations that are bindings for the event
*/
public void setEventBindings(Annotation... eventBindings)
{
this.eventBindings = Arrays.asList(eventBindings);
}

/*
* (non-Javadoc)
*
Expand Down
Expand Up @@ -30,7 +30,7 @@ public Injectable(AnnotatedItem<T, S> annotatedItem)

public Annotation[] getBindingTypes()
{
return (Annotation[]) annotatedItem.getAnnotations().toArray();
return annotatedItem.getAnnotations().toArray(new Annotation[0]);
}

protected Injectable() {}
Expand Down
Expand Up @@ -19,7 +19,6 @@ public class SimpleConstructor<T> extends Unit<T, Constructor<T>> implements Com

private AnnotatedConstructor<T> constructor;

@SuppressWarnings("unchecked")
public SimpleConstructor(Constructor<T> constructor)
{
super(constructor.getParameterTypes(), constructor.getParameterAnnotations());
Expand Down
Expand Up @@ -21,15 +21,14 @@ public Constructor<T> getAnnotatedConstructor()
@Override
public String toString()
{
return constructor + " " + getAnnotatedConstructor().toString();
return constructor.toGenericString();
}

public Constructor<T> getDelegate()
{
return constructor;
}

@SuppressWarnings("unchecked")
public Class<? extends T> getType()
{
return constructor.getDeclaringClass();
Expand Down
Expand Up @@ -87,13 +87,11 @@ else if (getXmlAnnotatedItem().getDelegate() != null)
{
log.finest("Component type specified in XML");
this.type = getXmlAnnotatedItem().getDelegate();
return;
}
else if (getAnnotatedItem().getDelegate() != null)
{
log.finest("Component type specified in Java");
this.type = getAnnotatedItem().getDelegate();
return;
}
else
{
Expand Down
@@ -1,11 +1,17 @@
package org.jboss.webbeans.model;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.logging.Logger;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.bindings.DependentAnnotationLiteral;
import org.jboss.webbeans.bindings.StandardAnnotationLiteral;
import org.jboss.webbeans.injectable.ComponentConstructor;
import org.jboss.webbeans.injectable.SimpleConstructor;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.SimpleAnnotatedItem;
import org.jboss.webbeans.introspector.SimpleAnnotatedField;
import org.jboss.webbeans.util.LoggerUtil;

/**
* Web Beans component meta model for the container instantiated, injectable,
Expand All @@ -14,24 +20,45 @@
* @author David Allen
*
*/
public class EventComponentModel<T> extends AbstractComponentModel<T, Object>
public class EventComponentModel<T> extends AbstractComponentModel<T, Field>
{
private static Logger log = LoggerUtil.getLogger(LOGGER_NAME);

private String location;
private AnnotatedItem<T, Object> annotatedItem;
private AnnotatedItem<T, Object> xmlAnnotatedItem;
private SimpleAnnotatedField<T> annotatedItem;
private SimpleAnnotatedField<T> xmlAnnotatedItem;
private ComponentConstructor<T> constructor;

public EventComponentModel(SimpleAnnotatedItem<T, Object> annotatedItem, SimpleAnnotatedItem<T, Object> xmlAnnotatedItem, ManagerImpl manager)
public EventComponentModel(SimpleAnnotatedField<T> annotatedItem, SimpleAnnotatedField<T> xmlAnnotatedItem, ManagerImpl manager)
{
this.annotatedItem = annotatedItem;
this.xmlAnnotatedItem = xmlAnnotatedItem;
this.init(manager);
}

@Override
protected void init(ManagerImpl container)
{
super.init(container);
this.initConstructor();
}

/**
* Initializes the constructor field of this class.
*/
@SuppressWarnings("unchecked")
protected void initConstructor()
{
// There should only be one constructor for the event implementation used here
Constructor[] constructors = this.annotatedItem.getType().getConstructors();
Constructor<T> classConstructor = (Constructor<T>)constructors[0];
constructor = new SimpleConstructor<T>(classConstructor);
}

@Override
public ComponentConstructor<T> getConstructor()
{
// TODO No constructor is needed, but make sure this does not brake instantiation
return null;
return constructor;
}

@Override
Expand All @@ -53,15 +80,23 @@ public String toString()
/* (non-Javadoc)
* @see org.jboss.webbeans.model.AbstractClassComponentModel#initType()
*/
@SuppressWarnings("unchecked")
@Override
protected void initType()
{
// TODO Get the class for Event and use it for the type
this.type = null;
if (getXmlAnnotatedItem().getDelegate() != null)
{
log.finest("Component type specified in XML");
this.type = (Class<T>) xmlAnnotatedItem.getType();
} else if (getAnnotatedItem().getDelegate() != null)
{
log.finest("Component type specified in Java");
this.type = (Class<T>) annotatedItem.getType();
}
}

@Override
protected AnnotatedItem<T, Object> getAnnotatedItem()
protected AnnotatedItem<T, Field> getAnnotatedItem()
{
return this.annotatedItem;
}
Expand All @@ -74,7 +109,7 @@ protected String getDefaultName()
}

@Override
protected AnnotatedItem<T, Object> getXmlAnnotatedItem()
protected AnnotatedItem<T, Field> getXmlAnnotatedItem()
{
return this.xmlAnnotatedItem;
}
Expand Down
@@ -1,8 +1,8 @@
package org.jboss.webbeans.test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

Expand All @@ -11,10 +11,12 @@
import javax.webbeans.Standard;

import org.jboss.webbeans.bindings.StandardAnnotationLiteral;
import org.jboss.webbeans.event.EventImpl;
import org.jboss.webbeans.injectable.ComponentConstructor;
import org.jboss.webbeans.introspector.SimpleAnnotatedItem;
import org.jboss.webbeans.introspector.SimpleAnnotatedField;
import org.jboss.webbeans.model.EventComponentModel;
import org.jboss.webbeans.test.bindings.AnotherDeploymentTypeAnnotationLiteral;
import org.jboss.webbeans.test.components.DangerCall;
import org.jboss.webbeans.test.mock.MockContainerImpl;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand All @@ -29,7 +31,8 @@
public class EventComponentModelTest
{
private MockContainerImpl manager = null;
private EventComponentModel<Event<? extends Object>> eventComponentModel = null;
private EventComponentModel<EventImpl<DangerCall>> eventComponentModel = null;
EventImpl<DangerCall> eventModelField = null;

@BeforeMethod
public void before() throws Exception
Expand All @@ -38,11 +41,10 @@ public void before() throws Exception
enabledDeploymentTypes.add(new StandardAnnotationLiteral());
enabledDeploymentTypes.add(new AnotherDeploymentTypeAnnotationLiteral());
manager = new MockContainerImpl(enabledDeploymentTypes);
eventComponentModel = new EventComponentModel<Event<? extends Object>>(
new SimpleAnnotatedItem<Event<? extends Object>, Object>(
new HashMap<Class<? extends Annotation>, Annotation>()),
new SimpleAnnotatedItem<Event<? extends Object>, Object>(
new HashMap<Class<? extends Annotation>, Annotation>()),
Field eventModelField = this.getClass().getDeclaredField("eventModelField");
eventComponentModel = new EventComponentModel<EventImpl<DangerCall>>(
new SimpleAnnotatedField<EventImpl<DangerCall>>(eventModelField),
new SimpleAnnotatedField<EventImpl<DangerCall>>(eventModelField),
manager);

}
Expand Down Expand Up @@ -88,9 +90,9 @@ public void testApiTypes()
@Test(groups = "eventbus")
public void testConstructor()
{
ComponentConstructor<Event<? extends Object>> constructor = eventComponentModel.getConstructor();
ComponentConstructor<EventImpl<DangerCall>> constructor = eventComponentModel.getConstructor();
assert constructor != null;
Event<? extends Object> event = constructor.invoke(manager);
Event<DangerCall> event = constructor.invoke(manager);
assert event != null;
}
}
11 changes: 6 additions & 5 deletions webbeans-ri/src/test/java/org/jboss/webbeans/test/EventTest.java
Expand Up @@ -28,6 +28,7 @@
* @author David Allen
*
*/
@SpecVersion("20081012")
public class EventTest
{
private MockContainerImpl manager = null;
Expand All @@ -42,19 +43,19 @@ public void before() throws Exception
}

/**
* Tests the {@link Event#fire(Object, Annotation...)} method with a sample
* event component.
* Tests the {@link Event#fire(Object, Annotation...)} method with a locally
* instantiated implementation.
*/
@SuppressWarnings("unchecked")
@Test(groups = "eventbus")
@Test(groups = "eventbus") @SpecAssertion(section="7.4")
public void testFireEvent()
{
DangerCall anEvent = new DangerCall();
// Create a test annotation for the event and use it to construct the
// event object
Annotation[] annotations = new Annotation[] { new AnimalStereotypeAnnotationLiteral() };
EventImpl<DangerCall> eventComponent = new EventImpl<DangerCall>(
annotations);
EventImpl<DangerCall> eventComponent = new EventImpl<DangerCall>();
eventComponent.setEventBindings(annotations);
eventComponent.setManager(manager);
eventComponent.fire(anEvent, new TameAnnotationLiteral(),
new SynchronousAnnotationLiteral());
Expand Down
Expand Up @@ -32,6 +32,7 @@
* @author David Allen
*
*/
@SpecVersion("20081012")
public class ObserverTest
{
private ManagerImpl manager;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void before() throws Exception
* {@link org.jboss.webbeans.event.ObserverImpl#getEventBindingTypes()}.
*/
@SuppressWarnings("unchecked")
@Test(groups = "eventbus")
@Test(groups = "eventbus") @SpecAssertion(section="7.3")
public final void testGetEventBindingTypes() throws Exception
{
Observer<Event> o = new ObserverImpl<Event>(tuna, om, Event.class);
Expand All @@ -91,7 +92,7 @@ public final void testGetEventBindingTypes() throws Exception
* @throws
* @throws Exception
*/
@Test(groups = "eventbus")
@Test(groups = "eventbus") @SpecAssertion(section="7.3")
public final void testGetEventType() throws Exception
{
Observer<Event> o = new ObserverImpl<Event>(tuna, om, Event.class);
Expand All @@ -103,7 +104,7 @@ public final void testGetEventType() throws Exception
* {@link org.jboss.webbeans.event.ObserverImpl#notify(javax.webbeans.Container, java.lang.Object)}
* .
*/
@Test(groups = "eventbus")
@Test(groups = "eventbus") @SpecAssertion(section={"7.2","7.3"})
public final void testNotify() throws Exception
{
AnObserver observerInstance = new AnObserver();
Expand Down

0 comments on commit 064d967

Please sign in to comment.