This is an automated email from the ASF dual-hosted git repository.
struberg pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git
The following commit(s) were added to refs/heads/main by this push:
new 009d67836 OWB-1443 fix enabled interceptor for custom Interceptor Beans
009d67836 is described below
commit 009d6783668563c4c2f4edeeb4a8b1703bd43190
Author: Mark Struberg <[email protected]>
AuthorDate: Sat Oct 12 17:59:10 2024 +0200
OWB-1443 fix enabled interceptor for custom Interceptor Beans
---
.../webbeans/intercept/InterceptorsManager.java | 1 +
.../intercept/MultipleInterceptedComponent.java | 1 -
.../webbeans/test/instance/InstanceSelectTest.java | 1 -
.../InterceptorDecoratorProxyFactoryTest.java | 1 -
.../interceptorbean/BigBrotherInterceptor.java | 43 ++++++++
.../interceptorbean/BigBrotherInterceptorBean.java | 122 +++++++++++++++++++++
.../interceptors/interceptorbean/BigBrothered.java | 33 ++++++
.../interceptorbean/BigBrotheredExtension.java | 37 +++++++
.../interceptorbean/CustomInterceptorTest.java | 58 ++++++++++
.../webbeans/test/managed/ProxyFactoryTest.java | 6 -
10 files changed, 294 insertions(+), 9 deletions(-)
diff --git
a/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorsManager.java
b/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorsManager.java
index 6b3822c69..a9102f21a 100644
---
a/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorsManager.java
+++
b/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorsManager.java
@@ -199,6 +199,7 @@ public class InterceptorsManager
Asserts.nullCheckForClass(interceptorClazz, "interceptorClazz can not
be null");
return configuredInterceptorClasses.contains(interceptorClazz)
+ || additionalInterceptorClasses.contains(interceptorClazz)
|| priorityInterceptors.contains(interceptorClazz);
}
diff --git
a/webbeans-impl/src/test/java/org/apache/webbeans/test/component/intercept/MultipleInterceptedComponent.java
b/webbeans-impl/src/test/java/org/apache/webbeans/test/component/intercept/MultipleInterceptedComponent.java
index 931251c24..aef3a8ff3 100644
---
a/webbeans-impl/src/test/java/org/apache/webbeans/test/component/intercept/MultipleInterceptedComponent.java
+++
b/webbeans-impl/src/test/java/org/apache/webbeans/test/component/intercept/MultipleInterceptedComponent.java
@@ -19,7 +19,6 @@
package org.apache.webbeans.test.component.intercept;
import java.util.ArrayList;
-import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
diff --git
a/webbeans-impl/src/test/java/org/apache/webbeans/test/instance/InstanceSelectTest.java
b/webbeans-impl/src/test/java/org/apache/webbeans/test/instance/InstanceSelectTest.java
index 868e581cf..2c4004e09 100644
---
a/webbeans-impl/src/test/java/org/apache/webbeans/test/instance/InstanceSelectTest.java
+++
b/webbeans-impl/src/test/java/org/apache/webbeans/test/instance/InstanceSelectTest.java
@@ -30,7 +30,6 @@ import jakarta.inject.Qualifier;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.Collection;
-import java.util.stream.Stream;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
diff --git
a/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/InterceptorDecoratorProxyFactoryTest.java
b/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/InterceptorDecoratorProxyFactoryTest.java
index 6e0cde012..dd6f8d6ff 100644
---
a/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/InterceptorDecoratorProxyFactoryTest.java
+++
b/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/factory/InterceptorDecoratorProxyFactoryTest.java
@@ -28,7 +28,6 @@ import java.lang.reflect.Type;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
-import java.util.Iterator;
import java.util.List;
import java.util.Set;
diff --git
a/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/BigBrotherInterceptor.java
b/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/BigBrotherInterceptor.java
new file mode 100644
index 000000000..784435bad
--- /dev/null
+++
b/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/BigBrotherInterceptor.java
@@ -0,0 +1,43 @@
+/*
+ * 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.interceptors.interceptorbean;
+
+import jakarta.interceptor.InvocationContext;
+
+/**
+ * The actual interceptor for {@link BigBrothered}.
+ * But not connected via annotations but via Extension.
+ */
+public class BigBrotherInterceptor
+{
+ private static boolean observed = false;
+
+ public Object invoke(InvocationContext context) throws Exception
+ {
+ System.out.println("Big Brother is watching you " +
context.getMethod());
+ observed = true;
+
+ return context.proceed();
+ }
+
+ public static boolean isObserved()
+ {
+ boolean wasObserved = observed;
+ observed = false;
+ return wasObserved;
+ }
+}
diff --git
a/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/BigBrotherInterceptorBean.java
b/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/BigBrotherInterceptorBean.java
new file mode 100644
index 000000000..d0b745797
--- /dev/null
+++
b/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/BigBrotherInterceptorBean.java
@@ -0,0 +1,122 @@
+/*
+ * 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.interceptors.interceptorbean;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.Collections;
+import java.util.Set;
+
+import jakarta.enterprise.context.Dependent;
+import jakarta.enterprise.context.spi.CreationalContext;
+import jakarta.enterprise.inject.spi.InjectionPoint;
+import jakarta.enterprise.inject.spi.InterceptionType;
+import jakarta.enterprise.inject.spi.Interceptor;
+import jakarta.enterprise.util.AnnotationLiteral;
+import jakarta.interceptor.InvocationContext;
+import org.apache.webbeans.annotation.DefaultLiteral;
+
+public class BigBrotherInterceptorBean implements
Interceptor<BigBrotherInterceptor>
+{
+ // it's good performance practice to keep the sets static as they are
requested tons of times!
+ public static final Set<Type> TYPES = Set.of(BigBrotherInterceptor.class);
+ public static final Set<Annotation> QUALIFIERS =
Set.of(DefaultLiteral.INSTANCE);
+ public static final Set<Annotation> INTERCEPTOR_BINDINGS = Set.of(new
AnnotationLiteral<BigBrothered>() {});
+
+ public BigBrotherInterceptorBean(int
totallyUselessParamJustToNotHaveADefaultCt)
+ {
+ // all fine ;)
+ }
+
+ @Override
+ public Set<Annotation> getInterceptorBindings()
+ {
+ return INTERCEPTOR_BINDINGS;
+ }
+
+ @Override
+ public boolean intercepts(InterceptionType type)
+ {
+ return InterceptionType.AROUND_INVOKE.equals(type);
+ }
+
+ @Override
+ public Object intercept(InterceptionType type, BigBrotherInterceptor
instance, InvocationContext ctx) throws Exception
+ {
+ return instance.invoke(ctx);
+ }
+
+ @Override
+ public Class<?> getBeanClass()
+ {
+ return BigBrotherInterceptor.class;
+ }
+
+ @Override
+ public Set<InjectionPoint> getInjectionPoints()
+ {
+ return Collections.emptySet();
+ }
+
+ @Override
+ public BigBrotherInterceptor
create(CreationalContext<BigBrotherInterceptor> creationalContext)
+ {
+ return new BigBrotherInterceptor();
+ }
+
+ @Override
+ public void destroy(BigBrotherInterceptor instance,
CreationalContext<BigBrotherInterceptor> creationalContext)
+ {
+ // no op
+ }
+
+ @Override
+ public Set<Type> getTypes()
+ {
+ return TYPES;
+ }
+
+ @Override
+ public Set<Annotation> getQualifiers()
+ {
+ return QUALIFIERS;
+ }
+
+ @Override
+ public Class<? extends Annotation> getScope()
+ {
+ return Dependent.class;
+ }
+
+ @Override
+ public String getName()
+ {
+ return null;
+ }
+
+ @Override
+ public Set<Class<? extends Annotation>> getStereotypes()
+ {
+ return Collections.emptySet();
+ }
+
+ @Override
+ public boolean isAlternative()
+ {
+ return false;
+ }
+}
diff --git
a/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/BigBrothered.java
b/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/BigBrothered.java
new file mode 100644
index 000000000..593ded7ea
--- /dev/null
+++
b/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/BigBrothered.java
@@ -0,0 +1,33 @@
+/*
+ * 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.interceptors.interceptorbean;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Test interceptor annotation to print 'big brother is watching you' for
every method invocation :)
+ *
+ * Note that this is not an InterceptorBinding as we do register the interface
via {@link BigBrotheredExtension}.
+ */
+@Target({ElementType.METHOD, ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface BigBrothered
+{
+}
diff --git
a/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/BigBrotheredExtension.java
b/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/BigBrotheredExtension.java
new file mode 100644
index 000000000..0488be865
--- /dev/null
+++
b/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/BigBrotheredExtension.java
@@ -0,0 +1,37 @@
+/*
+ * 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.interceptors.interceptorbean;
+
+import jakarta.enterprise.event.Observes;
+import jakarta.enterprise.inject.spi.AfterBeanDiscovery;
+import jakarta.enterprise.inject.spi.BeforeBeanDiscovery;
+import jakarta.enterprise.inject.spi.Extension;
+
+/**
+ * Extension to register the BigBrotherInterceptor and friends.
+ */
+public class BigBrotheredExtension implements Extension
+{
+ public void registerInterceptorBinding(@Observes BeforeBeanDiscovery
beforeBeanDiscovery) {
+ beforeBeanDiscovery.addInterceptorBinding(BigBrothered.class);
+ }
+
+ public void registerInterceptor(@Observes AfterBeanDiscovery
afterBeanDiscovery) {
+ afterBeanDiscovery.addBean(new BigBrotherInterceptorBean(42));
+ }
+
+}
diff --git
a/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/CustomInterceptorTest.java
b/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/CustomInterceptorTest.java
new file mode 100644
index 000000000..520a447f2
--- /dev/null
+++
b/webbeans-impl/src/test/java/org/apache/webbeans/test/interceptors/interceptorbean/CustomInterceptorTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.interceptors.interceptorbean;
+
+import jakarta.enterprise.context.ApplicationScoped;
+import org.apache.webbeans.test.AbstractUnitTest;
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test for programmatically added Interceptors
+ */
+public class CustomInterceptorTest extends AbstractUnitTest
+{
+ @Test
+ public void testCustomInterceptor() throws Exception
+ {
+ BigBrotheredExtension bbe = new BigBrotheredExtension();
+ addExtension(bbe);
+ startContainer(LittleJohnDoe.class);
+
+ final LittleJohnDoe joe = getInstance(LittleJohnDoe.class);
+
+ joe.makeACoffee();
+ assertTrue(BigBrotherInterceptor.isObserved());
+
+ joe.watchTv();
+ assertTrue(BigBrotherInterceptor.isObserved());
+ }
+
+ @ApplicationScoped
+ @BigBrothered
+ public static class LittleJohnDoe
+ {
+ public void makeACoffee() {
+ System.out.println("Making a coffee");
+ }
+
+ public void watchTv() {
+ System.out.println("Watching TV");
+ }
+ }
+}
diff --git
a/webbeans-impl/src/test/java/org/apache/webbeans/test/managed/ProxyFactoryTest.java
b/webbeans-impl/src/test/java/org/apache/webbeans/test/managed/ProxyFactoryTest.java
index 407aed1d7..61ea567a3 100644
---
a/webbeans-impl/src/test/java/org/apache/webbeans/test/managed/ProxyFactoryTest.java
+++
b/webbeans-impl/src/test/java/org/apache/webbeans/test/managed/ProxyFactoryTest.java
@@ -20,7 +20,6 @@
package org.apache.webbeans.test.managed;
import java.lang.annotation.ElementType;
-import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@@ -34,19 +33,14 @@ import java.util.concurrent.atomic.AtomicReference;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.Produces;
-import jakarta.enterprise.inject.Typed;
-import jakarta.enterprise.inject.literal.NamedLiteral;
import jakarta.enterprise.inject.spi.Bean;
-import jakarta.enterprise.inject.spi.Extension;
import jakarta.enterprise.inject.spi.InterceptionFactory;
import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.inject.Named;
-import jakarta.inject.Qualifier;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InterceptorBinding;
import jakarta.interceptor.InvocationContext;
-import org.apache.webbeans.test.discovery.InterceptorAnnotatedDiscoveryTest;
import org.junit.Assert;
import org.apache.webbeans.config.WebBeansContext;