Author: rmannibucau
Date: Wed Feb 24 11:46:02 2016
New Revision: 1732080
URL: http://svn.apache.org/viewvc?rev=1732080&view=rev
Log:
OWB-1122 fixing InjectionPoint#getQualifiers when used in conjonction with
Instance.select().get()
Added:
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/instance/InstanceQualifierInjectionPointTest.java
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionPointProducer.java
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java?rev=1732080&r1=1732079&r2=1732080&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
Wed Feb 24 11:46:02 2016
@@ -23,8 +23,11 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.annotation.Annotation;
+import java.lang.reflect.Member;
import java.lang.reflect.Type;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
@@ -34,8 +37,8 @@ import java.util.Set;
import javax.enterprise.context.spi.AlterableContext;
import javax.enterprise.context.spi.Context;
-import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Instance;
+import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.util.TypeLiteral;
@@ -175,7 +178,7 @@ public class InstanceImpl<T> implements
public Instance<T> select(Annotation... qualifiers)
{
Annotation[] newQualifiersArray = getAdditionalQualifiers(qualifiers);
- return new InstanceImpl<T>(injectionClazz, injectionPoint,
webBeansContext, parentCreationalContext, newQualifiersArray);
+ return new InstanceImpl<T>(injectionClazz, new
InstanceInjectionPoint(injectionPoint, newQualifiersArray), webBeansContext,
parentCreationalContext, newQualifiersArray);
}
/**
@@ -270,7 +273,6 @@ public class InstanceImpl<T> implements
Provider<T> provider =
webBeansContext.getNormalScopeProxyFactory().getInstanceProvider(proxy);
NormalScopedBeanInterceptorHandler handler =
(NormalScopedBeanInterceptorHandler)provider;
Bean<T> bean = (Bean<T>)handler.getBean();
- CreationalContext<T> creationalContext =
(CreationalContext<T>)parentCreationalContext;
Class<? extends Annotation> beanScope = bean.getScope();
Context currentContext =
webBeansContext.getBeanManagerImpl().getContext(beanScope);
if (currentContext instanceof AlterableContext)
@@ -358,10 +360,79 @@ public class InstanceImpl<T> implements
}
builder.append(qualifier.toString());
+ i++;
}
builder.append("}");
return builder.toString();
}
+
+ private static class InstanceInjectionPoint implements InjectionPoint,
Serializable
+ {
+ private InjectionPoint delegate;
+ private Set<Annotation> qualifiers;
+
+ protected InstanceInjectionPoint(final InjectionPoint injectionPoint,
final Annotation[] newQualifiersArray)
+ {
+ this.delegate = injectionPoint;
+ this.qualifiers = Collections.unmodifiableSet(new
HashSet<Annotation>(Arrays.asList(newQualifiersArray)));
+ }
+
+ @Override
+ public Type getType()
+ {
+ return delegate.getType();
+ }
+
+ @Override
+ public Set<Annotation> getQualifiers()
+ {
+ return qualifiers;
+ }
+
+ @Override
+ public Bean<?> getBean()
+ {
+ return delegate.getBean();
+ }
+
+ @Override
+ public Member getMember()
+ {
+ return delegate.getMember();
+ }
+
+ @Override
+ public Annotated getAnnotated()
+ {
+ return delegate.getAnnotated();
+ }
+
+ @Override
+ public boolean isDelegate()
+ {
+ return delegate.isDelegate();
+ }
+
+ @Override
+ public boolean isTransient()
+ {
+ return delegate.isTransient();
+ }
+
+ private void readObject(final ObjectInputStream inp) throws
IOException, ClassNotFoundException
+ {
+ final OwbCustomObjectInputStream owbCustomObjectInputStream = new
OwbCustomObjectInputStream(inp, WebBeansUtil.getCurrentClassLoader());
+ qualifiers =
Set.class.cast(owbCustomObjectInputStream.readObject());
+ delegate =
InjectionPoint.class.cast(owbCustomObjectInputStream.readObject());
+ }
+
+ private void writeObject(final ObjectOutputStream op) throws
IOException
+ {
+ final ObjectOutputStream out = new ObjectOutputStream(op);
+ out.writeObject(qualifiers);
+ out.writeObject(delegate);
+ }
+ }
}
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionPointProducer.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionPointProducer.java?rev=1732080&r1=1732079&r2=1732080&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionPointProducer.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/InjectionPointProducer.java
Wed Feb 24 11:46:02 2016
@@ -104,7 +104,7 @@ public class InjectionPointProducer exte
// nothing to do
}
- private class InjectionPointDelegate implements InjectionPoint,
Serializable
+ private static class InjectionPointDelegate implements InjectionPoint,
Serializable
{
private InjectionPoint ip;
private Type type;
Added:
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/instance/InstanceQualifierInjectionPointTest.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/instance/InstanceQualifierInjectionPointTest.java?rev=1732080&view=auto
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/instance/InstanceQualifierInjectionPointTest.java
(added)
+++
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/instance/InstanceQualifierInjectionPointTest.java
Wed Feb 24 11:46:02 2016
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.webbeans.test.instance;
+
+import org.apache.webbeans.test.AbstractUnitTest;
+import org.junit.Test;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.Any;
+import javax.enterprise.inject.Instance;
+import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.util.AnnotationLiteral;
+import javax.inject.Inject;
+import javax.inject.Qualifier;
+import java.lang.annotation.Annotation;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+
+public class InstanceQualifierInjectionPointTest extends AbstractUnitTest
+{
+ @Inject
+ private QualifiersHolder holder;
+
+ @Inject
+ @Any
+ private Instance<ShardContract> instance;
+
+ @Test
+ public void checkQualfiers() {
+ startContainer(Arrays.<Class<?>>asList(
+ Qualifier1.class,
+ QualifiersHolder.class,
+ Factory.class), Collections.<String>emptyList(), true);
+
+ assertNotNull(instance.select(new AnnotationLiteral<Qualifier1>()
{}).get());
+ assertEquals(2, holder.getQualifiers().size());
+ }
+
+
+ public static class Factory
+ {
+ @Inject
+ @Any
+ private Instance<ShardContract> instance;
+
+ @Inject
+ private QualifiersHolder holder;
+
+ @Produces
+ @Qualifier1
+ public ShardContract produces(final InjectionPoint ip)
+ {
+ holder.setQualifiers(ip.getQualifiers());
+ return new ShardContract()
+ {
+ };
+ }
+ }
+
+ @Target(METHOD)
+ @Retention(RUNTIME)
+ @Qualifier
+ public @interface Qualifier1
+ {
+ }
+
+ public interface ShardContract
+ {
+ }
+
+ @ApplicationScoped
+ public static class QualifiersHolder
+ {
+ private Collection<Annotation> qualifiers;
+
+ public Collection<Annotation> getQualifiers()
+ {
+ return qualifiers;
+ }
+
+ public void setQualifiers(final Collection<Annotation> qualifiers)
+ {
+ this.qualifiers = qualifiers;
+ }
+ }
+}