[JBoss Microcontainer Development] - Re: Adding getAnnotationsAnnotatedWith() to MDR
by kabir.khan@jboss.com
Some more changes to AbstractMDL to avoid having to iterate over the contexts every time
| ===================================================================
| --- src/main/java/org/jboss/metadata/plugins/loader/AbstractMetaDataLoader.java (revision 97282)
| +++ src/main/java/org/jboss/metadata/plugins/loader/AbstractMetaDataLoader.java (working copy)
| @@ -22,6 +22,8 @@
| package org.jboss.metadata.plugins.loader;
|
| import java.lang.annotation.Annotation;
| +import java.util.ArrayList;
| +import java.util.List;
|
| import org.jboss.metadata.spi.loader.MetaDataLoader;
| import org.jboss.metadata.spi.retrieval.AnnotationItem;
| @@ -31,6 +33,7 @@
| import org.jboss.metadata.spi.retrieval.MetaDataRetrieval;
| import org.jboss.metadata.spi.retrieval.MetaDatasItem;
| import org.jboss.metadata.spi.retrieval.ValidTime;
| +import org.jboss.metadata.spi.retrieval.basic.BasicAnnotationsItem;
| import org.jboss.metadata.spi.retrieval.helper.AnnotationToMetaDataBridge;
| import org.jboss.metadata.spi.retrieval.helper.AnnotationsToMetaDatasBridge;
| import org.jboss.metadata.spi.scope.ScopeKey;
| @@ -54,6 +57,8 @@
| /** The scope key */
| private ScopeKey scopeKey;
|
| + private volatile AnnotationsAnnotatedWithCache cache;
| +
| /**
| * Create a new AbstractMetaDataLoader.
| */
| @@ -92,7 +97,47 @@
| {
| return retrieveAnnotations();
| }
| +
| + protected AnnotationsAnnotatedWithCache getAnnotationsAnnotatedWithCache()
| + {
| + if (cache == null)
| + {
| + synchronized (this)
| + {
| + if (cache == null)
| + {
| + cache = new AnnotationsAnnotatedWithCache();
| + }
| + }
| + }
| + return cache;
| + }
|
| + public AnnotationsItem retrieveAnnotationsAnnotatedWith(Class<? extends Annotation> meta)
| + {
| + AnnotationsItem item = getAnnotationsAnnotatedWithCache().getAnnotationsAnnotatedWith(meta);
| + if (item == null)
| + {
| + AnnotationsItem annotations = retrieveAnnotations();
| + List<AnnotationItem<? extends Annotation>> values = new ArrayList<AnnotationItem<? extends Annotation>>(annotations.getAnnotations().length);
| + for (AnnotationItem<? extends Annotation> current : annotations.getAnnotations())
| + {
| + for (Annotation ann : current.getAnnotation().annotationType().getAnnotations())
| + {
| + if (meta == ann.annotationType())
| + {
| + values.add(current);
| + break;
| + }
| + }
| + }
| + item = new BasicAnnotationsItem(this, values.toArray(new AnnotationItem[values.size()]));
| + cache.addAnnotationsAnnotatedWith(meta, item);
| + }
| + return item;
| + }
| +
| +
| @SuppressWarnings("unchecked")
| public <T> MetaDataItem<T> retrieveMetaData(Class<T> type)
| {
| @@ -141,5 +186,12 @@
| public void invalidate()
| {
| validTime.invalidate();
| + invalidateAnnotationsAnnotatedWithCache();
| }
| +
| + protected void invalidateAnnotationsAnnotatedWithCache()
| + {
| + if (cache != null)
| + cache.invalidate();
| + }
| }
|
The cache is basically just a wrapper around a map
| public class AnnotationsAnnotatedWithCache
| {
| private ConcurrentMap<Class<? extends Annotation>, AnnotationsItem> annotationsByMetaAnnotation = new ConcurrentHashMap<Class<? extends Annotation>, AnnotationsItem>();
|
| public AnnotationsItem getAnnotationsAnnotatedWith(Class<? extends Annotation> meta)
| {
| AnnotationsItem annotationsItem = annotationsByMetaAnnotation.get(meta);
| if (annotationsItem != null)
| return annotationsItem;
| return null;
| }
|
| public void addAnnotationsAnnotatedWith(Class<? extends Annotation> meta, AnnotationsItem item)
| {
| annotationsByMetaAnnotation.putIfAbsent(meta, item);
| }
|
| public void invalidate()
| {
| annotationsByMetaAnnotation.clear();
| }
| }
|
AMDL.invalidateCache() gets called when annotations are added/removed from MemoryMetaDataLoader, which seems to be the only AMDL subclass that changes the annotations
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4269050#4269050
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4269050
16 years, 4 months
[Embedded JBoss Development] - Re: Embedded API Design Problems
by david.lloyd@jboss.com
"richard.opalka(a)jboss.com" wrote : "jason.greene(a)jboss.com" wrote : I disagree with the server factory example in Rule 4. IllegalArgumentException is the appropriate exception to be thrown
| |
| Of course you're right Jason, the correct code should be:
|
Again, I can't agree with you. You're abiding by a set of subjective criteria as though they were laws. :-) Rather than saying "this is incorrect" what you really mean is, "this is not how I like it, but that's just my opinion".
The true test is, is the API nice to use? Does it make sense logically? If these two are true, then the API is probably just fine. Everyone has their own opinion about how APIs should look and feel, and I can tell you now that there will never be a time where everyone converges on what your (or my) idea of "right" is.
So I think a better measurement is, rather than saying "X, Y and Z violate my rules for APIs", look at what code which actually USES the API might look like and ask yourself, "can this cause a surprise for users? Is it unintuitive? Is there a way this could be made better with that in mind?" If the answer is something like "Yes, the user will need 5 probably-identical catch clauses every time they call method X, and they're all very unlikely!" then you can objectively say it's a problem. If the answer is "I don't like ____" then that's just your opinion and you should advertise it thus.
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4269046#4269046
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4269046
16 years, 4 months
[JBoss Microcontainer Development] - Adding getAnnotationsAnnotatedWith() to MDR
by kabir.khan@jboss.com
As mentioned here
http://www.jboss.org/index.html?module=bb&op=viewtopic&t=163887&start=10
"alesj" wrote : "kabir.khan(a)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.
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4269025#4269025
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4269025
16 years, 4 months