| 72 | When using generics in the API methods, java runs a so called type erasure on all generic types, reducing a Collection<Param> to a simple Collection. |
| 73 | The runtime system cannot determine the content type of this collection unless the user places an extra hint using the @Contains annotation. |
| 74 | |
| 75 | {{{ |
| 76 | // DOES NOT WORK: Map<String,Param> will be reduced to Map, Collection<Param> will be reduced to Collection |
| 77 | public interface Api |
| 78 | { |
| 79 | Map<String,Param> returnMyParamInMap(); |
| 80 | |
| 81 | void passManyParams( Collection<Param> params ); |
| 82 | } |
| 83 | }}} |
| 84 | |
| 85 | Place a @Contains in front of the method to annotate a return type. Place it before a formal parameter |
| 86 | to annotate method parameters. |
| 87 | {{{ |
| 88 | // WORKS! |
| 89 | public interface Api |
| 90 | { |
| 91 | @Contains(Param.class) |
| 92 | Map<String,Param> returnMyParamInMap(); |
| 93 | |
| 94 | void passManyParams( @Contains(Param.class) |
| 95 | Collection<Param> params ); |
| 96 | } |
| 97 | }}} |
| 98 | |
| 99 | == Client Side == |
| 100 | Now remote clients can also use Collections and Maps containing own types: |
| 101 | {{{ |
| 102 | Api remote_api = XmlRpc.createClient( Api.class, "handlerId", host, port ); |
| 103 | |
| 104 | Map<String,Param> map = remote_api.returnMyParamInMap(); |
| 105 | for( Param p : map.values() ) |
| 106 | { |
| 107 | ... |
| 108 | } |
| 109 | |
| 110 | remote_api.passManyParams( Collections.asList( new Param[]{new Param()} ) ); |
| 111 | ... |
| 112 | }}} |