Author: struberg
Date: Mon Jun 5 16:06:20 2017
New Revision: 1797670
URL: http://svn.apache.org/viewvc?rev=1797670&view=rev
Log:
OWB-1187 AnnotatedMethodConfigurator
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/AnnotatedMethodConfiguratorImpl.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/AnnotatedTypeConfiguratorImpl.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractAnnotatedCallable.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedMethodImpl.java
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/configurator/AnnotatedTypeConfiguratorImplTest.java
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/AnnotatedMethodConfiguratorImpl.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/AnnotatedMethodConfiguratorImpl.java?rev=1797670&r1=1797669&r2=1797670&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/AnnotatedMethodConfiguratorImpl.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/AnnotatedMethodConfiguratorImpl.java
Mon Jun 5 16:06:20 2017
@@ -26,18 +26,29 @@ import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;
+import org.apache.webbeans.portable.AnnotatedMethodImpl;
+
+
public class AnnotatedMethodConfiguratorImpl<T> implements
AnnotatedMethodConfigurator<T>
{
+ private final AnnotatedMethodImpl<T> annotatedMethod;
+
+ public AnnotatedMethodConfiguratorImpl(AnnotatedMethodImpl<T>
annotatedMethod)
+ {
+ this.annotatedMethod = annotatedMethod;
+ }
+
@Override
public AnnotatedMethod<T> getAnnotated()
{
- throw new UnsupportedOperationException("TODO implement CDI 2.0");
+ return annotatedMethod;
}
@Override
public AnnotatedMethodConfigurator<T> add(Annotation annotation)
{
- throw new UnsupportedOperationException("TODO implement CDI 2.0");
+ annotatedMethod.addAnnotation(annotation);
+ return this;
}
@Override
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/AnnotatedTypeConfiguratorImpl.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/AnnotatedTypeConfiguratorImpl.java?rev=1797670&r1=1797669&r2=1797670&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/AnnotatedTypeConfiguratorImpl.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/configurator/AnnotatedTypeConfiguratorImpl.java
Mon Jun 5 16:06:20 2017
@@ -19,6 +19,7 @@
package org.apache.webbeans.configurator;
import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.portable.AnnotatedMethodImpl;
import org.apache.webbeans.portable.AnnotatedTypeImpl;
import javax.enterprise.inject.spi.AnnotatedType;
@@ -29,16 +30,22 @@ import javax.enterprise.inject.spi.confi
import java.lang.annotation.Annotation;
import java.util.Set;
import java.util.function.Predicate;
+import java.util.stream.Collectors;
public class AnnotatedTypeConfiguratorImpl<T> implements
AnnotatedTypeConfigurator<T>
{
private final AnnotatedTypeImpl<T> annotatedType;
+ private Set<AnnotatedMethodConfigurator<? super T>>
annotatedMethodConfigurators;
- public AnnotatedTypeConfiguratorImpl(WebBeansContext webBeansContext,
AnnotatedType<?> annotatedType)
+ public AnnotatedTypeConfiguratorImpl(WebBeansContext webBeansContext,
AnnotatedType<T> originalAnnotatedType)
{
- this.annotatedType = new AnnotatedTypeImpl<>(webBeansContext,
annotatedType);
+ this.annotatedType = new AnnotatedTypeImpl<>(webBeansContext,
originalAnnotatedType);
+
+ annotatedMethodConfigurators = annotatedType.getMethods().stream()
+ .map(m -> new
AnnotatedMethodConfiguratorImpl<>((AnnotatedMethodImpl<T>) m))
+ .collect(Collectors.toSet());
}
@@ -72,7 +79,7 @@ public class AnnotatedTypeConfiguratorIm
@Override
public Set<AnnotatedMethodConfigurator<? super T>> methods()
{
- throw new UnsupportedOperationException("TODO implement CDI 2.0");
+ return annotatedMethodConfigurators;
}
@Override
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractAnnotatedCallable.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractAnnotatedCallable.java?rev=1797670&r1=1797669&r2=1797670&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractAnnotatedCallable.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractAnnotatedCallable.java
Mon Jun 5 16:06:20 2017
@@ -37,7 +37,7 @@ import javax.enterprise.inject.spi.Annot
*
* @param <X> declaring class
*/
-abstract class AbstractAnnotatedCallable<X> extends AbstractAnnotatedMember<X>
implements AnnotatedCallable<X>
+public abstract class AbstractAnnotatedCallable<X> extends
AbstractAnnotatedMember<X> implements AnnotatedCallable<X>
{
/**Annotated parameters*/
private List<AnnotatedParameter<X>> annotatedParameters = new
ArrayList<AnnotatedParameter<X>>();
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedMethodImpl.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedMethodImpl.java?rev=1797670&r1=1797669&r2=1797670&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedMethodImpl.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedMethodImpl.java
Mon Jun 5 16:06:20 2017
@@ -35,7 +35,7 @@ import javax.enterprise.inject.spi.Annot
*
* @param <X> class info
*/
-class AnnotatedMethodImpl<X> extends AbstractAnnotatedCallable<X> implements
AnnotatedMethod<X>
+public class AnnotatedMethodImpl<X> extends AbstractAnnotatedCallable<X>
implements AnnotatedMethod<X>
{
/**
@@ -51,6 +51,16 @@ class AnnotatedMethodImpl<X> extends Abs
setAnnotatedParameters(GenericsUtil.resolveParameterTypes(declaringType.getJavaClass(),
javaMember), javaMember.getParameterAnnotations());
}
+ /**
+ * Copy ct for Configurators
+ */
+ public AnnotatedMethodImpl(WebBeansContext webBeansContext,
AnnotatedType<X> declaringType, AnnotatedMethod<X> originalAnnotatedMethod)
+ {
+ super(webBeansContext, originalAnnotatedMethod.getBaseType(),
originalAnnotatedMethod.getJavaMember(), declaringType);
+
+ //X TODO copy AnnotatedParameters
+ }
+
@Override
protected Set<Type> extractTypeClojure(final Type baseType)
{ // we want to skip hasTypeParameters() check which is already done for
methods
Modified:
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/configurator/AnnotatedTypeConfiguratorImplTest.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/configurator/AnnotatedTypeConfiguratorImplTest.java?rev=1797670&r1=1797669&r2=1797670&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/configurator/AnnotatedTypeConfiguratorImplTest.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/configurator/AnnotatedTypeConfiguratorImplTest.java
Mon Jun 5 16:06:20 2017
@@ -19,9 +19,11 @@
package org.apache.webbeans.test.configurator;
import org.apache.webbeans.test.AbstractUnitTest;
+import org.junit.Assert;
import org.junit.Test;
import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.ProcessBeanAttributes;
@@ -45,107 +47,134 @@ public class AnnotatedTypeConfiguratorIm
public void testAddAnnotationToClass()
{
- AnnotatedTypeConfiguratorExtension extension = new
AnnotatedTypeConfiguratorExtension(pat -> pat.configureAnnotatedType().add(new
TheQualifierLiteral("type")),
-
pba ->
-
{
-
Set<Annotation> annotations =
pba.getAnnotated().getAnnotations();
-
assertEquals(1, annotations.size());
-
assertEquals(TheQualifier.class,
annotations.iterator().next().annotationType());
-
});
- addExtension(extension);
- startContainer(AnnotatedTypeConfigClass.class);
- shutdown();
+ checkAnnotatedType(
+ pat -> pat.configureAnnotatedType().add(new
TheQualifierLiteral("type")),
+ pba ->
+ {
+ Set<Annotation> annotations =
pba.getAnnotated().getAnnotations();
+ assertEquals(1, annotations.size());
+ assertEquals(TheQualifier.class,
annotations.iterator().next().annotationType());
+ },
+ AnnotatedTypeConfigClass.class);
}
@Test
public void testAddAnnotationToClass_classAlreadyContainsAnnotations()
{
- AnnotatedTypeConfiguratorExtension extension = new
AnnotatedTypeConfiguratorExtension(pat -> pat.configureAnnotatedType().add(new
TheQualifierLiteral("type")),
-
pba ->
-
{
-
Set<Annotation> annotations =
pba.getAnnotated().getAnnotations();
-
assertEquals(2, annotations.size());
-
});
- addExtension(extension);
- startContainer(AnnotatedTypeConfigClassWithAnnotation.class);
- shutdown();
+ checkAnnotatedType(
+ pat -> pat.configureAnnotatedType().add(new
TheQualifierLiteral("type")),
+ pba ->
+ {
+ Set<Annotation> annotations =
pba.getAnnotated().getAnnotations();
+ assertEquals(2, annotations.size());
+ },
+ AnnotatedTypeConfigClassWithAnnotation.class);
}
@Test
public void testRemoveAnnotation()
{
- AnnotatedTypeConfiguratorExtension extension = new
AnnotatedTypeConfiguratorExtension(
- pat -> pat.configureAnnotatedType().add(new
TheQualifierLiteral("one"))
- .add(new TheQualifierLiteral("two"))
- .remove(a -> ((TheQualifier)
a).value().equals("two")),
- pba ->
- {
- Set<Annotation> annotations =
pba.getAnnotated().getAnnotations();
- assertEquals(1, annotations.size());
- Annotation annotation = annotations.iterator().next();
- assertEquals(TheQualifier.class,
annotation.annotationType());
- assertEquals("one", ((TheQualifier) annotation).value());
- });
-
- addExtension(extension);
- startContainer(AnnotatedTypeConfigClass.class);
- shutdown();
+ checkAnnotatedType(
+ pat -> pat.configureAnnotatedType().add(new
TheQualifierLiteral("one"))
+ .add(new TheQualifierLiteral("two"))
+ .remove(a -> ((TheQualifier) a).value().equals("two")),
+ pba ->
+ {
+ Set<Annotation> annotations =
pba.getAnnotated().getAnnotations();
+ assertEquals(1, annotations.size());
+ Annotation annotation = annotations.iterator().next();
+ assertEquals(TheQualifier.class, annotation.annotationType());
+ assertEquals("one", ((TheQualifier) annotation).value());
+ },
+ AnnotatedTypeConfigClass.class);
}
@Test
public void testRemoveAnnotation_classAlreadyContainsAnnotations()
{
- AnnotatedTypeConfiguratorExtension extension = new
AnnotatedTypeConfiguratorExtension(
- pat -> pat.configureAnnotatedType().add(new
TheQualifierLiteral("one"))
- .remove(a -> ((TheQualifier)
a).value().equals("one")),
- pba ->
- {
- Set<Annotation> annotations =
pba.getAnnotated().getAnnotations();
- assertEquals(2, annotations.size());
- });
-
- addExtension(extension);
- startContainer(AnnotatedTypeConfigClassWithAnnotation.class);
- shutdown();
+ checkAnnotatedType(
+ pat -> pat.configureAnnotatedType().add(new
TheQualifierLiteral("one"))
+ .remove(a -> ((TheQualifier) a).value().equals("one")),
+ pba ->
+ {
+ Set<Annotation> annotations =
pba.getAnnotated().getAnnotations();
+ assertEquals(2, annotations.size());
+ },
+ AnnotatedTypeConfigClassWithAnnotation.class);
}
@Test
public void testRemoveAllAnnotations()
{
- AnnotatedTypeConfiguratorExtension extension = new
AnnotatedTypeConfiguratorExtension(
- pat -> pat.configureAnnotatedType().add(new
TheQualifierLiteral("one"))
- .add(new TheQualifierLiteral("two"))
- .removeAll(),
- pba ->
- {
- Set<Annotation> annotations =
pba.getAnnotated().getAnnotations();
- assertTrue(annotations.isEmpty());
- });
-
- addExtension(extension);
- startContainer(AnnotatedTypeConfigClass.class);
- shutdown();
+ checkAnnotatedType(
+ pat -> pat.configureAnnotatedType().add(new
TheQualifierLiteral("one"))
+ .add(new TheQualifierLiteral("two"))
+ .removeAll(),
+ pba ->
+ {
+ Set<Annotation> annotations =
pba.getAnnotated().getAnnotations();
+ assertTrue(annotations.isEmpty());
+ },
+ AnnotatedTypeConfigClass.class);
}
@Test
public void testRemoveAllAnnotations_classAlreadyContainsAnnotations()
{
- AnnotatedTypeConfiguratorExtension extension = new
AnnotatedTypeConfiguratorExtension(
- pat -> pat.configureAnnotatedType().removeAll(),
- pba ->
- {
- Set<Annotation> annotations =
pba.getAnnotated().getAnnotations();
- assertTrue(annotations.isEmpty());
- });
+ checkAnnotatedType(
+ pat -> pat.configureAnnotatedType().removeAll(),
+ pba ->
+ {
+ Set<Annotation> annotations =
pba.getAnnotated().getAnnotations();
+ assertTrue(annotations.isEmpty());
+ },
+ AnnotatedTypeConfigClass.class);
+ }
+
+ @Test
+ public void testAddAnnotationToMethod()
+ {
+ checkAnnotatedType(
+ pat ->
+ {
+ pat.configureAnnotatedType()
+ .filterMethods(m ->
m.getJavaMember().getName().equals("method1"))
+ .findFirst()
+ .get()
+ .add(new TheQualifierLiteral("Method1"));
+ },
+ pba ->
+ {
+ Assert.assertTrue(pba.getAnnotated() instanceof AnnotatedType);
+ AnnotatedType<?> at = (AnnotatedType<?>) pba.getAnnotated();
+ Set<Annotation> annotations = at.getMethods().stream()
+ .filter(m -> m.getJavaMember().getName().equals("method1"))
+ .findFirst()
+ .get().getAnnotations();
+ assertEquals(1, annotations.size());
+ Annotation ann = annotations.iterator().next();
+ assertEquals(TheQualifier.class, ann.annotationType());
+ assertEquals("Method1", ((TheQualifier) ann).value());
+
+ }, AnnotatedTypeConfigClass.class);
+ }
+
+ private void
checkAnnotatedType(Consumer<ProcessAnnotatedType<AnnotatedTypeConfigClass>>
typeConfigurator,
+ Consumer<ProcessBeanAttributes>
beanAttributesConsumer,
+ Class<?> classToCheck)
+ {
+ AnnotatedTypeConfiguratorExtension extension
+ = new
AnnotatedTypeConfiguratorExtension(typeConfigurator,beanAttributesConsumer);
addExtension(extension);
- startContainer(AnnotatedTypeConfigClass.class);
+ startContainer(classToCheck);
shutdown();
}
+
public static class AnnotatedTypeConfiguratorExtension implements Extension
{
@@ -173,6 +202,18 @@ public class AnnotatedTypeConfiguratorIm
public static class AnnotatedTypeConfigClass
{
+ private String field1;
+ private Integer field2;
+
+ public void method1()
+ {
+ // do nothing
+ }
+
+ public String method2()
+ {
+ return "method 2";
+ }
}
@TheQualifier(value = "default")