Adding getAnnotationsAnnotatedWith() to MDR
kabirkhan Dec 4, 2009 9:32 AMAs mentioned here
http://www.jboss.org/index.html?module=bb&op=viewtopic&t=163887&start=10
"alesj" wrote:"kabir.khan@jboss.com" wrote:
Since most qualifiers I've seen (in jsr-299 and jsr-330) are picked out using annotations on the annotation, I propose adding something to BeanAnnotationAdapter to handle meta-annotations.
I would put this support directly to Reflect or MDR
- which ever suites best, but probably both will have to adapt.
BAA should just use this feature, not implement it.
I've added this to MetaData:
/** * Get all the annotations annotated with the given meta annotation * * @param the meta annotation * @return the annotations annotated with the meta annotation */ Annotation[] getAnnotationsAnnotatedWith(Class<? extends Annotation> meta);
and this to MetaDataRetrieval
/** * Get all the annotations annotated with the given meta annotation * * @param the meta annotation * @return the annotations annotated with the meta annotation */ AnnotationsItem retrieveAnnotationsAnnotatedWith(Class<? extends Annotation> meta);
The implemetation of this in AbstractMetaDataLoader
public AnnotationsItem retrieveAnnotationsAnnotatedWith(Class<? extends Annotation> meta) { AnnotationsItem annotations = retrieveAnnotations(); List<AnnotationItem<? extends Annotation>> values = new ArrayList<AnnotationItem<? extends Annotation>>(annotations.getAnnotations().length); for (AnnotationItem<? extends Annotation> item : annotations.getAnnotations()) { for (Annotation ann : item.getAnnotation().annotationType().getAnnotations()) { if (meta == ann.annotationType()) { values.add(item); break; } } } return new BasicAnnotationsItem(this, values.toArray(new AnnotationItem[values.size()])); }
And then the implemetation in AbstractMetaDataContext
public AnnotationsItem retrieveAnnotationsAnnotatedWith(Class<? extends Annotation> meta) { return new CummulativeAnnotationsItem(this, true, new AnnotationsAnnotatedWithFilter(meta)); } private static class AnnotationsAnnotatedWithFilter implements CummulativeAnnotationsFilter { Class<? extends Annotation> meta; public AnnotationsAnnotatedWithFilter(Class<? extends Annotation> meta) { this.meta = meta; } public AnnotationsItem getAnnotations(MetaDataRetrieval retrieval) { return retrieval.retrieveAnnotationsAnnotatedWith(meta); } }
Changes to CummulativeAnnotationsItem
Index: src/main/java/org/jboss/metadata/spi/retrieval/cummulative/CummulativeAnnotationsItem.java =================================================================== --- src/main/java/org/jboss/metadata/spi/retrieval/cummulative/CummulativeAnnotationsItem.java (revision 97282) +++ src/main/java/org/jboss/metadata/spi/retrieval/cummulative/CummulativeAnnotationsItem.java (working copy) @@ -49,6 +49,8 @@ /** The valid time */ private long validTime; + private CummulativeAnnotationsFilter filter = AllAnnotationsFilter.INSTANCE; + /** * Create a new CummulativeAnnotationsItem. * @@ -57,11 +59,25 @@ */ public CummulativeAnnotationsItem(MetaDataContext context, boolean includeParent) { + this(context, includeParent, null); + } + + /** + * Create a new CummulativeAnnotationsItem. + * + * @param context the context + * @param includeParent whether to include the parent + */ + public CummulativeAnnotationsItem(MetaDataContext context, boolean includeParent, CummulativeAnnotationsFilter filter) + { if (context == null) throw new IllegalArgumentException("Null context"); this.context = context; this.includeParent = includeParent; + if (filter != null) + this.filter = filter; + init(context.getValidTime().getValidTime()); } @@ -131,7 +147,7 @@ for (MetaDataRetrieval retrieval : retrievals) { - AnnotationsItem item = retrieval.retrieveAnnotations(); + AnnotationsItem item = filter.getAnnotations(retrieval); if (item != null) { AnnotationItem<? extends Annotation>[] items = item.getAnnotations(); @@ -150,4 +166,14 @@ setAnnotationItems(items); this.validTime = validTime; } + + private static class AllAnnotationsFilter implements CummulativeAnnotationsFilter + { + private static final CummulativeAnnotationsFilter INSTANCE = new AllAnnotationsFilter(); + + public AnnotationsItem getAnnotations(MetaDataRetrieval retrieval) + { + return retrieval.retrieveAnnotations(); + } + } }
I am currently reworking the current tests to also check this new functionality. One issue I have not yet looked into is the caching of this information, which is next on my list.