[ 
https://issues.apache.org/jira/browse/FELIX-4357?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13858472#comment-13858472
 ] 

Pierre De Rop commented on FELIX-4357:
--------------------------------------

I just committed some fixes and the Felix4357 test case for the support of 
primitive types when using @Property annotation.
I tried to follow your suggestion by changing the type of the 'value' to 
'Object' in order to support any types, but I failed because it seems that java 
only allows me to use one of the following types for annotation attributes:

- a Primitive type (string, long, double, float, int, byte, char, boolean, 
short)
- a Class
- an Enum
- another Annotation
- or an array of the above types

So, what I simply did is the following:

- I first added attributes for the primitive types, in order to simply let you 
specify a property value with a primitive type.
For example:

{code}
@property(name="foo", value="bar") // type: String (scalar)
@property(name="foo", value={"bar", "zoo"}) // type: String[]
@Property(name="service.ranking", intValue=10) // type: Integer
@Property(name="foo", intValue={10, 11}) // type: Integer[]
@Property(name="foo", longValue=10) // type: Long
@Property(name="foo", longValue={10, 11}) // type: Long[]
@Property(name="foo", doubleValue=10) // type: Double
@Property(name="foo", doubleValue={10, 11}) // type: Double[]
@Property(name="foo", floatValue=10) // type: Float
@Property(name="foo", floatValue={10, 11}) // type: Float[]
@Property(name="foo", byteValue=2) // type: Byte
@Property(name="foo", byteValue={2, 3}) // type: Byte[]
@Property(name="foo", charValue='A') // type: Character
@Property(name="foo", charValue={'A', 'B'}) // type: Character[]
@Property(name="foo", booleanValue=true) // type: Boolean
@Property(name="foo", booleanValue={true, false}) // type: Boolean[]
@Property(name="foo", shortValue=1) // type: Short
@Property(name="foo", shortValue={1, 2}) // type: Short[]
{code}

- Alternatively, you can specify the property value type using a combination of 
'String value()' and 'Class type()' attributes. The 'type' attribute allows to 
specify an explicit primitive type, which will be used to parse the property 
string value.
For example, if you do this:

{code}
@Property(name="service.ranking", value="10", type=Integer.class)
{code}

Then the service.ranking property value will be built using 
Integer.valueOf("10").

For now, with what I just committed, the type attribute must match one of the 
primitive types, because if we want to allow to specify any types (using the 
'type' attribute), it means that the dependency manager runtime would have to 
make a Class.forName on the specific type and use a convention, like passing 
the string value to the constructor, or using a "valueOf" method, and moreover, 
the runtime bundle would have to declare a "DynamicImport-Package: *" header in 
order to be able to instantiate any custom types ... So I think that for the 
moment,  we should only take into account simple primitive types (the fixes I 
committed allows to do this), and for properties with specific user types, we 
should declare them programatically by returning a Map from the @Start 
annotation: For example:

{code}
@Component
class ServiceImpl implements Service {
    @Start
    Map start() {
        return new HashMap() {{
           put("foo", new MySpecificUserObject());
        }};
    }
}
{code}

Let me know if you are satisfied with this.

/Pierre




> Support types beside String/String[] in @Property annotation.
> -------------------------------------------------------------
>
>                 Key: FELIX-4357
>                 URL: https://issues.apache.org/jira/browse/FELIX-4357
>             Project: Felix
>          Issue Type: Improvement
>          Components: Dependency Manager
>            Reporter: Marcel Offermans
>            Assignee: Pierre De Rop
>            Priority: Minor
>
> The dependency manager has an extension that allows you to use annotations to 
> specify components, services and their dependencies. One of the supported 
> annotations is @Property, which allows you to specify service properties. 
> However, the values it supports are just String and String[], so if you need 
> other types (for example when specifying a service ranking) you are out of 
> luck. You can use the "workaround" described in the javadoc, which is to use 
> a @Start annotation and method, but it would be more convenient if @Property 
> had support for arbitrary types (for example by just making the value an 
> Object). This issue is about discussing and potentially adding such support.



--
This message was sent by Atlassian JIRA
(v6.1.5#6160)

Reply via email to