You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by rm...@apache.org on 2019/09/17 09:22:19 UTC
[openwebbeans] branch master updated: OWB-1298 ensure annotatedtype
equals method is valid enabling our beansdeployer logic to deduplicate
instances
This is an automated email from the ASF dual-hosted git repository.
rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git
The following commit(s) were added to refs/heads/master by this push:
new 919c48b OWB-1298 ensure annotatedtype equals method is valid enabling our beansdeployer logic to deduplicate instances
919c48b is described below
commit 919c48b50dab4e03300b7133c11cc5c6e50756f4
Author: Romain Manni-Bucau <rm...@gmail.com>
AuthorDate: Tue Sep 17 11:22:08 2019 +0200
OWB-1298 ensure annotatedtype equals method is valid enabling our beansdeployer logic to deduplicate instances
---
.../webbeans/container/AnnotatedTypeWrapper.java | 28 ++++++++++
.../webbeans/portable/AnnotatedTypeImpl.java | 29 ++++++++++
.../webbeans/portable/AnnotatedTypeImplTest.java | 63 ++++++++++++++++++++++
.../test/portable/BeforeBeanDiscoveryTest.java | 29 ++++++++++
4 files changed, 149 insertions(+)
diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/container/AnnotatedTypeWrapper.java b/webbeans-impl/src/main/java/org/apache/webbeans/container/AnnotatedTypeWrapper.java
index 5fe0b02..3a5d284 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/container/AnnotatedTypeWrapper.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/container/AnnotatedTypeWrapper.java
@@ -104,4 +104,32 @@ public class AnnotatedTypeWrapper<T> implements AnnotatedType<T>
{
return source;
}
+
+ @Override
+ public boolean equals(final Object o)
+ {
+ if (this == o)
+ {
+ return true;
+ }
+ if (o == null)
+ {
+ return false;
+ }
+ if (getClass() == o.getClass())
+ {
+ return false; // == should have worked
+ }
+ if (AnnotatedType.class.isInstance(o))
+ {
+ return original.equals(o);
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return original.hashCode();
+ }
}
diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedTypeImpl.java b/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedTypeImpl.java
index fb668c0..1610b74 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedTypeImpl.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/portable/AnnotatedTypeImpl.java
@@ -183,6 +183,35 @@ public class AnnotatedTypeImpl<X>
return getJavaClass();
}
+ @Override
+ public boolean equals(final Object o)
+ {
+ if (this == o)
+ {
+ return true;
+ }
+ if (null == o)
+ {
+ return false;
+ }
+ final Class<?> otherClass = o.getClass();
+ if (getClass() == otherClass)
+ {
+ return false; // == should have worked
+ }
+ if (AnnotatedType.class.isInstance(o))
+ {
+ return o.equals(this);
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() // enough, no need to create a hashcode from attributes
+ {
+ return super.hashCode();
+ }
+
private State getState()
{
State result = state;
diff --git a/webbeans-impl/src/test/java/org/apache/webbeans/portable/AnnotatedTypeImplTest.java b/webbeans-impl/src/test/java/org/apache/webbeans/portable/AnnotatedTypeImplTest.java
new file mode 100644
index 0000000..74a24fb
--- /dev/null
+++ b/webbeans-impl/src/test/java/org/apache/webbeans/portable/AnnotatedTypeImplTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.portable;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+import javax.enterprise.inject.spi.AnnotatedType;
+
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.container.AnnotatedTypeWrapper;
+import org.junit.Test;
+
+public class AnnotatedTypeImplTest
+{
+ @Test
+ public void equalsEvaluation()
+ {
+ final AnnotatedType<Foo> annotatedType = new AnnotatedTypeImpl<>(
+ new WebBeansContext(), Foo.class, null);
+ assertEquals(annotatedType, annotatedType); // obvious but does not cost much and is required
+
+ final AnnotatedType<Foo> wrapped = new AnnotatedTypeWrapper<>(null, annotatedType, "");
+ assertEquals(annotatedType, wrapped);
+ assertEquals(wrapped, annotatedType);
+
+ final AnnotatedType<AnnotatedTypeImplTest> anotherAnnotatedType = new AnnotatedTypeImpl<>(
+ new WebBeansContext(), AnnotatedTypeImplTest.class, null);
+ assertNotEquals(wrapped, anotherAnnotatedType);
+ assertNotEquals(anotherAnnotatedType, wrapped);
+ }
+
+ @Test
+ public void hashCodeComputation()
+ {
+ final AnnotatedType<Foo> annotatedType = new AnnotatedTypeImpl<>(
+ new WebBeansContext(), Foo.class, null);
+ assertEquals(annotatedType.hashCode(), annotatedType.hashCode());
+
+ final AnnotatedType<Foo> wrapped = new AnnotatedTypeWrapper<>(null, annotatedType, "");
+ assertEquals(annotatedType.hashCode(), wrapped.hashCode());
+ }
+
+ public static class Foo
+ {
+ }
+}
diff --git a/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/BeforeBeanDiscoveryTest.java b/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/BeforeBeanDiscoveryTest.java
index 16dda41..8429358 100644
--- a/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/BeforeBeanDiscoveryTest.java
+++ b/webbeans-impl/src/test/java/org/apache/webbeans/test/portable/BeforeBeanDiscoveryTest.java
@@ -1,10 +1,17 @@
package org.apache.webbeans.test.portable;
+import static org.junit.Assert.assertNotNull;
+
import java.util.ArrayList;
import java.util.Collection;
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.event.Observes;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.BeforeBeanDiscovery;
+import javax.enterprise.inject.spi.Extension;
import javax.enterprise.util.AnnotationLiteral;
import org.junit.Assert;
@@ -33,6 +40,23 @@ import org.junit.Test;
public class BeforeBeanDiscoveryTest extends AbstractUnitTest
{
+ @Test
+ public void testAddAdditionalAnnotatedTypeFallbackForNull()
+ {
+ addExtension(new Extension()
+ {
+ void addBean(@Observes final BeforeBeanDiscovery beforeBeanDiscovery, final BeanManager beanManager)
+ {
+ beforeBeanDiscovery.addAnnotatedType(beanManager.createAnnotatedType(Foo.class), null);
+ }
+ });
+ startContainer(Foo.class);
+
+ // just check we don't have :
+ // javax.enterprise.inject.AmbiguousResolutionException: There is more than one Bean @Default
+ final Foo instance = getInstance(Foo.class);
+ assertNotNull(instance);
+ }
@Test
public void testAddAdditionalAnnotatedType()
@@ -79,4 +103,9 @@ public class BeforeBeanDiscoveryTest extends AbstractUnitTest
shutDownContainer();
}
+
+ @ApplicationScoped
+ public static class Foo
+ {
+ }
}