Skip to content

Commit

Permalink
WELD-1293 Make Event and Instance obtainable dynamically (with sensib…
Browse files Browse the repository at this point in the history
…le default type/qualifiers)
  • Loading branch information
jharting committed Feb 22, 2013
1 parent 7aaf051 commit 3b40891
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 38 deletions.
Expand Up @@ -16,16 +16,13 @@
*/
package org.jboss.weld.bean.builtin;

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

import java.util.List;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Decorator;
import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.weld.bean.DecorableBean;
import org.jboss.weld.exceptions.IllegalArgumentException;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.Decorators;
Expand All @@ -47,12 +44,7 @@ protected AbstractDecorableBuiltInBean(String idSuffix, BeanManagerImpl beanMana

@Override
public T create(CreationalContext<T> creationalContext) {
InjectionPoint ip = cip.peek();
// some of the built-in beans require injection point metadata while other can do without it
if (ip == null && (isInjectionPointMetadataRequired() || !getDecorators(ip).isEmpty())) {
injectionPointNotAvailable();
return null;
}
InjectionPoint ip = getInjectionPoint(cip);

List<Decorator<?>> decorators = getDecorators(ip);
T instance = newInstance(ip, creationalContext);
Expand All @@ -68,19 +60,15 @@ public T create(CreationalContext<T> creationalContext) {

protected abstract Class<T> getProxyClass();

protected boolean isInjectionPointMetadataRequired() {
return false;
protected InjectionPoint getInjectionPoint(CurrentInjectionPoint cip) {
return cip.peek();
}

@Override
public Class<?> getBeanClass() {
return getClass();
}

protected void injectionPointNotAvailable() {
throw new IllegalArgumentException(DYNAMIC_LOOKUP_OF_BUILT_IN_NOT_ALLOWED, toString());
}

@Override
public List<Decorator<?>> getDecorators() {
return beanManager.resolveDecorators(getTypes(), getQualifiers());
Expand Down
Expand Up @@ -16,6 +16,8 @@
*/
package org.jboss.weld.bean.builtin;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;

Expand All @@ -24,6 +26,8 @@
import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.weld.bean.proxy.ProxyFactory;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.injection.EmptyInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;

/**
Expand Down Expand Up @@ -62,7 +66,13 @@ protected List<Decorator<?>> getDecorators(InjectionPoint ip) {
}

@Override
protected boolean isInjectionPointMetadataRequired() {
return true;
protected InjectionPoint getInjectionPoint(CurrentInjectionPoint cip) {
InjectionPoint ip = super.getInjectionPoint(cip);
if (ip == null) {
ip = new DynamicLookupInjectionPoint(EmptyInjectionPoint.INSTANCE, getDefaultType(), Collections.<Annotation>emptySet());
}
return ip;
}

protected abstract Type getDefaultType();
}
10 changes: 10 additions & 0 deletions impl/src/main/java/org/jboss/weld/bean/builtin/EventBean.java
Expand Up @@ -18,16 +18,21 @@

import static org.jboss.weld.util.reflection.Reflections.cast;

import java.lang.reflect.Type;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Event;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.util.TypeLiteral;

import org.jboss.weld.event.EventImpl;
import org.jboss.weld.manager.BeanManagerImpl;

public class EventBean extends AbstractFacadeBean<Event<?>> {

private static final Class<Event<?>> TYPE = cast(Event.class);
@SuppressWarnings("serial")
private static final Type DEFAULT_TYPE = new TypeLiteral<Event<Object>>(){}.getType();

public EventBean(BeanManagerImpl manager) {
super(Event.class.getSimpleName(), manager, TYPE);
Expand All @@ -48,4 +53,9 @@ public String toString() {
return "Implicit Bean [javax.enterprise.event.Event] with qualifiers [@Default]";
}

@Override
protected Type getDefaultType() {
return DEFAULT_TYPE;
}

}
Expand Up @@ -16,9 +16,12 @@
*/
package org.jboss.weld.bean.builtin;

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

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.weld.exceptions.IllegalArgumentException;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.bean.SerializableForwardingInjectionPoint;
Expand Down Expand Up @@ -58,9 +61,14 @@ public void destroy(InjectionPoint instance, CreationalContext<InjectionPoint> c

}


@Override
protected boolean isInjectionPointMetadataRequired() {
return true;
protected InjectionPoint getInjectionPoint(CurrentInjectionPoint cip) {
InjectionPoint ip = super.getInjectionPoint(cip);
if (ip == null) {
throw new IllegalArgumentException(DYNAMIC_LOOKUP_OF_BUILT_IN_NOT_ALLOWED, toString());
}
return ip;
}

@Override
Expand Down
16 changes: 11 additions & 5 deletions impl/src/main/java/org/jboss/weld/bean/builtin/InstanceBean.java
Expand Up @@ -27,18 +27,19 @@

import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.collections.Arrays2;
import org.jboss.weld.util.reflection.Reflections;

public class InstanceBean extends AbstractFacadeBean<Instance<?>> {
private static final Class<Instance<?>> INSTANCE_TYPE = new TypeLiteral<Instance<?>>() {
private static final Type INSTANCE_TYPE = new TypeLiteral<Instance<Object>>() {
private static final long serialVersionUID = -1246199714407637856L;
}.getRawType();
private static final Class<Provider<?>> PROVIDER_TYPE = new TypeLiteral<Provider<?>>() {
}.getType();
private static final Type PROVIDER_TYPE = new TypeLiteral<Provider<Object>>() {
private static final long serialVersionUID = -5256050387550468441L;
}.getRawType();
}.getType();
private static final Set<Type> DEFAULT_TYPES = Arrays2.<Type>asSet(INSTANCE_TYPE, PROVIDER_TYPE, Object.class);

public InstanceBean(BeanManagerImpl manager) {
super(Instance.class.getSimpleName(), manager, INSTANCE_TYPE);
super(Instance.class.getSimpleName(), manager, Reflections.<Class<Instance<?>>>cast(Instance.class));
}

@Override
Expand All @@ -60,4 +61,9 @@ public String toString() {
return "Implicit Bean [javax.enterprise.inject.Instance] with qualifiers [@Default]";
}

@Override
protected Type getDefaultType() {
return INSTANCE_TYPE;
}

}
Expand Up @@ -16,6 +16,12 @@
*/
package org.jboss.weld.tests.resolution;

import static org.junit.Assert.assertEquals;

import javax.enterprise.inject.Instance;
import javax.enterprise.util.TypeLiteral;
import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
Expand All @@ -24,15 +30,9 @@
import org.jboss.weld.literal.DefaultLiteral;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.test.util.Utils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.enterprise.inject.Instance;
import javax.enterprise.util.TypeLiteral;
import javax.inject.Inject;
import java.util.List;

@RunWith(Arquillian.class)
public class LookupInstanceTest {
@Deployment
Expand All @@ -45,16 +45,13 @@ public static Archive<?> deploy() {
@Inject
private BeanManagerImpl beanManager;

@SuppressWarnings("serial")
@Test
public void testLookupInstance() throws Exception {
try {
Utils.getReference(
beanManager,
new TypeLiteral<Instance<List<?>>>() {
}.getRawType(), DefaultLiteral.INSTANCE);
Assert.fail();
} catch (IllegalArgumentException expected) {
}
Instance<Object> instance = Utils.getReference(beanManager, new TypeLiteral<Instance<Object>>() {
}.getRawType(), DefaultLiteral.INSTANCE);
Foo foo = instance.select(Foo.class).get();
assertEquals("foo", foo.getName());
}

}

0 comments on commit 3b40891

Please sign in to comment.