JEE6:在拦截器实现中获取 Binding 注释

发布于 2024-12-28 20:07:43 字数 768 浏览 3 评论 0原文

我有一个拦截器绑定,它是参数化的:

@InterceptorBinding
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Traced {

   @Nonbinding
   boolean traceFull() default true;
}

然后我定义一个拦截器实现

@Interceptor
@Traced
public class TracingInterceptor implements Serializable { ... }

在实现中我想检查哪个值设置为traceFull参数。 (我不想实现 true、false 和 null 的三个拦截器实现)

因此,我的实现会检查被拦截方法的拦截器绑定注释:

Traced traceAnnotation = context.getMethod().getAnnotation(Traced.class);
if (traceAnnotation != null && traceAnnotation.traceFull()) { ... }

这很好用,但如果我使用 Stereotype 或嵌套拦截器绑定,那么我不会t 获取该方法的 @Traced annotatopn,我无法检查设置的值。

所以我的问题是:如何在拦截器实现中获得“调用”绑定?

I have an interceptor binding, which is parameterized:

@InterceptorBinding
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Traced {

   @Nonbinding
   boolean traceFull() default true;
}

Then I define an interceptor implementation

@Interceptor
@Traced
public class TracingInterceptor implements Serializable { ... }

In the implementation I want to check which value is set to the traceFull-parameter.
(I do not want to implement three interceptor implementations for true, false and null)

So my implementation checks the Interceptor-Binding-Annotation of the intercepted method:

Traced traceAnnotation = context.getMethod().getAnnotation(Traced.class);
if (traceAnnotation != null && traceAnnotation.traceFull()) { ... }

This works fine but if I use a Stereotype or a nested interceptor binding then I don't get the @Traced annotatopn of the method and I cannot check the value which is set.

So my question is: How can I get the 'calling' binding within my interceptor implementation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖哒 2025-01-04 20:07:43

您甚至可以使用反射来解析构造型中的注释。

在同样的情况下(需要拦截器中的附加非绑定信息),我实现了一个实用方法:

public <T extends Annotation> T findBindingAnnotation( Class<T> bindingType, InvocationContext ic )
{
    Method method = ic.getMethod();
    if ( method != null && method.isAnnotationPresent( bindingType ) )
    {
        return method.getAnnotation( bindingType );
    }
    Class<? extends Object> type = ic.getTarget().getClass();
    if ( type.isAnnotationPresent( bindingType ) )
    {
        return type.getAnnotation( bindingType );
    }

    T annotationFromStereoType = annotationFromStereoType( bindingType, type );
    if ( annotationFromStereoType != null )
    {
        return annotationFromStereoType;
    }

    throw new UnsupportedOperationException( "no binding annotation found: " + bindingType.getCanonicalName() );
}

private <T extends Annotation> T annotationFromStereoType( Class<T> bindingType, Class<? extends Object> type )
{
    for ( Annotation annotation : type.getAnnotations() )
    {
        Class<? extends Annotation> annotationType = annotation.annotationType();
        if ( annotationType.isAnnotationPresent( Stereotype.class ) )
        {
            if ( annotationType.isAnnotationPresent( bindingType ) )
            {
                return annotationType.getAnnotation( bindingType );
            }
            else
            {
                T recursiveLookup = annotationFromStereoType( bindingType, annotationType );
                if ( null != recursiveLookup )
                {
                    return recursiveLookup;
                }
            }
        }
    }
    return null;
}

You can use reflection even for resolving the annotation from stereotypes.

Being in the same situation (requiring additional nonbinding information in the interceptor), I have implemented a utility method:

public <T extends Annotation> T findBindingAnnotation( Class<T> bindingType, InvocationContext ic )
{
    Method method = ic.getMethod();
    if ( method != null && method.isAnnotationPresent( bindingType ) )
    {
        return method.getAnnotation( bindingType );
    }
    Class<? extends Object> type = ic.getTarget().getClass();
    if ( type.isAnnotationPresent( bindingType ) )
    {
        return type.getAnnotation( bindingType );
    }

    T annotationFromStereoType = annotationFromStereoType( bindingType, type );
    if ( annotationFromStereoType != null )
    {
        return annotationFromStereoType;
    }

    throw new UnsupportedOperationException( "no binding annotation found: " + bindingType.getCanonicalName() );
}

private <T extends Annotation> T annotationFromStereoType( Class<T> bindingType, Class<? extends Object> type )
{
    for ( Annotation annotation : type.getAnnotations() )
    {
        Class<? extends Annotation> annotationType = annotation.annotationType();
        if ( annotationType.isAnnotationPresent( Stereotype.class ) )
        {
            if ( annotationType.isAnnotationPresent( bindingType ) )
            {
                return annotationType.getAnnotation( bindingType );
            }
            else
            {
                T recursiveLookup = annotationFromStereoType( bindingType, annotationType );
                if ( null != recursiveLookup )
                {
                    return recursiveLookup;
                }
            }
        }
    }
    return null;
}
亣腦蒛氧 2025-01-04 20:07:43

这在 CDI 1.0 中不可能,但在 CDI 1.1 中应该可以

This is not possible in CDI 1.0, but should be in CDI 1.1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文