Skip to content

Commit

Permalink
added support for fixed parameters via annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
oak committed Jan 11, 2022
1 parent 3706d07 commit b3e84e0
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 8 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,54 @@ public interface LibraryService {
```json
{"jsonrpc":"2.0", "method": "VideoLibrary.GetTVShows", "params": { "properties": ["title"] }, "id":1}
```
#### Fixed parameters

You may need to pass some fixed parameters to a JsonRpc method. In this case, use the annotation @JsonRpcFixedParam on the service method. You may also use @JsonRpcFixedParams to pass a collection of fixed parameters.

```java
@JsonRpcService("/jsonrpc")
public interface LibraryService {
@JsonRpcMethod("VideoLibrary.GetTVShows")
@JsonRpcFixedParam(name = "status", value = "published")
List<TVShow> fetchTVShows(@JsonRpcParam(value="properties") final List<String> properties);
}
```

```json
{
"jsonrpc":"2.0",
"method": "VideoLibrary.GetTVShows",
"params": {
"status": "published",
"properties": ["title"]
},
"id":1
}
```

```java
@JsonRpcService("/jsonrpc")
public interface LibraryService {
@JsonRpcMethod("VideoLibrary.GetTVShows")
@JsonRpcFixedParams(fixedParams = {
@JsonRpcFixedParam(name = "status", value = "published"),
@JsonRpcFixedParam(name = "order_by", value = "recent") })
List<TVShow> fetchTVShows(@JsonRpcParam(value="properties") final List<String> properties);
}
```

```json
{
"jsonrpc":"2.0",
"method": "VideoLibrary.GetTVShows",
"params": {
"status": "published",
"order_by": "recent",
"properties": ["title"]
},
"id":1
}
```

[Google Group]: http://groups.google.com/group/json-rpc
[Jackson page]: https://github.com/FasterXML/jackson
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/googlecode/jsonrpc4j/JsonRpcFixedParam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.googlecode.jsonrpc4j;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonRpcFixedParam {

/**
* @return the parameter's name.
*/
String name();

/**
* @return the parameter's value.
*/
String value();
}
16 changes: 16 additions & 0 deletions src/main/java/com/googlecode/jsonrpc4j/JsonRpcFixedParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.googlecode.jsonrpc4j;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonRpcFixedParams {

/**
* @return a fixed parameter's collection.
*/
JsonRpcFixedParam[] fixedParams();
}
63 changes: 55 additions & 8 deletions src/main/java/com/googlecode/jsonrpc4j/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,27 @@ public static Object parseArguments(Method method, Object[] arguments) {
if (jsonRpcMethod != null)
paramsPassMode = jsonRpcMethod.paramsPassMode();

Map<String, Object> namedParams = getNamedParameters(method, arguments);
Map<String, Object> params = new LinkedHashMap<>();

params.putAll(getFixedParametersCollection(method));
params.putAll(getFixedParameters(method));
params.putAll(getNamedParameters(method, arguments));

switch (paramsPassMode) {
case ARRAY:
if (namedParams.size() > 0) {
Object[] parsed = new Object[namedParams.size()];
if (params.size() > 0) {
Object[] parsed = new Object[params.size()];
int i = 0;
for (Object value : namedParams.values()) {
for (Object value : params.values()) {
parsed[i++] = value;
}
return parsed;
} else {
return arguments != null ? arguments : new Object[]{};
}
case OBJECT:
if (namedParams.size() > 0) {
return namedParams;
if (params.size() > 0) {
return params;
} else {
if (arguments == null) {
return new Object[]{};
Expand All @@ -221,8 +225,8 @@ public static Object parseArguments(Method method, Object[] arguments) {
}
case AUTO:
default:
if (namedParams.size() > 0) {
return namedParams;
if (params.size() > 0) {
return params;
} else {
return arguments != null ? arguments : new Object[]{};
}
Expand Down Expand Up @@ -260,6 +264,49 @@ private static Map<String, Object> getNamedParameters(Method method, Object[] ar
return namedParams;
}

/**
* Checks method for @JsonRpcFixedParam annotations and returns fixed
* parameters.
*
* @param method the method
* @return fixed parameters or empty if no annotations found
*/
public static Map<String, Object> getFixedParameters(Method method) {

Map<String, Object> fixedParams = new LinkedHashMap<>();

for (Annotation an : getAnnotations(method)) {
if (an instanceof JsonRpcFixedParam) {
JsonRpcFixedParam jAnn = (JsonRpcFixedParam) an;
fixedParams.put(jAnn.name(), jAnn.value());
}
}

return fixedParams;
}

/**
* Checks method for @JsonRpcFixedParams annotation and returns fixed
* parameters.
*
* @param method the method
* @return fixed parameters or empty if no annotations found
*/
public static Map<String, Object> getFixedParametersCollection(Method method) {

Map<String, Object> fixedParams = new LinkedHashMap<>();

JsonRpcFixedParams jsonRpcFixedParams = getAnnotation(method, JsonRpcFixedParams.class);

if (jsonRpcFixedParams != null) {
for (JsonRpcFixedParam fixedParam : jsonRpcFixedParams.fixedParams()) {
fixedParams.put(fixedParam.name(), fixedParam.value());
}
}

return fixedParams;
}

public static void clearCache() {
methodCache.clear();
parameterTypeCache.clear();
Expand Down
112 changes: 112 additions & 0 deletions src/test/java/com/googlecode/jsonrpc4j/ReflectionUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,35 @@ public void someNamedParamsPassParamsAuto() throws Exception {

ReflectionUtil.parseArguments(JsonRpcTestService.class.getMethod("someNamedParamsPassParamsAuto", String.class, int.class), null);
}

@Test
public void fixedParamPassParamsAuto() throws Exception {
Object[] arguments = { "1", 2 };

@SuppressWarnings("unchecked")
Map<String, Object> params = (Map<String, Object>) ReflectionUtil.parseArguments(
JsonRpcTestService.class.getMethod("fixedParamPassParamsAuto", String.class, int.class), arguments);

assertEquals(3, params.size());
assertEquals("1", params.get("one"));
assertEquals(2, params.get("two"));
assertEquals("value1", params.get("param1"));
}

@Test
public void fixedParamsPassParamsAuto() throws Exception {
Object[] arguments = { "1", 2 };

@SuppressWarnings("unchecked")
Map<String, Object> params = (Map<String, Object>) ReflectionUtil.parseArguments(
JsonRpcTestService.class.getMethod("fixedParamsPassParamsAuto", String.class, int.class), arguments);

assertEquals(4, params.size());
assertEquals("1", params.get("one"));
assertEquals(2, params.get("two"));
assertEquals("value1", params.get("param1"));
assertEquals("value2", params.get("param2"));
}

@Test
public void allNamedParamsPassParamsAuto() throws Exception {
Expand All @@ -82,6 +111,33 @@ public void someNamedParamsPassParamsArray() throws Exception {

ReflectionUtil.parseArguments(JsonRpcTestService.class.getMethod("someNamedParamsPassParamsArray", String.class, int.class), null);
}

@Test
public void fixedParamPassParamsArray() throws Exception {
Object[] arguments = { "1", 2 };

Object[] params = (Object[]) ReflectionUtil.parseArguments(
JsonRpcTestService.class.getMethod("fixedParamPassParamsArray", String.class, int.class), arguments);

assertEquals(3, params.length);
assertEquals("value1", params[0]);
assertEquals("1", params[1]);
assertEquals(2, params[2]);
}

@Test
public void fixedParamsPassParamsArray() throws Exception {
Object[] arguments = { "1", 2 };

Object[] params = (Object[]) ReflectionUtil.parseArguments(
JsonRpcTestService.class.getMethod("fixedParamsPassParamsArray", String.class, int.class), arguments);

assertEquals(4, params.length);
assertEquals("value1", params[0]);
assertEquals("value2", params[1]);
assertEquals("1", params[2]);
assertEquals(2, params[3]);
}

@Test
public void allNamedParamsPassParamsArray() throws Exception {
Expand All @@ -106,6 +162,35 @@ public void someNamedParamsPassParamsObject() throws Exception {

ReflectionUtil.parseArguments(JsonRpcTestService.class.getMethod("someNamedParamsPassParamsObject", String.class, int.class), null);
}

@Test
public void fixedParamPassParamsObject() throws Exception {
Object[] arguments = { "1", 2 };

@SuppressWarnings("unchecked")
Map<String, Object> params = (Map<String, Object>) ReflectionUtil.parseArguments(
JsonRpcTestService.class.getMethod("fixedParamPassParamsObject", String.class, int.class), arguments);

assertEquals(3, params.size());
assertEquals("1", params.get("one"));
assertEquals(2, params.get("two"));
assertEquals("value1", params.get("param1"));
}

@Test
public void fixedParamsPassParamsObject() throws Exception {
Object[] arguments = { "1", 2 };

@SuppressWarnings("unchecked")
Map<String, Object> params = (Map<String, Object>) ReflectionUtil.parseArguments(
JsonRpcTestService.class.getMethod("fixedParamsPassParamsObject", String.class, int.class), arguments);

assertEquals(4, params.size());
assertEquals("1", params.get("one"));
assertEquals(2, params.get("two"));
assertEquals("value1", params.get("param1"));
assertEquals("value2", params.get("param2"));
}

@Test
public void allNamedParamsPassParamsObject() throws Exception {
Expand Down Expand Up @@ -143,6 +228,15 @@ private interface JsonRpcTestService {
@JsonRpcMethod(value = "someNamedParamsPassParamsAuto", paramsPassMode = JsonRpcParamsPassMode.AUTO)
void someNamedParamsPassParamsAuto(@JsonRpcParam("one") String one, int two);

@JsonRpcMethod(value = "fixedParamPassParamsAuto", paramsPassMode = JsonRpcParamsPassMode.AUTO)
@JsonRpcFixedParam(name = "param1", value = "value1")
void fixedParamPassParamsAuto(@JsonRpcParam("one") String one, @JsonRpcParam("two") int two);

@JsonRpcMethod(value = "fixedParamsPassParamsAuto", paramsPassMode = JsonRpcParamsPassMode.AUTO)
@JsonRpcFixedParams(fixedParams = { @JsonRpcFixedParam(name = "param1", value = "value1"),
@JsonRpcFixedParam(name = "param2", value = "value2") })
void fixedParamsPassParamsAuto(@JsonRpcParam("one") String one, @JsonRpcParam("two") int two);

@JsonRpcMethod(value = "allNamedParamsPassParamsAuto", paramsPassMode = JsonRpcParamsPassMode.AUTO)
void allNamedParamsPassParamsAuto(@JsonRpcParam("one") String one, @JsonRpcParam("two") int two);

Expand All @@ -152,6 +246,15 @@ private interface JsonRpcTestService {
@JsonRpcMethod(value = "someNamedParamsPassParamsArray", paramsPassMode = JsonRpcParamsPassMode.ARRAY)
void someNamedParamsPassParamsArray(@JsonRpcParam("one") String one, int two);

@JsonRpcMethod(value = "fixedParamPassParamsArray", paramsPassMode = JsonRpcParamsPassMode.ARRAY)
@JsonRpcFixedParam(name = "param1", value = "value1")
void fixedParamPassParamsArray(@JsonRpcParam("one") String one, @JsonRpcParam("two") int two);

@JsonRpcMethod(value = "fixedParamsPassParamsArray", paramsPassMode = JsonRpcParamsPassMode.ARRAY)
@JsonRpcFixedParams(fixedParams = { @JsonRpcFixedParam(name = "param1", value = "value1"),
@JsonRpcFixedParam(name = "param2", value = "value2") })
void fixedParamsPassParamsArray(@JsonRpcParam("one") String one, @JsonRpcParam("two") int two);

@JsonRpcMethod(value = "allNamedParamsPassParamsArray", paramsPassMode = JsonRpcParamsPassMode.ARRAY)
void allNamedParamsPassParamsArray(@JsonRpcParam("one") String one, @JsonRpcParam("two") int two);

Expand All @@ -161,6 +264,15 @@ private interface JsonRpcTestService {
@JsonRpcMethod(value = "someNamedParamsPassParamsObject", paramsPassMode = JsonRpcParamsPassMode.OBJECT)
void someNamedParamsPassParamsObject(@JsonRpcParam("one") String one, int two);

@JsonRpcMethod(value = "fixedParamPassParamsObject", paramsPassMode = JsonRpcParamsPassMode.OBJECT)
@JsonRpcFixedParam(name = "param1", value = "value1")
void fixedParamPassParamsObject(@JsonRpcParam("one") String one, @JsonRpcParam("two") int two);

@JsonRpcMethod(value = "fixedParamsPassParamsObject", paramsPassMode = JsonRpcParamsPassMode.OBJECT)
@JsonRpcFixedParams(fixedParams = { @JsonRpcFixedParam(name = "param1", value = "value1"),
@JsonRpcFixedParam(name = "param2", value = "value2") })
void fixedParamsPassParamsObject(@JsonRpcParam("one") String one, @JsonRpcParam("two") int two);

@JsonRpcMethod(value = "allNamedParamsPassParamsObject", paramsPassMode = JsonRpcParamsPassMode.OBJECT)
void allNamedParamsPassParamsObject(@JsonRpcParam("one") String one, @JsonRpcParam("two") int two);

Expand Down

0 comments on commit b3e84e0

Please sign in to comment.