Author: rsandtner
Date: Fri Jun 2 20:42:20 2017
New Revision: 1797434
URL: http://svn.apache.org/viewvc?rev=1797434&view=rev
Log:
OWB-1187 first part of AnnotatedTypeConfigurator
Added:
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/configurator/
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/AnnotatedTypeConfiguratorImpl.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractAnnotated.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedTypeImpl.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/ProcessAnnotatedTypeImpl.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/discovery/BeforeBeanDiscoveryImpl.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/generics/GProcessAnnotatedType.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
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=1797434&r1=1797433&r2=1797434&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
Fri Jun 2 20:42:20 2017
@@ -18,6 +18,9 @@
*/
package org.apache.webbeans.configurator;
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.portable.AnnotatedTypeImpl;
+
import javax.enterprise.inject.spi.AnnotatedType;
import
javax.enterprise.inject.spi.configurator.AnnotatedConstructorConfigurator;
import javax.enterprise.inject.spi.configurator.AnnotatedFieldConfigurator;
@@ -26,32 +29,44 @@ import javax.enterprise.inject.spi.confi
import java.lang.annotation.Annotation;
import java.util.Set;
import java.util.function.Predicate;
-import java.util.stream.Stream;
public class AnnotatedTypeConfiguratorImpl implements AnnotatedTypeConfigurator
{
+
+ private final AnnotatedTypeImpl<?> annotatedType;
+
+
+ public AnnotatedTypeConfiguratorImpl(WebBeansContext webBeansContext,
AnnotatedType<?> annotatedType)
+ {
+ this.annotatedType = new AnnotatedTypeImpl<>(webBeansContext,
annotatedType);
+ }
+
+
@Override
public AnnotatedType getAnnotated()
{
- throw new UnsupportedOperationException("TODO implement CDI 2.0");
+ return annotatedType;
}
@Override
public AnnotatedTypeConfigurator add(Annotation annotation)
{
- throw new UnsupportedOperationException("TODO implement CDI 2.0");
+ annotatedType.addAnnotation(annotation);
+ return this;
}
@Override
public AnnotatedTypeConfigurator remove(Predicate predicate)
{
- throw new UnsupportedOperationException("TODO implement CDI 2.0");
+ annotatedType.getAnnotations().removeIf(predicate);
+ return this;
}
@Override
public AnnotatedTypeConfigurator removeAll()
{
- throw new UnsupportedOperationException("TODO implement CDI 2.0");
+ annotatedType.clearAnnotations();
+ return this;
}
@Override
@@ -61,32 +76,15 @@ public class AnnotatedTypeConfiguratorIm
}
@Override
- public Stream<AnnotatedMethodConfigurator> filterMethods(Predicate
predicate)
- {
- throw new UnsupportedOperationException("TODO implement CDI 2.0");
- }
-
- @Override
public Set<AnnotatedFieldConfigurator> fields()
{
throw new UnsupportedOperationException("TODO implement CDI 2.0");
}
@Override
- public Stream<AnnotatedFieldConfigurator> filterFields(Predicate predicate)
- {
- throw new UnsupportedOperationException("TODO implement CDI 2.0");
- }
-
- @Override
public Set<AnnotatedConstructorConfigurator> constructors()
{
throw new UnsupportedOperationException("TODO implement CDI 2.0");
}
- @Override
- public Stream<AnnotatedConstructorConfigurator>
filterConstructors(Predicate predicate)
- {
- throw new UnsupportedOperationException("TODO implement CDI 2.0");
- }
}
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractAnnotated.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractAnnotated.java?rev=1797434&r1=1797433&r2=1797434&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractAnnotated.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AbstractAnnotated.java
Fri Jun 2 20:42:20 2017
@@ -65,11 +65,28 @@ abstract class AbstractAnnotated impleme
}
/**
+ * Copy consturctor
+ *
+ * @param webBeansContext current {@link WebBeansContext}
+ * @param annotated to copy
+ */
+ protected AbstractAnnotated(WebBeansContext webBeansContext, Annotated
annotated)
+ {
+ this.baseType = annotated.getBaseType();
+ this.webBeansContext = webBeansContext;
+
+ this.typeClosures = annotated.getTypeClosure();
+ this.annotations.addAll(annotated.getAnnotations());
+ }
+
+
+
+ /**
* Adds new annotation to set.
*
* @param annotation new annotation
*/
- protected void addAnnotation(Annotation annotation)
+ public void addAnnotation(Annotation annotation)
{
annotations.add(annotation);
}
@@ -86,9 +103,14 @@ abstract class AbstractAnnotated impleme
*/
protected void setAnnotations(Annotation[] annotations)
{
- this.annotations.clear();
+ clearAnnotations();
Collections.addAll(this.annotations, annotations);
}
+
+ public void clearAnnotations()
+ {
+ annotations.clear();
+ }
/**
* {@inheritDoc}
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedTypeImpl.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedTypeImpl.java?rev=1797434&r1=1797433&r2=1797434&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedTypeImpl.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedTypeImpl.java
Fri Jun 2 20:42:20 2017
@@ -43,7 +43,7 @@ import org.apache.webbeans.config.WebBea
* @param <X> class type
* @version $Rev$ $Date$
*/
-class AnnotatedTypeImpl<X>
+public class AnnotatedTypeImpl<X>
extends AbstractAnnotated
implements AnnotatedType<X>
{
@@ -105,6 +105,32 @@ class AnnotatedTypeImpl<X>
}
/**
+ * Copy constructor
+ *
+ * @param webBeansContext actual {@link WebBeansContext}
+ * @param annotatedType to copy
+ */
+ public AnnotatedTypeImpl(WebBeansContext webBeansContext, AnnotatedType
annotatedType)
+ {
+ super(webBeansContext, annotatedType);
+ this.annotatedClass = annotatedType.getJavaClass();
+
+ //X TODO revisit!!
+ if (annotatedType instanceof AnnotatedTypeImpl)
+ {
+ AnnotatedTypeImpl annotatedTypeImpl = (AnnotatedTypeImpl)
annotatedType;
+ this.supertype = annotatedTypeImpl.supertype;
+ this.state = annotatedTypeImpl.state;
+ }
+ else
+ {
+ // X TODO
+ this.supertype = null;
+ }
+
+ }
+
+ /**
* {@inheritDoc}
*/
@Override
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/ProcessAnnotatedTypeImpl.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/ProcessAnnotatedTypeImpl.java?rev=1797434&r1=1797433&r2=1797434&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/ProcessAnnotatedTypeImpl.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/ProcessAnnotatedTypeImpl.java
Fri Jun 2 20:42:20 2017
@@ -18,6 +18,9 @@
*/
package org.apache.webbeans.portable.events;
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.configurator.AnnotatedTypeConfiguratorImpl;
+
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator;
@@ -29,6 +32,10 @@ import javax.enterprise.inject.spi.confi
*/
public class ProcessAnnotatedTypeImpl<X> extends EventBase implements
ProcessAnnotatedType<X>
{
+
+ private final WebBeansContext webBeansContext;
+
+
/**Annotated Type*/
private AnnotatedType<X> annotatedType = null;
@@ -42,13 +49,16 @@ public class ProcessAnnotatedTypeImpl<X>
*/
private boolean modifiedAnnotatedType = false;
+ private AnnotatedTypeConfiguratorImpl configurator;
+
/**
* Creates a new instance with the given annotated type.
*
* @param annotatedType annotated type
*/
- public ProcessAnnotatedTypeImpl(AnnotatedType<X> annotatedType)
+ public ProcessAnnotatedTypeImpl(WebBeansContext webBeansContext,
AnnotatedType<X> annotatedType)
{
+ this.webBeansContext = webBeansContext;
this.annotatedType = annotatedType;
}
@@ -59,7 +69,15 @@ public class ProcessAnnotatedTypeImpl<X>
public AnnotatedType<X> getAnnotatedType()
{
checkState();
- return annotatedType;
+
+ if (configurator == null)
+ {
+ return annotatedType;
+ }
+ else
+ {
+ return configurator.getAnnotated();
+ }
}
/**
@@ -71,6 +89,10 @@ public class ProcessAnnotatedTypeImpl<X>
checkState();
annotatedType = type;
modifiedAnnotatedType = true;
+
+ //X TODO test
+ // reset configurator
+ configurator = null;
}
/**
@@ -92,11 +114,15 @@ public class ProcessAnnotatedTypeImpl<X>
veto = true;
}
- //X TODO OWB-1182 CDI 2.0
@Override
public AnnotatedTypeConfigurator<X> configureAnnotatedType()
{
- throw new UnsupportedOperationException("CDI 2.0 not yet imlemented");
+ if (configurator == null)
+ {
+ configurator = new AnnotatedTypeConfiguratorImpl(webBeansContext,
annotatedType);
+ }
+
+ return configurator;
}
/**
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/discovery/BeforeBeanDiscoveryImpl.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/discovery/BeforeBeanDiscoveryImpl.java?rev=1797434&r1=1797433&r2=1797434&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/discovery/BeforeBeanDiscoveryImpl.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/discovery/BeforeBeanDiscoveryImpl.java
Fri Jun 2 20:42:20 2017
@@ -138,7 +138,7 @@ public class BeforeBeanDiscoveryImpl imp
//X TODO OWB-1182 CDI 2.0
@Override
- public <T> AnnotatedTypeConfigurator<T> addAnnotatedType(Class<T> aClass,
String s)
+ public <T> AnnotatedTypeConfigurator<T> addAnnotatedType(Class<T> clazz,
String id)
{
throw new UnsupportedOperationException("CDI 2.0 not yet imlemented");
}
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/generics/GProcessAnnotatedType.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/generics/GProcessAnnotatedType.java?rev=1797434&r1=1797433&r2=1797434&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/generics/GProcessAnnotatedType.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/portable/events/generics/GProcessAnnotatedType.java
Fri Jun 2 20:42:20 2017
@@ -20,14 +20,15 @@ package org.apache.webbeans.portable.eve
import javax.enterprise.inject.spi.AnnotatedType;
+import org.apache.webbeans.config.WebBeansContext;
import org.apache.webbeans.portable.events.ProcessAnnotatedTypeImpl;
@SuppressWarnings("unchecked")
public class GProcessAnnotatedType extends ProcessAnnotatedTypeImpl implements
GenericBeanEvent
{
- public GProcessAnnotatedType(AnnotatedType annotatedType )
+ public GProcessAnnotatedType(WebBeansContext webBeansContext,
AnnotatedType annotatedType )
{
- super(annotatedType);
+ super(webBeansContext, annotatedType);
}
@Override
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java?rev=1797434&r1=1797433&r2=1797434&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
Fri Jun 2 20:42:20 2017
@@ -971,7 +971,7 @@ public final class WebBeansUtil
*/
public <T> GProcessAnnotatedType
fireProcessAnnotatedTypeEvent(AnnotatedType<T> annotatedType)
{
- GProcessAnnotatedType processAnnotatedEvent = new
GProcessAnnotatedType(annotatedType);
+ GProcessAnnotatedType processAnnotatedEvent = new
GProcessAnnotatedType(webBeansContext, annotatedType);
//Fires ProcessAnnotatedType
webBeansContext.getBeanManagerImpl().fireEvent(processAnnotatedEvent,
true, AnnotationUtil.EMPTY_ANNOTATION_ARRAY);
Added:
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=1797434&view=auto
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/configurator/AnnotatedTypeConfiguratorImplTest.java
(added)
+++
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/configurator/AnnotatedTypeConfiguratorImplTest.java
Fri Jun 2 20:42:20 2017
@@ -0,0 +1,207 @@
+/*
+ * 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.configurator;
+
+import org.apache.webbeans.test.AbstractUnitTest;
+import org.junit.Test;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.ProcessAnnotatedType;
+import javax.enterprise.inject.spi.ProcessBeanAttributes;
+import javax.enterprise.util.AnnotationLiteral;
+import javax.inject.Qualifier;
+import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.util.Set;
+import java.util.function.Consumer;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class AnnotatedTypeConfiguratorImplTest extends AbstractUnitTest
+{
+
+ @Test
+ 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();
+ }
+
+ @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();
+ }
+
+
+ @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();
+ }
+
+ @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();
+ }
+
+ @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();
+ }
+
+ @Test
+ public void testRemoveAllAnnotations_classAlreadyContainsAnnotations()
+ {
+ AnnotatedTypeConfiguratorExtension extension = new
AnnotatedTypeConfiguratorExtension(
+ pat -> pat.configureAnnotatedType().removeAll(),
+ pba ->
+ {
+ Set<Annotation> annotations =
pba.getAnnotated().getAnnotations();
+ assertTrue(annotations.isEmpty());
+ });
+
+ addExtension(extension);
+ startContainer(AnnotatedTypeConfigClass.class);
+ shutdown();
+ }
+
+
+ public static class AnnotatedTypeConfiguratorExtension implements Extension
+ {
+
+ private final Consumer<ProcessAnnotatedType<AnnotatedTypeConfigClass>>
typeConfigurator;
+ private final Consumer<ProcessBeanAttributes> beanAttributesConsumer;
+
+
AnnotatedTypeConfiguratorExtension(Consumer<ProcessAnnotatedType<AnnotatedTypeConfigClass>>
typeConfigurator,
+ Consumer<ProcessBeanAttributes>
beanAttributesConsumer)
+ {
+ this.typeConfigurator = typeConfigurator;
+ this.beanAttributesConsumer = beanAttributesConsumer;
+ }
+
+ public void createAnnotatedType(@Observes
ProcessAnnotatedType<AnnotatedTypeConfigClass> pat)
+ {
+ typeConfigurator.accept(pat);
+ }
+
+ public void getCreatedAnnotatedType(@Observes
ProcessBeanAttributes<AnnotatedTypeConfigClass> pba)
+ {
+ beanAttributesConsumer.accept(pba);
+ }
+
+ }
+
+ public static class AnnotatedTypeConfigClass
+ {
+ }
+
+ @TheQualifier(value = "default")
+ public static class AnnotatedTypeConfigClassWithAnnotation
+ {
+
+ }
+
+ @Qualifier
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target(value = {ElementType.TYPE, ElementType.METHOD, ElementType.FIELD,
ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.ANNOTATION_TYPE})
+ public @interface TheQualifier
+ {
+ String value();
+ }
+
+ public static class TheQualifierLiteral extends
AnnotationLiteral<TheQualifier> implements TheQualifier
+ {
+
+ private final String value;
+
+ TheQualifierLiteral(String value)
+ {
+ this.value = value;
+ }
+
+ public String value()
+ {
+ return value;
+ }
+ }
+}