FELIX-468 Implement ordering or JavaClassDescriptor's of the current project
to be handled by the plugin to ensure base classes are handled before their
extensions.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@614161 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/SCRDescriptorMojo.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/SCRDescriptorMojo.java
index 49cdf13..062590c 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/SCRDescriptorMojo.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/SCRDescriptorMojo.java
@@ -92,6 +92,22 @@
                                                                              this.project);
         // iterate through all source classes and check for component tag
         final JavaClassDescription[] javaSources = jManager.getSourceDescriptions();
+        
+        if (getLog().isDebugEnabled()) {
+            getLog().debug("Java Sources before sorting");
+            for (int i=0; i < javaSources.length; i++) {
+                System.out.println(i + " - " + javaSources[i].getName());
+            }
+        }
+        
+        Arrays.sort(javaSources, new JavaClassDescriptionInheritanceComparator());
+        
+        if (getLog().isDebugEnabled()) {
+            getLog().debug("Java Sources after sorting");
+            for (int i=0; i < javaSources.length; i++) {
+                System.out.println(i + " - " + javaSources[i].getName());
+            }
+        }
 
         final Components components = new Components();
         final Components abstractComponents = new Components();
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/JavaClassDescriptionInheritanceComparator.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/JavaClassDescriptionInheritanceComparator.java
new file mode 100644
index 0000000..2980bb1
--- /dev/null
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/JavaClassDescriptionInheritanceComparator.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.scrplugin.tags;
+
+
+import java.util.Comparator;
+
+import org.apache.maven.plugin.MojoExecutionException;
+
+
+/**
+ * The <code>JavaClassDescriptionInheritanceComparator</code> orders
+ * {@link JavaClassDescription} objects by their inheritance:
+ * <ol>
+ * <li>If the descriptors are the same, zero is returned
+ * <li>If the first descriptor is an extension of the second, 1 is returned
+ * <li>If the second descriptor is an extension of the first, -1 is returned
+ * <li>Otherwise if the first descriptor is nested deeper in the hierarchy 1 is
+ * returned, else if the second descriptor is nested deeper in the hierarchy -1
+ * is returned.
+ * <li>Finally, the natural order of the class names is returned
+ * </ol>
+ */
+public class JavaClassDescriptionInheritanceComparator implements Comparator
+{
+
+    public int compare( Object o1, Object o2 )
+    {
+        JavaClassDescription cd1 = ( JavaClassDescription ) o1;
+        JavaClassDescription cd2 = ( JavaClassDescription ) o2;
+
+        // the descriptors are the same
+        if ( equals( cd1, cd2 ) )
+        {
+            return 0;
+        }
+
+        try
+        {
+
+            int level1 = 0;
+            int level2 = 0;
+
+            // if cd1 is an extension of cd2
+            JavaClassDescription super1 = cd1.getSuperClass();
+            while ( super1 != null )
+            {
+                if ( equals( super1, cd2 ) )
+                {
+                    return 1;
+                }
+                super1 = super1.getSuperClass();
+                level1++;
+            }
+
+            // if cd2 is an extension of cd1
+            JavaClassDescription super2 = cd2.getSuperClass();
+            while ( super2 != null )
+            {
+                if ( equals( super2, cd1 ) )
+                {
+                    return -1;
+                }
+                super2 = super2.getSuperClass();
+                level2++;
+            }
+
+            // class do not share the hierarchy, order by hierarchy level
+            if ( level1 < level2 )
+            {
+                return -1;
+            }
+            else if ( level1 > level2 )
+            {
+                return 1;
+            }
+        }
+        catch ( MojoExecutionException mee )
+        {
+            // what shall we do ??
+        }
+
+        // last ressort: order by class name
+        return cd1.getName().compareTo( cd2.getName() );
+    }
+
+
+    // compare for equality: returns true if both descriptors have the same name
+    private boolean equals( JavaClassDescription cd1, JavaClassDescription cd2 )
+    {
+        return cd1.getName().equals( cd2.getName() );
+    }
+}
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/cl/ClassLoaderJavaClassDescription.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/cl/ClassLoaderJavaClassDescription.java
index 158e8a3..4e8bf14 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/cl/ClassLoaderJavaClassDescription.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/cl/ClassLoaderJavaClassDescription.java
@@ -213,4 +213,8 @@
     public boolean isPublic() {
         return Modifier.isPublic(this.clazz.getModifiers());
     }
+    
+    public String toString() {
+        return getName();
+    }
 }
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/qdox/QDoxJavaClassDescription.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/qdox/QDoxJavaClassDescription.java
index bab9c77..9a51a96 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/qdox/QDoxJavaClassDescription.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/qdox/QDoxJavaClassDescription.java
@@ -308,4 +308,8 @@
         meth.setModifiers(new String[] {"protected"});
         this.javaClass.addMethod(meth);
     }
+    
+    public String toString() {
+        return getName();
+    }
 }