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

David Bosschaert edited comment on FELIX-6137 at 5/31/19 1:51 PM:
------------------------------------------------------------------

Hi [~marett], this is actually by design. You can convert a map to an interface 
or annotation without having to specify all the values available as methods in 
those. However, if you invoke a method on the interface or annotation for which 
no value is specified in the original map or a default is otherwise provided, 
the converter throws an exception. This behaviour is specified in the OSGi 
converter spec 
([https://osgi.org/specification/osgi.enterprise/7.0.0/util.converter.html#util.converter-maps)]
 section 707.4.4.4.5 Annotation states: "If no default is specified a 
ConversionException is thrown."

However, since returning {{null}} for undefined values is a pretty common 
scenario, you can quite easily achieve this by slightly customizing your 
converter using an Error Handler. This is described in section 707.7 
([https://osgi.org/specification/osgi.enterprise/7.0.0/util.converter.html#d0e107836)]
 of the converter spec.

So to apply this to your test case, instead of
{code:java}
@Test
public void testDictionaryToAnnotationWithoutDefault() {
  Dictionary<String, Object> dict = new TestDictionary<>();
  TestAnnotation ta = converter.convert(dict).to(TestAnnotation.class);
  assertEquals(null, ta.noDefault());
}
{code}
You can change it to:
{code:java}
@Test
public void testDictionaryToAnnotationWithoutDefault() {
  Converter nullDefaultConverter = converter.newConverterBuilder().
            errorHandler((o,t) -> null).build();
        
  Dictionary<String, Object> dict = new TestDictionary<>();
  TestAnnotation ta = nullDefaultConverter.convert(dict).
    to(TestAnnotation.class);
  assertEquals(null, ta.noDefault());
}
{code}
Note the error handler lambda, which will return null instead of throwing a 
conversion exception. It will provide the behaviour you are looking for I think.
  


was (Author: bosschaert):
Hi [~marett], this is actually by design. You can convert a map to an interface 
or annotation without having to specify all the values available as methods in 
those. However, if you invoke a method on the interface or annotation for which 
no value is specified in the original map or a default is otherwise provided, 
the converter throws an exception. This behaviour is specified in the OSGi 
converter spec 
([https://osgi.org/specification/osgi.enterprise/7.0.0/util.converter.html#util.converter-maps)]
 section 707.4.4.4.5 Annotation states: "If no default is specified a 
ConversionException is thrown."

However, since returning {{null}} for undefined values is a pretty common 
scenario, you can quite easily achieve this by slightly customizing your 
converter using an Error Handler. This is described in section 707.7 
([https://osgi.org/specification/osgi.enterprise/7.0.0/util.converter.html#d0e107836)]
 of the converter spec.

So to apply this to your test case, instead of
{code:java}
@Test
public void testDictionaryToAnnotationWithoutDefault() {
  Dictionary<String, Object> dict = new TestDictionary<>();
  TestAnnotation ta = converter.convert(dict).to(TestAnnotation.class);
  assertEquals(null, ta.noDefault());
}
{code}
You can change it to:
{code:java}
@Test
public void testDictionaryToAnnotationWithoutDefault() {
  Converter nullDefaultConverter = converter.newConverterBuilder().
            errorHandler((o,t) -> null).build();
        
  Dictionary<String, Object> dict = new TestDictionary<>();
  TestAnnotation ta = 
nullDefaultConverter.convert(dict).to(TestAnnotation.class);
  assertEquals(null, ta.noDefault());
}
{code}
Note the error handler lambda, which will return null instead of throwing a 
conversion exception. It will provide the behaviour you are looking for I think.
  

> Converting an default-less annotation property throws instead of returning 
> null
> -------------------------------------------------------------------------------
>
>                 Key: FELIX-6137
>                 URL: https://issues.apache.org/jira/browse/FELIX-6137
>             Project: Felix
>          Issue Type: Bug
>          Components: Converter
>    Affects Versions: converter-1.0.8
>            Reporter: Timothee Maret
>            Assignee: David Bosschaert
>            Priority: Major
>
> Converting a dictionary to an annotation that does not define a default value 
> property throws a {{org.osgi.util.converter.ConversionException}} like the 
> one below instead of returning {{null}}.
> {code}
> org.osgi.util.converter.ConversionException: No value for property: noDefault
>       at 
> org.osgi.util.converter.ConvertingImpl$4.invoke(ConvertingImpl.java:806)
>       at org.osgi.util.converter.$Proxy9.noDefault(Unknown Source)
>       at 
> org.osgi.util.converter.ConverterMapTest.testDictionaryToAnnotationWithoutDefault(ConverterMapTest.java:520)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>       at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>       at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>       at java.lang.reflect.Method.invoke(Method.java:498)
>       at 
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
>       at 
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
>       at 
> org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
>       at 
> org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
>       at 
> org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
>       at 
> org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
>       at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
>       at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
>       at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
>       at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
>       at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
>       at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
>       at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
>       at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to