osgi/bugzilla-archive

[DS] No way to enforce Arrays in component properties via Annotation

bjhargrave opened this issue · 6 comments

Original bug ID: BZ#3074
From: @juergen-albert
Reported version: R7

Comment author: @juergen-albert

The Spec declares that:

1

Must result in a Integer array (or an array of any other declared type).

Thus, I can enforce an array using the XML. The https://osgi.org/specification/osgi.cmpn/7.0.0/service.component.html#org.osgi.service.component.annotations.Component.property-- on the other hand allows for a declaration of testInt:Integer=1 But I have no posibility to enforce an array, when I only have one value.

I believe that the annotations and the XML should provide the same possibilities. On the Other hand we support something close to this in for Capabilities (see: https://osgi.org/specification/osgi.cmpn/7.0.0/service.component.html#service.component-capabilities ) where we allow Provide-Capability: osgi.service; objectClass:List= "org.osgi.service.component.runtime.ServiceComponentRuntime".

Comment author: @timothyjward

The Component property member sucks and you shouldn't use it, use a component property type instead and get type safety!

@ ComponentPropertyType
public @ Interface TestInt {
  int[] value();
}


@ Component
@ TestInt(1)
public class MyComponent {}

Comment author: @juergen-albert

The ComponentProperty is definitely a good alternative. Nevertheless, should the Component Annotation reflect the same as the XML. I would suggest e.g testInt:Integer[] as a possibility.

Comment author: @timothyjward

(In reply to Jürgen Albert from comment BZ#2)

The ComponentProperty is definitely a good alternative. Nevertheless, should
the Component Annotation reflect the same as the XML. I would suggest e.g
testInt:Integer[] as a possibility.

The original design goal of the annotations was never to be 100% expressive and there are still things that can only be expressed in XML. I'm not sure that reversing that goal and making the annotations more complex to use is the answer here, especially when using a Component Property Type is better in just about every way.

Comment author: @bjhargrave

We cannot make a reasonable change to the property element of Component to allow a single element array to be specified. I would use a component property type as Tim suggests or use Converter to coerce a scalar or array value into an array at runtime.

Comment author: @bjhargrave

DS is working as designed.