You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by pd...@apache.org on 2018/10/08 21:04:28 UTC

svn commit: r1843203 - in /felix/trunk/dependencymanager: org.apache.felix.dependencymanager.annotation/src/org/apache/felix/dm/annotation/api/ org.apache.felix.dependencymanager.annotation/src/org/apache/felix/dm/annotation/plugin/bnd/ org.apache.feli...

Author: pderop
Date: Mon Oct  8 21:04:28 2018
New Revision: 1843203

URL: http://svn.apache.org/viewvc?rev=1843203&view=rev
Log:
FELIX-5957: Check if a default implementation is used only on optional dependencies

Modified:
    felix/trunk/dependencymanager/org.apache.felix.dependencymanager.annotation/src/org/apache/felix/dm/annotation/api/ServiceDependency.java
    felix/trunk/dependencymanager/org.apache.felix.dependencymanager.annotation/src/org/apache/felix/dm/annotation/plugin/bnd/AnnotationCollector.java
    felix/trunk/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/ServiceDependency.java

Modified: felix/trunk/dependencymanager/org.apache.felix.dependencymanager.annotation/src/org/apache/felix/dm/annotation/api/ServiceDependency.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/org.apache.felix.dependencymanager.annotation/src/org/apache/felix/dm/annotation/api/ServiceDependency.java?rev=1843203&r1=1843202&r2=1843203&view=diff
==============================================================================
--- felix/trunk/dependencymanager/org.apache.felix.dependencymanager.annotation/src/org/apache/felix/dm/annotation/api/ServiceDependency.java (original)
+++ felix/trunk/dependencymanager/org.apache.felix.dependencymanager.annotation/src/org/apache/felix/dm/annotation/api/ServiceDependency.java Mon Oct  8 21:04:28 2018
@@ -159,7 +159,12 @@ public @interface ServiceDependency
     String filter() default "";
 
     /**
-     * The class for the default implementation, if the dependency is not available.
+     * Sets the default implementation for an <code>optional</code> service dependency. You can use this to supply
+     * your own implementation that will be used instead of a Null Object when the dependency is
+     * not available. This is also convenient if the service dependency is not an interface
+     * (which would cause the Null Object creation to fail) but a class.
+     * Only use this attribute on an optional service dependency applied on a class field.
+     * 
      * @return the default implementation class
      */
     Class<?> defaultImpl() default Object.class;

Modified: felix/trunk/dependencymanager/org.apache.felix.dependencymanager.annotation/src/org/apache/felix/dm/annotation/plugin/bnd/AnnotationCollector.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/org.apache.felix.dependencymanager.annotation/src/org/apache/felix/dm/annotation/plugin/bnd/AnnotationCollector.java?rev=1843203&r1=1843202&r2=1843203&view=diff
==============================================================================
--- felix/trunk/dependencymanager/org.apache.felix.dependencymanager.annotation/src/org/apache/felix/dm/annotation/plugin/bnd/AnnotationCollector.java (original)
+++ felix/trunk/dependencymanager/org.apache.felix.dependencymanager.annotation/src/org/apache/felix/dm/annotation/plugin/bnd/AnnotationCollector.java Mon Oct  8 21:04:28 2018
@@ -875,7 +875,7 @@ public class AnnotationCollector extends
 
         // defaultImpl attribute
         writer.putClass(annotation, EntryParam.defaultImpl);
-
+        
         // added callback
         writer.putString(annotation, EntryParam.added, (!m_isField) ? m_method : null);
 
@@ -907,6 +907,17 @@ public class AnnotationCollector extends
         
         // dereference flag
         writer.putString(annotation, EntryParam.dereference, String.valueOf(doDereference));
+        
+        if (writer.getParameter(EntryParam.defaultImpl) != null) {
+            // If the defaultImpl attribute is set, then check if the required flag is set to false.
+        	if ("true".equals(writer.getParameter(EntryParam.required))) {
+        		throw new IllegalArgumentException("ServiceDependency defaultImpl attribute can't be used on a required dependency from class " + m_currentClassName);
+        	}
+            // If the defaultImpl attribute is set, then check if dependency is applied on a class field
+        	if (! m_isField) {
+        		throw new IllegalArgumentException("ServiceDependency defaultImpl attribute can only be used when the ServiceDependency is applied on a class field from class " + m_currentClassName);
+        	}
+        }
     }
         
     /**

Modified: felix/trunk/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/ServiceDependency.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/ServiceDependency.java?rev=1843203&r1=1843202&r2=1843203&view=diff
==============================================================================
--- felix/trunk/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/ServiceDependency.java (original)
+++ felix/trunk/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/ServiceDependency.java Mon Oct  8 21:04:28 2018
@@ -289,10 +289,11 @@ public interface ServiceDependency exten
 	public ServiceDependency setService(Class<?> serviceName, ServiceReference serviceReference);
     
     /**
-     * Sets the default implementation for this service dependency. You can use this to supply
+     * Sets the default implementation for an <code>optional</code> service dependency. You can use this to supply
      * your own implementation that will be used instead of a Null Object when the dependency is
      * not available. This is also convenient if the service dependency is not an interface
      * (which would cause the Null Object creation to fail) but a class.
+     * Only use this attribute on an optional service dependency injected on a class field.
      * 
      * @param implementation the instance to use or the class to instantiate if you want to lazily
      *     instantiate this implementation