Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>IMHO one could think about the Visitor pattern in combination with a factory. The factory will return a wrapper object that knows the exact annotation type and which the visitor will be able...</p> <pre><code>class MyVisitor { public void visit(VisitableAnnotationType1 at) { //something AnnotationType1 specific } public void visit(VisitableAnnotationType2 at) { //something AnnotationType2 specific } ... // put methods for further annotation types here } class VisitableFactory { public abstract class VisitableAnnotation { public abstract void accept(MyVisitor visitor); } class VisitableAnnotationType1 implements VisitableAnnotation { public void accept(MyVisitor visitor) { visitor.visit(this); } } public static VisitableAnnotation getVisitable(Annotation a) { if(AnnotationType1.class.isAssignableFrom(a.getClass()) { //explicitely cast to the respective AnnotationType return new VisitableAnnotationType1((AnnotationType1)a); } else if (AnnotationType2.class.isAssignableFrom(a.getClass()) { //explicitely cast to the respective AnnotationType return new VisitableAnnotationType1((AnnotationType1)a); } } } </code></pre> <p>As we cannot extend Annotation, we need those wrapper classes in the factory. You could also pass the original annotation which is then contained in that wrapper class.</p> <p>What you have to do: For each new AnnotationType add a new "wrapper" class to the factory, extend the factory's</p> <pre><code>getVisitable() </code></pre> <p>method accordingly and also add an according method to the Visitor:</p> <pre><code>public void doSomething(VisitableAnnotationTypeXYZ at) { //something AnnotationTypeXYZ specific } </code></pre> <p>now the generic validation (or whatever) code looks like:</p> <pre><code>List&lt;ValidatableAnnotation&gt; annotations = mergeConstraintsFromClassAndMethod(serviceClass, serviceMethod); MyVisitor visitor = new MyVisitor(); for (ValidatableAnnotation annotation : annotations) { VisitableFactory.getVisitable(annotation).accept(visitor); } </code></pre> <p>The visiting works by the indirection that the visited object calls the visitor with itself as the argument and thus the correct visit method will be invoked. Hope that helps ;-) Code is not tested, though...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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