Jakarta ee JEE6:在拦截器实现中获取绑定注释

Jakarta ee JEE6:在拦截器实现中获取绑定注释,jakarta-ee,cdi,interceptor,Jakarta Ee,Cdi,Interceptor,我有一个拦截器绑定,它是参数化的: @InterceptorBinding @Target({ ElementType.METHOD, ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) public @interface Traced { @Nonbinding boolean traceFull() default true; } 然后我定义了一个拦截器实现 @Interceptor @Traced public c

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

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

   @Nonbinding
   boolean traceFull() default true;
}
然后我定义了一个拦截器实现

@Interceptor
@Traced
public class TracingInterceptor implements Serializable { ... }
在实现中,我想检查哪个值被设置为tracevel参数。 (我不想为true、false和null实现三个拦截器实现)

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

Traced traceAnnotation = context.getMethod().getAnnotation(Traced.class);
if (traceAnnotation != null && traceAnnotation.traceFull()) { ... }
这很好,但是如果我使用一个原型或嵌套的拦截器绑定,那么我就不会得到该方法的@tracked注释主题,也无法检查设置的值


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

这在CDI 1.0中不可能,但应该在CDI 1.1中这在CDI 1.0中不可能,但应该在CDI 1.1中您甚至可以使用反射来解析来自原型的注释

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

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;
}
public T findBindingAnnotation(类bindingType,调用上下文ic)
{
Method=ic.getMethod();
if(method!=null&&method.isAnnotationPresent(bindingType))
{
返回方法.getAnnotation(bindingType);
}

类您甚至可以使用反射来解析来自原型的注释

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

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;
}
public T findBindingAnnotation(类bindingType,调用上下文ic)
{
Method=ic.getMethod();
if(method!=null&&method.isAnnotationPresent(bindingType))
{
返回方法.getAnnotation(bindingType);
}

非常感谢你的回答。但是有没有办法让拦截器可以用注释参数化?也许我必须用BeanManager或类似的东西编写一些代码?你可能可以通过java反射从InNotificationContext中获取注释。IIRC,这很多都受到拦截器规范的限制。我使用的是reflectionn要获得它,请在调用上下文上使用getMethod()和getTarget()方法。问题是WELD正在返回getTarget()的包装实例与实例本身相反。如果我的注释是@Inheritated,这不是什么大问题,但如果不是,那就麻烦了。遗憾的是,我没有让它用于构造型,所以不要使用它们。非常感谢你的回答。但是有没有办法使拦截器可以用注释参数化?也许我必须用BeanManager或其他东西编写一些代码像这样吗?您可能可以通过java反射从InNotificationContext.IIRC获取注释,这在很大程度上受到拦截器规范的限制。我使用反射来获取注释,在调用上下文上使用getMethod()和getTarget()方法。问题是WELD返回了getTarget()的包装实例与实例本身相反。如果我的注释是@inheritated的,则不会有太大问题,如果不是,则会有麻烦。遗憾的是,我没有让它用于原型,所以不要使用它们。