<div class="gmail_quote">On Wed, Apr 14, 2010 at 8:34 AM, Pete Muir <span dir="ltr"><<a href="mailto:pmuir@redhat.com">pmuir@redhat.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I think we need to take a step back and establish a couple of things:<br>
<br>
1) are all the annotations we have suitable to be meta-annotations?<br>
2) should it be possible to use meta-annotations anywhere, or just in certain places (on stereotypes)<br>
3) should these annotations be usable via seam-xml?<br>
<br>
IMO the answers are<br>
<br>
1) No. Not all annotations are suitable as meta-annotations. This is a principle clearly established by the CDI spec (which doesn't allow qualifiers etc. as meta-annotations). We should consider whether each annotation is suitable to be a meta-annotation or not.<br>
</blockquote><div><br></div><div>+1 Constraint breed consistency.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
2) In general no, only on stereotypes. There are a few reasons. In general, stereotypes were designed for exactly the purpose of stereotyping an action. The overhead on the user is minimal (I believe the concern that users will get confused by their annotation not working can be solved by tooling). Finally, if stereotypes are reused, the CDI container is capable of caching the recursive scan of the annotation hierarchy (which no extension can do as it doesn't know the lifecycle of the underlying class), and also applying any metadata specified through the CDI SPI (e.g. XML config) to the stereotype (something which is not required in other cases). This means the complexity of the checking the annotations is reduced to N (rather than the N^N we get with a recursive scan).<br>
<br>
IOW, taking the JMS case, it would be necessary to specify:<br>
<br>
@Stereotype<br>
@Retention(RUNTIME)<br>
@Target({ PARAMETER, METHOD, FIELD })<br>
@JmsDestination(jndiName = "/jms/my-topic")<br>
public @interface MyTopic {}<br></blockquote><div><br></div><div>I think that's very fair. It certainly helps clarify the purpose.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
3) Yes. This reduces the potential for user confusion, and makes it much more powerful.<br>
<br>
So, I would propose we create a new API in WeldExtensions that looks like:<br>
<br>
/**<br>
* Discover if a AnnotatedElement <b>element</b> has been annotated with <b>annotationType</b>. This<br>
* also discovers annotations defined through a @{@link Stereotype} and the CDI SPI.<br>
*<br>
* @param element The element to inspect.<br>
* @param annotationType<br>
* @param metaAnnotation Whether the annotation may be used as a meta-annotation or not<br>
*<br>
* @return true if annotation is present either on the method itself. Returns false if the annotation<br>
* is not present<br>
* @throws IllegalArgumentException if element or annotationType is null<br>
*/<br>
public static boolean isAnnotationPresent(AnnotatedElement element, Class<? extends Annotation> annotationType, boolean metaAnnotation);<br>
<br>
/**<br>
* Inspect AnnoatedElement <b>element</b> for a specific <b>type</b> of annotation. This<br>
* also discovers annotations defined through a @ {@link Stereotype} and the CDI SPI.<br>
*<br>
* @param element The element to inspect<br>
* @param annotationType The annotation type to check for<br>
* @param metaAnnotation Whether the annotation may be used as a meta-annotation or not<br>
*<br>
* @return The annotation instance found on this method or null if no matching annotation was found.<br>
* @throws IllegalArgumentException if element or annotationType is null<br>
*/<br>
public static <A extends Annotation> A getAnnotation(AnnotatedElement element, final Class<A> annotationType, boolean metaAnnotation)<br>
<br>
This is somewhat different to Lincoln's original proposal (adjusted to add the meta-annotation stuff, aligned with the Java Reflection API naming conventions, removed the stuff about whether the annotation is on the declaring class as well - if this is really needed we can add a boolean flag for this I guess).<br>
<br>
Adam also raised the need to be able to inspect the annotations on an annotation element, which indicates we need another utility method:<br>
<br>
/**<br>
* Inspect AnnoatedElement <b>element</b> for a specific <b>type</b> of annotation. This<br>
* also discovers annotations defined through a @ {@link Stereotype} and the CDI SPI. Having found<br>
* the annotation, it is inspected for a member with name <b>memberName</b>.<br>
*<br>
* @param element The element to inspect<br>
* @param annotationType The annotation type to check for<br>
* @param memberName The name of the member to look for<br>
* @param expectedMemberType The expectedType of the member<br>
* @param metaAnnotation Whether the annotation may be used as a meta-annotation or not<br>
*<br>
* @return The annotation member's value or null if no matching annotation was found<br>
* @throws IllegalArgumentException if element, annotationType, memberName or expectedMemberType is null<br>
* @throws IllegalArgumentException if the annotationType does not have a member of memberName<br>
* @throws ClassCastException if the value of memberName cannot be cast to expectedMemberType<br>
*/<br>
public static <V> V getAnnotationMemberValue(AnnotatedElement element, final Class<A> annotationType, String memberName, Class<V> expectedMemberType, boolean metaAnnotation)<br>
<br>
WDYT?</blockquote><div><br></div><div>I'm not sure I understand why we need a separate method to get an annotation member value. It requires specifying a string name. Why can't we simply return the annotation instance and then allow the value to be read by invoking the member method?</div>
<div><br></div><div>Consider</div><div><br></div><div>public @interface Begin {</div><div> String id default "";</div><div>}</div><div><br></div><div>annotations.get(annotatedElement, Begin.class, true).id(); </div>
<div><br></div><div>I'll admit I was excited about the more fluent method names that Lincoln had in his code. Do we really need to have these long method names? Seems like pomp and circumstance to me. I suppose that this is what Java has always been about, so why take the garb off the queen? Boo.</div>
<div><br></div><div>-Dan</div></div><br>-- <br>Dan Allen<br>Senior Software Engineer, Red Hat | Author of Seam in Action<br>Registered Linux User #231597<br><br><a href="http://mojavelinux.com">http://mojavelinux.com</a><br>
<a href="http://mojavelinux.com/seaminaction">http://mojavelinux.com/seaminaction</a><br><a href="http://www.google.com/profiles/dan.j.allen">http://www.google.com/profiles/dan.j.allen</a><br>