I'm not a maintainer on those projects so I defer on applying the following
patches.  However, I think that it is a bug.  If it is a bug, it will also
require a change in the annotations project in WicketStuff.  I have created
a patch for each.  Could you test it and see if it works?  If so, I'll go
ahead and commit it (unless one of the maintainers objects here first).

Patch for annotations: http://pastebin.com/nxQuLfqf
<http://pastebin.com/nxQuLfqf>Patch for merged-resources:
http://pastebin.com/Cbpjtvqp

<http://pastebin.com/Cbpjtvqp>File attachments to the list will be stripped.

--
Jeremy Thomerson
http://www.wickettraining.com



On Mon, May 17, 2010 at 9:43 PM, Ryan Crumley <crum...@gmail.com> wrote:

> All,
>
> [If there is a wicketstuff specific mailing list please let me know
> and I will be happy to post there (wicketstuff.org is down so its
> difficult to find information).]
>
> I am using wicketstuff-merged-resources (3.1-SNAPSHOT) with
> wicketstuff-annotation (1.1) and I have found a potential issue with
> annotation detection. My page hierarchy looks something like this:
>
> public abstract class BasePage extends WebPage { ... }
>
> public class HomePage extends BasePage { ... }
>
> I would like to add @JsContribution to BasePage however the resources
> are only included if the concrete page (in this example: 'HomePage')
> also has resource annotations. Removing @JsContribution from HomePage
> results in all resources from BasePage also disappearing. Is this a
> known bug? Am I using the framework incorrectly?
>
> Thanks in advance for any guidance.
>
> Ryan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>
Index: annotation/src/main/java/org/wicketstuff/config/MatchingResources.java
===================================================================
--- annotation/src/main/java/org/wicketstuff/config/MatchingResources.java	(revision 5264)
+++ annotation/src/main/java/org/wicketstuff/config/MatchingResources.java	(working copy)
@@ -86,12 +86,14 @@
         return urls;
     }
 
+
     /**
      * Get all matching classes that are annotated with the given Annotation.
      * @param annotation an annotation class
+     * @param includeSubclasses if true, this will also return classes whose superclass has the specified annotation
      * @return List of all classes that have the given annotation.  List is empty if non matches found.
      */
-    public List<Class<?>> getAnnotatedMatches(Class<? extends Annotation> annotation)
+    public List<Class<?>> getAnnotatedMatches(Class<? extends Annotation> annotation, boolean includeSubclasses) 
     {
         List<Class<?>> matches = new ArrayList<Class<?>>();
         MetadataReaderFactory f = new SimpleMetadataReaderFactory();
@@ -108,22 +110,45 @@
             }
             AnnotationMetadata anno = meta.getAnnotationMetadata();
             Set<String> types = anno.getAnnotationTypes();
-            if (types.contains(annotation.getName()))
+            if (types.contains(annotation.getName()) || (includeSubclasses && anySuperHas(getClass(anno.getSuperClassName()), annotation)))
             {
-                try
-                {
-                    matches.add(Class.forName(anno.getClassName()));
-                }
-                catch (ClassNotFoundException e)
-                {
-                    throw new RuntimeException(e);
-                }
+                matches.add(getClass(anno.getClassName()));
             }
         }
         return matches;
     }
 
     /**
+     * Get all matching classes that are annotated with the given Annotation.  Note that this method only returns
+     * those classes that actually contain the annotation, and does not return classes whose superclass contains 
+     * the annotation.
+     *  
+     * @param annotation an annotation class
+     * @return List of all classes that have the given annotation.  List is empty if non matches found.
+     * @see MatchingResources#getAnnotatedMatches(Class, boolean)
+     */
+    public List<Class<?>> getAnnotatedMatches(Class<? extends Annotation> annotation)
+    {
+        return getAnnotatedMatches(annotation, false);
+    }
+
+    private boolean anySuperHas(Class<?> clz, Class<? extends Annotation> annotation) 
+    {
+        return clz != null && (clz.isAnnotationPresent(annotation) || anySuperHas(clz.getSuperclass(), annotation));
+    }
+
+    private Class<?> getClass(String className) {
+        try
+        {
+            return Class.forName(className);
+        }
+        catch (ClassNotFoundException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
      * Get a single matching resource.  Throws an exception if multiple are found.  This is useful if you
      * are expecting to find only one instance of an item on the classpath.
      * @return The single matching {...@link Resource}
Index: src/main/java/org/wicketstuff/mergedresources/annotations/ContributionScanner.java
===================================================================
--- src/main/java/org/wicketstuff/mergedresources/annotations/ContributionScanner.java	(revision 5264)
+++ src/main/java/org/wicketstuff/mergedresources/annotations/ContributionScanner.java	(working copy)
@@ -33,7 +33,7 @@
 	private Map<String, SortedSet<WeightedResourceSpec>> scan(MatchingResources resources) {
 		Map<String, SortedSet<WeightedResourceSpec>> contributions = new HashMap<String, SortedSet<WeightedResourceSpec>>();
 		
-		for (Class<?> cls : resources.getAnnotatedMatches(JsContribution.class)) {
+		for (Class<?> cls : resources.getAnnotatedMatches(JsContribution.class, true)) {
 			JsContribution a = cls.getAnnotation(JsContribution.class);
 			addJsContributions(cls, a, contributions);
 		}
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to