Skip to content

Commit d33349a

Browse files
committed
Fix issue with Generics in ProcessAnnotatedTypes event
1 parent 18f37f8 commit d33349a

File tree

6 files changed

+350
-23
lines changed

6 files changed

+350
-23
lines changed

webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.lang.reflect.InvocationTargetException;
2626
import java.lang.reflect.ParameterizedType;
2727
import java.lang.reflect.Type;
28-
import java.lang.reflect.TypeVariable;
2928
import java.util.ArrayList;
3029
import java.util.Collection;
3130
import java.util.Comparator;
@@ -747,19 +746,10 @@ private boolean checkEventTypeParameterForExtensions(Type beanClass, Type observ
747746
{
748747
if(ClassUtil.isTypeVariable(observerTypeActualArg))
749748
{
750-
TypeVariable<?> tv = (TypeVariable<?>)observerTypeActualArg;
751-
Type tvBound = tv.getBounds()[0];
752-
753-
if(tvBound instanceof Class)
749+
if (Class.class.isInstance(beanClass) && GenericsUtil.isAssignableFrom(false, true, observerTypeActualArg, beanClass, new HashMap<>()))
754750
{
755-
Class<?> clazzTvBound = (Class<?>)tvBound;
756-
757-
if(Class.class.isInstance(beanClass) && clazzTvBound.isAssignableFrom(Class.class.cast(beanClass)))
758-
{
759-
return true;
760-
}
761-
}
762-
751+
return true;
752+
}
763753
}
764754
else if(ClassUtil.isWildCardType(observerTypeActualArg))
765755
{
@@ -773,9 +763,9 @@ else if(observerTypeActualArg instanceof Class)
773763
return true;
774764
}
775765
}
776-
else if (observerTypeActualArg instanceof ParameterizedType)
766+
else if(observerTypeActualArg instanceof ParameterizedType)
777767
{
778-
return GenericsUtil.isAssignableFrom(false, true, observerTypeActualArg, beanClass, new HashMap<>());
768+
return GenericsUtil.isAssignableFrom(true, true, observerTypeActualArg, beanClass, new HashMap<>());
779769
}
780770

781771
return false;

webbeans-impl/src/test/java/org/apache/webbeans/test/portable/WithAnnotationTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ public void testWithAnnotation()
5151

5252
Assert.assertEquals(4, WithAnnotationExtension.scannedClasses);
5353
Assert.assertEquals(1, WithAnnotationExtension.one);
54+
Assert.assertEquals(1, WithAnnotationExtension.extend);
5455
}
5556

5657

5758
public static class WithAnnotationExtension implements Extension
5859
{
5960
public static int scannedClasses = 0;
6061
public static int one = 0;
62+
public static int extend = 0;
6163

6264
public void processClassess(@Observes @WithAnnotations(MyAnnoation.class) ProcessAnnotatedType pat)
6365
{
@@ -73,6 +75,12 @@ public void noIssueWithGenericsOWB997(@Observes @WithAnnotations(MyAnnoation.cla
7375
{
7476
one++;
7577
}
78+
79+
<T extends WithAnnotatedClass> void noIssueWithExtendGenericsOWB997(@Observes @WithAnnotations(MyAnnoation.class) ProcessAnnotatedType<T> pat)
80+
{
81+
extend++;
82+
}
83+
7684
}
7785

7886
@Retention(RetentionPolicy.RUNTIME)

webbeans-impl/src/test/java/org/apache/webbeans/test/portable/events/PortableEventTest.java

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,28 @@
2121
import java.util.ArrayList;
2222
import java.util.Collection;
2323

24-
import org.junit.Assert;
25-
26-
import org.apache.webbeans.test.AbstractUnitTest;
27-
import org.apache.webbeans.test.portable.events.beans.Apple;
28-
import org.apache.webbeans.test.portable.events.beans.AppleTree;
29-
import org.apache.webbeans.test.portable.events.beans.Cherry;
30-
import org.apache.webbeans.test.portable.events.beans.CherryTree;
31-
import org.apache.webbeans.test.portable.events.beans.Tree;
3224
import org.apache.webbeans.test.portable.events.extensions.AppleExtension;
3325
import org.apache.webbeans.test.portable.events.extensions.AppleExtension1;
3426
import org.apache.webbeans.test.portable.events.extensions.MessageReceiverExtension;
3527
import org.apache.webbeans.test.portable.events.extensions.MessageSenderExtension;
3628
import org.apache.webbeans.test.portable.events.extensions.NotAppleExtnsion;
29+
import org.apache.webbeans.test.portable.events.extensions.ParameterizedTypeWithTypeVariableExtension;
3730
import org.apache.webbeans.test.portable.events.extensions.RawTypeExtension;
31+
import org.apache.webbeans.test.portable.events.extensions.ThreeParameterMixedVarianceExtension;
3832
import org.apache.webbeans.test.portable.events.extensions.TreeExtension;
33+
import org.apache.webbeans.test.portable.events.extensions.TwoParameterTypeWithTypeVariableExtension;
3934
import org.apache.webbeans.test.portable.events.extensions.TypeVariableExtension;
4035
import org.apache.webbeans.test.portable.events.extensions.WildcardExtension;
4136
import org.apache.webbeans.test.portable.events.extensions.WrongTypeVariableExtension;
4237
import org.apache.webbeans.test.portable.events.extensions.WrongWildcardExtension;
38+
import org.junit.Assert;
39+
40+
import org.apache.webbeans.test.AbstractUnitTest;
41+
import org.apache.webbeans.test.portable.events.beans.Apple;
42+
import org.apache.webbeans.test.portable.events.beans.AppleTree;
43+
import org.apache.webbeans.test.portable.events.beans.Cherry;
44+
import org.apache.webbeans.test.portable.events.beans.CherryTree;
45+
import org.apache.webbeans.test.portable.events.beans.Tree;
4346
import org.junit.Test;
4447

4548
public class PortableEventTest extends AbstractUnitTest
@@ -260,4 +263,68 @@ public void testNumberCallsGenerics()
260263

261264
shutDownContainer();
262265
}
266+
267+
@Test
268+
public void testParameterizedTypeWithTypeVariableExtension()
269+
{
270+
Collection<String> beanXmls = new ArrayList<String>();
271+
272+
Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
273+
beanClasses.add(ParameterizedTypeWithTypeVariableExtension.PaintToolFactoryImpl.class);
274+
addExtension(new ParameterizedTypeWithTypeVariableExtension());
275+
startContainer(beanClasses, beanXmls);
276+
277+
Assert.assertTrue(ParameterizedTypeWithTypeVariableExtension.CALLED);
278+
279+
shutDownContainer();
280+
}
281+
282+
@Test
283+
public void testTwoParameterTypeWithTypeVariableExtension()
284+
{
285+
Collection<String> beanXmls = new ArrayList<String>();
286+
287+
Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
288+
beanClasses.add(TwoParameterTypeWithTypeVariableExtension.KeyValueStoreImpl.class);
289+
addExtension(new TwoParameterTypeWithTypeVariableExtension());
290+
startContainer(beanClasses, beanXmls);
291+
292+
Assert.assertTrue(TwoParameterTypeWithTypeVariableExtension.CALLED);
293+
294+
shutDownContainer();
295+
}
296+
297+
@Test
298+
public void testThreeParameterMixedVarianceExtension_positive()
299+
{
300+
ThreeParameterMixedVarianceExtension.CALLED = false;
301+
302+
Collection<String> beanXmls = new ArrayList<String>();
303+
304+
Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
305+
beanClasses.add(ThreeParameterMixedVarianceExtension.TripleStoreImpl.class);
306+
addExtension(new ThreeParameterMixedVarianceExtension());
307+
startContainer(beanClasses, beanXmls);
308+
309+
Assert.assertTrue(ThreeParameterMixedVarianceExtension.CALLED);
310+
311+
shutDownContainer();
312+
}
313+
314+
@Test
315+
public void testThreeParameterMixedVarianceExtension_negative()
316+
{
317+
ThreeParameterMixedVarianceExtension.CALLED = false;
318+
319+
Collection<String> beanXmls = new ArrayList<String>();
320+
321+
Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
322+
beanClasses.add(ThreeParameterMixedVarianceExtension.TripleStoreWrongImpl.class);
323+
addExtension(new ThreeParameterMixedVarianceExtension());
324+
startContainer(beanClasses, beanXmls);
325+
326+
Assert.assertFalse(ThreeParameterMixedVarianceExtension.CALLED);
327+
328+
shutDownContainer();
329+
}
263330
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.webbeans.test.portable.events.extensions;
20+
21+
import jakarta.enterprise.event.Observes;
22+
import jakarta.enterprise.inject.spi.Extension;
23+
import jakarta.enterprise.inject.spi.ProcessAnnotatedType;
24+
25+
public class ParameterizedTypeWithTypeVariableExtension implements Extension
26+
{
27+
28+
public static boolean CALLED = false;
29+
30+
<T extends PaintToolFactory<?>> void processClasses(@Observes ProcessAnnotatedType<T> event)
31+
{
32+
CALLED = true;
33+
}
34+
35+
public static class PaintToolFactoryImpl implements PaintToolFactory<PaintBrush<Red>>
36+
{
37+
@Override
38+
public PaintBrush<Red> createPaintTool()
39+
{
40+
return new PaintBrush<Red>();
41+
}
42+
}
43+
44+
public static class PaintBrush<T extends Color> implements PaintTool<T>
45+
{
46+
@Override
47+
public void paint(T color)
48+
{
49+
// no-op
50+
}
51+
}
52+
53+
public interface PaintTool<T extends Color>
54+
{
55+
void paint(T color);
56+
}
57+
58+
public interface PaintToolFactory<T extends PaintTool<?>>
59+
{
60+
T createPaintTool();
61+
}
62+
63+
public interface Color
64+
{
65+
void getColor();
66+
}
67+
68+
public static class Red implements Color {
69+
70+
@Override
71+
public void getColor() {
72+
73+
}
74+
}
75+
76+
public static class Green implements Color {
77+
78+
@Override
79+
public void getColor() {
80+
81+
}
82+
}
83+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.webbeans.test.portable.events.extensions;
20+
21+
import jakarta.enterprise.event.Observes;
22+
import jakarta.enterprise.inject.spi.Extension;
23+
import jakarta.enterprise.inject.spi.ProcessAnnotatedType;
24+
25+
/**
26+
* Three-parameter generic with mixed variance:
27+
* {@code TripleStore<? extends HeadlineFoo, ?, ? extends HeadlineBar>}.
28+
*/
29+
public class ThreeParameterMixedVarianceExtension implements Extension
30+
{
31+
public static boolean CALLED = false;
32+
33+
void processClasses(@Observes ProcessAnnotatedType<TripleStore<? extends HeadlineFoo, ?, ? extends HeadlineBar>> event)
34+
{
35+
CALLED = true;
36+
}
37+
38+
public interface Baz3
39+
{
40+
}
41+
42+
public static class BazImpl3 implements Baz3
43+
{
44+
}
45+
46+
public static class OtherBazImpl3 implements Baz3
47+
{
48+
}
49+
50+
public interface Bar3<T extends Baz3>
51+
{
52+
}
53+
54+
public static class HeadlineBar implements Bar3<BazImpl3>
55+
{
56+
}
57+
58+
public static class OtherBar implements Bar3<OtherBazImpl3>
59+
{
60+
}
61+
62+
public interface Foo3
63+
{
64+
}
65+
66+
public static class HeadlineFoo implements Foo3
67+
{
68+
}
69+
70+
public static class OtherFoo implements Foo3
71+
{
72+
}
73+
74+
public interface TripleStore<A extends Foo3, B, C extends Bar3<?>>
75+
{
76+
void store(A a, B b, C c);
77+
}
78+
79+
/**
80+
* Matches the observer: uses HeadlineFoo and HeadlineBar.
81+
*/
82+
public static class TripleStoreImpl implements TripleStore<HeadlineFoo, String, HeadlineBar>
83+
{
84+
@Override
85+
public void store(HeadlineFoo a, String b, HeadlineBar c)
86+
{
87+
// no-op
88+
}
89+
}
90+
91+
/**
92+
* Does not match the observer: uses OtherBar instead of HeadlineBar.
93+
*/
94+
public static class TripleStoreWrongImpl implements TripleStore<HeadlineFoo, String, OtherBar>
95+
{
96+
@Override
97+
public void store(HeadlineFoo a, String b, OtherBar c)
98+
{
99+
// no-op
100+
}
101+
}
102+
}
103+

0 commit comments

Comments
 (0)