From 21fcc0f4810bc3cc25c6447bcc7755917e4b7856 Mon Sep 17 00:00:00 2001 From: Matej Novotny Date: Wed, 8 Jan 2025 15:02:06 +0100 Subject: [PATCH] WELD-2806 Prevent compiled lambdas in class from being processed as interceptable methods. --- impl/src/main/java/org/jboss/weld/util/Beans.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/impl/src/main/java/org/jboss/weld/util/Beans.java b/impl/src/main/java/org/jboss/weld/util/Beans.java index 253761668e..ca97cc0a12 100644 --- a/impl/src/main/java/org/jboss/weld/util/Beans.java +++ b/impl/src/main/java/org/jboss/weld/util/Beans.java @@ -20,6 +20,7 @@ import java.lang.annotation.Annotation; import java.lang.annotation.Repeatable; import java.lang.reflect.GenericArrayType; +import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; @@ -169,7 +170,7 @@ public static boolean isBeanProxyable(Bean bean, BeanManagerImpl manager) { // note that a bridge method can be a candidate for interception in rare cases; do not discard those boolean businessMethod = !annotatedMethod.isStatic() && !annotatedMethod.isAnnotationPresent(Inject.class); - if (businessMethod && !isInterceptorMethod(annotatedMethod)) { + if (businessMethod && !isInterceptorMethod(annotatedMethod) && !isLambdaMethod(annotatedMethod)) { annotatedMethods.add(annotatedMethod); } } @@ -185,6 +186,14 @@ private static boolean isInterceptorMethod(AnnotatedMethod annotatedMethod) { return false; } + // Lambdas in classes will be compiled into methods declared on that given class. + // The exact representation differs between javac (standard compiler) and EJC (Eclipse compiler). + // We want to avoid attempting to intercept these methods - note that the detection is on a "best effort" basis. + private static boolean isLambdaMethod(AnnotatedMethod annotatedMethod) { + Method javaMember = annotatedMethod.getJavaMember(); + return !javaMember.isBridge() && javaMember.isSynthetic(); + } + /** * Checks that all the qualifiers in the set requiredQualifiers are in the set of qualifiers. Qualifier equality rules for * annotation members are followed.