Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could use <a href="http://docs.jboss.org/weld/reference/1.0.0/en-US/html/interceptors.html" rel="nofollow">CDI's interceptor annotations</a> and do something like:</p> <pre><code>... @Validated public String sayHello(@Validator(MyValidator.class) String name) { ... </code></pre> <p>Where <code>@Validated</code> binds the method to a <code>ValidationInterceptor</code> class or so, where you use an <code>@AroundInvoke</code> method and its <code>InvocationContext</code> parameter to inspect the passed parameters and validate them.</p> <p>One "difficulty" with this approach is that you will have to use reflection to get the <code>@Validator</code> annotation and specified validator class for each parameter, then create/retrieve corresponding instances before you can finally do the actual validation.</p> <p>A slightly different approach would be to inject an <code>Instance&lt;T&gt;</code> of a common superclass/interface of your validators into the <code>ValidationInterceptor</code> and annotate the <code>sayHello</code> parameters with validator qualifiers:</p> <pre><code>// in ValidationInterceptor: @Inject private Instance&lt;Validator&gt; validatorInstance; @AroundInvoke public Object validate(InvocationContext context) { // get Annotation instances for target method parameters // ... Validator validator = validatorInstance.select(annotations).get(); // ... validator.validate(parameter); ... } // sayHello: ... @Validated public String sayHello(@NameValidator String name) { ... </code></pre> <p>To get parameter annotations from <code>InvocationContext</code>, do something like:</p> <pre><code>Annotation[][] annotations = context.getMethod().getParameterAnnotations(); </code></pre> <p>You may also consider pre-processing these annotations in an <code>@AfterBeanDiscovery</code> event handler.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload