Skip to content

Commit

Permalink
WELD-438
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuir committed Feb 17, 2010
1 parent 0977d6b commit 464b582
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 51 deletions.
40 changes: 19 additions & 21 deletions impl/src/main/java/org/jboss/weld/bean/AbstractProducerBean.java
Expand Up @@ -59,7 +59,6 @@
import org.jboss.weld.exceptions.IllegalProductException;
import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.injection.DummyInjectionPoint;
import org.jboss.weld.introspector.WeldMember;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.metadata.cache.MetaAnnotationStore;
Expand Down Expand Up @@ -256,35 +255,34 @@ else if (instance != null)
throw new IllegalProductException(NON_SERIALIZABLE_PRODUCT_ERROR, getProducer());
}
InjectionPoint injectionPoint = Container.instance().services().get(CurrentInjectionPoint.class).peek();
if (injectionPoint == null || injectionPoint.equals(DummyInjectionPoint.INSTANCE))
if (injectionPoint != null && injectionPoint.getBean() != null)
{
return;
}
if (!instanceSerializable && Beans.isPassivatingScope(injectionPoint.getBean(), beanManager))
{
if (injectionPoint.getMember() instanceof Field)
if (!instanceSerializable && Beans.isPassivatingScope(injectionPoint.getBean(), beanManager))
{
if (!injectionPoint.isTransient() && instance != null && !instanceSerializable)
if (injectionPoint.getMember() instanceof Field)
{
throw new IllegalProductException(NON_SERIALIZABLE_FIELD_INJECTION_ERROR, this, injectionPoint);
if (!injectionPoint.isTransient() && instance != null && !instanceSerializable)
{
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))
else if (injectionPoint.getMember() instanceof Method)
{
throw new IllegalProductException(NON_SERIALIZABLE_INITIALIZER_PARAM_INJECTION_ERROR, this, injectionPoint);
Method method = (Method) injectionPoint.getMember();
if (method.isAnnotationPresent(Inject.class))
{
throw new IllegalProductException(NON_SERIALIZABLE_INITIALIZER_PARAM_INJECTION_ERROR, this, injectionPoint);
}
if (method.isAnnotationPresent(Produces.class))
{
throw new IllegalProductException(NON_SERIALIZABLE_PRODUCER_PARAM_INJECTION_ERROR, this, injectionPoint);
}
}
if (method.isAnnotationPresent(Produces.class))
else if (injectionPoint.getMember() instanceof Constructor<?>)
{
throw new IllegalProductException(NON_SERIALIZABLE_PRODUCER_PARAM_INJECTION_ERROR, this, injectionPoint);
throw new IllegalProductException(NON_SERIALIZABLE_CONSTRUCTOR_PARAM_INJECTION_ERROR, this, injectionPoint);
}
}
else if (injectionPoint.getMember() instanceof Constructor<?>)
{
throw new IllegalProductException(NON_SERIALIZABLE_CONSTRUCTOR_PARAM_INJECTION_ERROR, this, injectionPoint);
}
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions impl/src/main/java/org/jboss/weld/bean/builtin/InstanceImpl.java
Expand Up @@ -37,6 +37,7 @@
import org.jboss.weld.Container;
import org.jboss.weld.exceptions.InvalidObjectException;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.injection.SimpleInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.resolution.ResolvableBuilder;
import org.jboss.weld.util.Beans;
Expand Down Expand Up @@ -67,17 +68,18 @@ private InstanceImpl(Type type, Annotation[] qualifiers, InjectionPoint injectio
public T get()
{
Bean<?> bean = getBeanManager().getBean(new ResolvableBuilder(getType()).addQualifiers(getQualifiers()).setDeclaringBean(getInjectionPoint().getBean()).create());
// Push in an empty CC to ensure that we don't get the CC of whatever is injecting the bean containing the Instance injection point
// Generate a correct injection point for the bean, we do this by taking the original injection point and adjusting the qualifiers and type
InjectionPoint ip = new SimpleInjectionPoint(getInjectionPoint().isTransient(), getInjectionPoint().isDelegate(), getType(), getQualifiers(), getInjectionPoint().getMember(), getInjectionPoint().getBean(), getInjectionPoint().getAnnotated());
try
{
Container.instance().services().get(CurrentInjectionPoint.class).pushDummy();
{
Container.instance().services().get(CurrentInjectionPoint.class).push(ip);
@SuppressWarnings("unchecked")
T instance = (T) getBeanManager().getReference(bean, getType(), getBeanManager().createCreationalContext(bean));
return instance;
}
finally
{
Container.instance().services().get(CurrentInjectionPoint.class).popDummy();
Container.instance().services().get(CurrentInjectionPoint.class).pop();
}
}

Expand Down
Expand Up @@ -33,6 +33,7 @@
import org.jboss.weld.context.CreationalContextImpl;
import org.jboss.weld.context.WeldCreationalContext;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.injection.SimpleInjectionPoint;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.jboss.weld.util.reflection.SecureReflections;
import org.slf4j.cal10n.LocLogger;
Expand Down Expand Up @@ -134,16 +135,16 @@ private <T> T getProxiedInstance(Bean<T> bean)
creationalContext = currentCreationalContext.get().getCreationalContext(bean);
outer = false;
}
Context context = Container.instance().deploymentManager().getContext(bean.getScope());
try
{
Context context = Container.instance().deploymentManager().getContext(bean.getScope());
// Ensure that there is no injection point associated
Container.instance().services().get(CurrentInjectionPoint.class).pushDummy();
Container.instance().services().get(CurrentInjectionPoint.class).push(SimpleInjectionPoint.EMPTY_INJECTION_POINT);
return context.get(bean, creationalContext);
}
finally
{
Container.instance().services().get(CurrentInjectionPoint.class).popDummy();
Container.instance().services().get(CurrentInjectionPoint.class).pop();
if (outer)
{
currentCreationalContext.remove();
Expand Down
Expand Up @@ -73,19 +73,6 @@ public InjectionPoint peek()
return null;
}
}

public void pushDummy()
{
currentInjectionPoint.get().push(DummyInjectionPoint.INSTANCE);
}

public void popDummy()
{
if (!currentInjectionPoint.get().isEmpty() && DummyInjectionPoint.INSTANCE.equals(currentInjectionPoint.get().peek()))
{
currentInjectionPoint.get().pop();
}
}

public void cleanup()
{
Expand Down
Expand Up @@ -26,44 +26,70 @@
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.InjectionPoint;

public class DummyInjectionPoint implements InjectionPoint
import org.jboss.weld.util.collections.Arrays2;

public class SimpleInjectionPoint implements InjectionPoint
{

public static final DummyInjectionPoint INSTANCE = new DummyInjectionPoint();
public static final InjectionPoint EMPTY_INJECTION_POINT = new SimpleInjectionPoint(false, false, Object.class, Collections.<Annotation>emptySet(), null, null, null);

private final boolean _transient;
private final boolean delegate;
private final Type type;
private final Set<Annotation> qualifiers;
private final Member member;
private final Bean<?> bean;
private final Annotated annotated;

public SimpleInjectionPoint(boolean _transient, boolean delegate, Type type, Set<Annotation> qualifiers, Member member, Bean<?> bean, Annotated annotated)
{
this._transient = _transient;
this.delegate = delegate;
this.type = type;
this.qualifiers = qualifiers;
this.member = member;
this.bean = bean;
this.annotated = annotated;
}

public SimpleInjectionPoint(boolean _transient, boolean delegate, Type type, Annotation[] qualifiers, Member member, Bean<?> bean, Annotated annotated)
{
this(_transient, delegate, type, Arrays2.asSet(qualifiers), member, bean, annotated);
}

public boolean isTransient()
{
return true;
return _transient;
}

public boolean isDelegate()
{
return false;
return delegate;
}

public Type getType()
{
return InjectionPoint.class;
return type;
}

public Set<Annotation> getQualifiers()
{
return Collections.emptySet();
return qualifiers;
}

public Member getMember()
{
return null;
return member;
}

public Bean<?> getBean()
{
return null;
return bean;
}

public Annotated getAnnotated()
{
return null;
return annotated;
}

}
6 changes: 5 additions & 1 deletion impl/src/main/java/org/jboss/weld/util/Beans.java
Expand Up @@ -124,7 +124,11 @@ public class Beans
*/
public static boolean isPassivatingScope(Bean<?> bean, BeanManagerImpl manager)
{
if (bean instanceof SessionBean<?>)
if (bean == null)
{
return false;
}
else if (bean instanceof SessionBean<?>)
{
return ((SessionBean<?>) bean).getEjbDescriptor().isStateful();
}
Expand Down
@@ -0,0 +1,46 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., 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.tests.injectionPoint;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Qualifier;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD, METHOD, TYPE, PARAMETER})
public @interface ExtraSpecial
{

static class ExtraSpecialLiteral extends AnnotationLiteral<ExtraSpecial> implements ExtraSpecial
{

private ExtraSpecialLiteral() {}

}

public static final ExtraSpecial INSTANCE = new ExtraSpecialLiteral();

}
Expand Up @@ -16,7 +16,10 @@
*/
package org.jboss.weld.tests.injectionPoint;

import java.lang.reflect.ParameterizedType;

import javax.enterprise.inject.IllegalProductException;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.testharness.impl.packaging.Artifact;
Expand Down Expand Up @@ -59,5 +62,26 @@ public void testGetDeclaringType()
{
assert getReference(GrassyField.class).getCow().getName().equals("daisy");
}

@Test(description = "WELD-438")
public void testInjectionPointWhenInstanceGetIsUsed() throws Exception
{
Pig pig = getReference(PigSty.class).getPig();
assert pig != null;
assert pig.getInjectionPoint().getBean() != null;
assert pig.getInjectionPoint().getBean().getBeanClass().equals(PigSty.class);
assert pig.getInjectionPoint().getMember().equals(PigSty.class.getDeclaredField("pig"));
assert pig.getInjectionPoint().getAnnotated() != null;
assert pig.getInjectionPoint().getAnnotated().getBaseType() instanceof ParameterizedType;
ParameterizedType parameterizedType = ((ParameterizedType) pig.getInjectionPoint().getAnnotated().getBaseType());
assert parameterizedType.getRawType().equals(Instance.class);
assert parameterizedType.getActualTypeArguments().length == 1;
assert parameterizedType.getActualTypeArguments()[0].equals(Pig.class);
assert pig.getInjectionPoint().getAnnotated().isAnnotationPresent(Special.class);
assert !pig.getInjectionPoint().getAnnotated().isAnnotationPresent(ExtraSpecial.class);
assert Utils.annotationSetMatches(pig.injectionPoint.getQualifiers(), Special.class, ExtraSpecial.class);
assert Pig.class.equals(pig.getInjectionPoint().getType());

}

}
17 changes: 17 additions & 0 deletions tests/src/test/java/org/jboss/weld/tests/injectionPoint/Pig.java
@@ -0,0 +1,17 @@
package org.jboss.weld.tests.injectionPoint;

import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;

@Special @ExtraSpecial
public class Pig
{

@Inject InjectionPoint injectionPoint;

public InjectionPoint getInjectionPoint()
{
return injectionPoint;
}

}
@@ -0,0 +1,16 @@
package org.jboss.weld.tests.injectionPoint;

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

public class PigSty
{
@Inject @Special Instance<Pig> pig;

public Pig getPig()
{
return pig.select(ExtraSpecial.INSTANCE).get();
}

}

@@ -0,0 +1,36 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., 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.tests.injectionPoint;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.inject.Qualifier;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD, METHOD, TYPE, PARAMETER})
public @interface Special
{

}

0 comments on commit 464b582

Please sign in to comment.