You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2013/01/30 21:49:14 UTC

svn commit: r1440660 - in /felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin: helper/ClassScanner.java xml/ComponentDescriptorIO.java xml/MetaTypeIO.java

Author: cziegeler
Date: Wed Jan 30 20:49:13 2013
New Revision: 1440660

URL: http://svn.apache.org/viewvc?rev=1440660&view=rev
Log:
FELIX-3332 : SCR annotations @Activate @Deactivate @Modified in outer classes also affect nested classes, annotations in nested classes are ignored 

Modified:
    felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/helper/ClassScanner.java
    felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java
    felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/MetaTypeIO.java

Modified: felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/helper/ClassScanner.java
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/helper/ClassScanner.java?rev=1440660&r1=1440659&r2=1440660&view=diff
==============================================================================
--- felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/helper/ClassScanner.java (original)
+++ felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/helper/ClassScanner.java Wed Jan 30 20:49:13 2013
@@ -143,26 +143,7 @@ public class ClassScanner {
                 // load the class
                 final Class<?> annotatedClass = project.getClassLoader().loadClass(src.getClassName());
 
-                final ClassDescription desc = this.processClass(annotatedClass, src.getFile().toString());
-                if ( desc != null ) {
-                    this.allDescriptions.put(annotatedClass.getName(), desc);
-                    if ( desc.getDescriptions(ComponentDescription.class).size() > 0) {
-                        result.add(desc);
-                        log.debug("Found component description " + desc + " in " + annotatedClass.getName());
-                    } else {
-                        // check whether one of the other annotations is used and log a warning (FELIX-3636)
-                        if ( desc.getDescription(PropertyDescription.class) != null
-                             || desc.getDescription(ReferenceDescription.class) != null
-                             || desc.getDescription(ServiceDescription.class) != null ) {
-                            iLog.addWarning("Class '" + src.getClassName() + "' contains SCR annotations, but not a" +
-                                 "@Component (or equivalent) annotation. Therefore no component descriptor is created for this" +
-                                 "class. Please add a @Component annotation and consider making it abstract.",
-                                 src.getFile().toString());
-                        }
-                    }
-                } else {
-                    this.allDescriptions.put(annotatedClass.getName(), new ClassDescription(annotatedClass, GENERATED));
-                }
+                this.process(annotatedClass, src, result);
             } catch (final ClassNotFoundException cnfe) {
                 throw new SCRDescriptorFailureException("Unable to load compiled class: " + src.getClassName(), cnfe);
             }
@@ -171,6 +152,41 @@ public class ClassScanner {
     }
 
     /**
+     * Process a class
+     * @throws SCRDescriptorException
+     * @throws SCRDescriptorFailureException
+     */
+    private void process(final Class<?> annotatedClass, final Source src, final List<ClassDescription> result)
+    throws SCRDescriptorFailureException, SCRDescriptorException {
+        final ClassDescription desc = this.processClass(annotatedClass, src.getFile().toString());
+        if ( desc != null ) {
+            this.allDescriptions.put(annotatedClass.getName(), desc);
+            if ( desc.getDescriptions(ComponentDescription.class).size() > 0) {
+                result.add(desc);
+                log.debug("Found component description " + desc + " in " + annotatedClass.getName());
+            } else {
+                // check whether one of the other annotations is used and log a warning (FELIX-3636)
+                if ( desc.getDescription(PropertyDescription.class) != null
+                     || desc.getDescription(ReferenceDescription.class) != null
+                     || desc.getDescription(ServiceDescription.class) != null ) {
+                    iLog.addWarning("Class '" + src.getClassName() + "' contains SCR annotations, but not a" +
+                         "@Component (or equivalent) annotation. Therefore no component descriptor is created for this" +
+                         "class. Please add a @Component annotation and consider making it abstract.",
+                         src.getFile().toString());
+                }
+            }
+        } else {
+            this.allDescriptions.put(annotatedClass.getName(), new ClassDescription(annotatedClass, GENERATED));
+        }
+        // process inner classes
+        for(final Class<?> innerClass : annotatedClass.getDeclaredClasses()) {
+            if ( !innerClass.isAnnotation() && !innerClass.isInterface() ) {
+                process(innerClass, src, result);
+            }
+        }
+    }
+
+    /**
      * Scan a single class.
      */
     private ClassDescription processClass(final Class<?> annotatedClass, final String location)

Modified: felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java?rev=1440660&r1=1440659&r2=1440660&view=diff
==============================================================================
--- felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java (original)
+++ felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java Wed Jan 30 20:49:13 2013
@@ -24,7 +24,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.security.Provider.Service;
 import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Iterator;
 import java.util.List;
 import java.util.StringTokenizer;
 
@@ -765,11 +765,34 @@ public class ComponentDescriptorIO {
         final List<String> fileNames = new ArrayList<String>();
         if ( options.isGenerateSeparateDescriptors() ) {
             final SpecVersion globalVersion = module.getOptions().getSpecVersion();
-            for(final ComponentContainer component : components ) {
+            while ( !components.isEmpty() ) {
+                // get the first component
+                final List<ComponentContainer> innerList = new ArrayList<ComponentContainer>();
+                final ComponentContainer component = components.remove(0);
+                innerList.add(component);
+                final int pos = component.getClassDescription().getDescribedClass().getName().indexOf('$');
+                final String baseClassName;
+                if ( pos == -1 ) {
+                    baseClassName = component.getClassDescription().getDescribedClass().getName();
+                } else {
+                    baseClassName = component.getClassDescription().getDescribedClass().getName().substring(0, pos);
+                }
+                final String baseClassPrefix = baseClassName + '$';
+
+                // check for inner classes
+                final Iterator<ComponentContainer> i = components.iterator();
+                while ( i.hasNext() ) {
+                    final ComponentContainer cc = i.next();
+                    if ( cc.getClassDescription().getDescribedClass().getName().startsWith(baseClassPrefix) ) {
+                        innerList.add(cc);
+                        i.remove();
+                    }
+                }
+
                 module.getOptions().setSpecVersion(component.getComponentDescription().getSpecVersion());
-                final File file = new File(descriptorDir, component.getClassDescription().getDescribedClass().getName() + ".xml");
+                final File file = new File(descriptorDir, baseClassName + ".xml");
                 try {
-                    ComponentDescriptorIO.generateXML(module, Collections.singletonList(component), file, logger);
+                    ComponentDescriptorIO.generateXML(module, innerList, file, logger);
                 } catch (final IOException e) {
                     throw new SCRDescriptorException("Unable to generate xml", file.toString(), e);
                 } catch (final TransformerException e) {

Modified: felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/MetaTypeIO.java
URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/MetaTypeIO.java?rev=1440660&r1=1440659&r2=1440660&view=diff
==============================================================================
--- felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/MetaTypeIO.java (original)
+++ felix/trunk/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/xml/MetaTypeIO.java Wed Jan 30 20:49:13 2013
@@ -21,7 +21,6 @@ package org.apache.felix.scrplugin.xml;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -97,14 +96,43 @@ public class MetaTypeIO {
 
                 final List<String> fileNames = new ArrayList<String>();
                 if ( options.isGenerateSeparateDescriptors() ) {
+                    // create a list with relevant components
+                    final List<ComponentContainer> components = new ArrayList<ComponentContainer>();
                     for(final ComponentContainer component : module.getComponents() ) {
                         if ( component.getMetatypeContainer() != null ) {
-                            final File file = new File(mtDir, component.getClassDescription().getDescribedClass().getName() + ".xml");
-                            logger.info("Generating 1 MetaType Descriptor in " + file);
-                            MetaTypeIO.write(module, Collections.singletonList(component), file);
-                            fileNames.add(parentDir.getName() + '/' + mtDir.getName() + '/' + file.getName());
+                            components.add(component);
                         }
                     }
+
+                    while ( !components.isEmpty() ) {
+                        // get the first component
+                        final List<ComponentContainer> innerList = new ArrayList<ComponentContainer>();
+                        final ComponentContainer component = components.remove(0);
+                        innerList.add(component);
+                        final int pos = component.getClassDescription().getDescribedClass().getName().indexOf('$');
+                        final String baseClassName;
+                        if ( pos == -1 ) {
+                            baseClassName = component.getClassDescription().getDescribedClass().getName();
+                        } else {
+                            baseClassName = component.getClassDescription().getDescribedClass().getName().substring(0, pos);
+                        }
+                        final String baseClassPrefix = baseClassName + '$';
+
+                        // check for inner classes
+                        final Iterator<ComponentContainer> i = components.iterator();
+                        while ( i.hasNext() ) {
+                            final ComponentContainer cc = i.next();
+                            if ( cc.getClassDescription().getDescribedClass().getName().startsWith(baseClassPrefix) ) {
+                                innerList.add(cc);
+                                i.remove();
+                            }
+                        }
+
+                        final File file = new File(mtDir, baseClassName + ".xml");
+                        logger.info("Generating " + innerList.size() + " MetaType Descriptor in " + file);
+                        MetaTypeIO.write(module, innerList, file);
+                        fileNames.add(parentDir.getName() + '/' + mtDir.getName() + '/' + file.getName());
+                    }
                 } else {
                     logger.info("Generating " + metatypeCount + " MetaType Descriptors in " + mtFile);
                     MetaTypeIO.write(module, module.getComponents(), mtFile);