Skip to content
This repository was archived by the owner on Jan 2, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions jsonrpc-java/src/main/java/org/json/rpc/client/JsonRpcInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,30 @@ public final class JsonRpcInvoker {

private final TypeChecker typeChecker;

private final Gson gson;

public JsonRpcInvoker() {
this(new GsonTypeChecker());
this(new GsonTypeChecker(), new Gson());
}

public JsonRpcInvoker(Gson gson) {
this(new GsonTypeChecker(), gson);
}

public JsonRpcInvoker(TypeChecker typeChecker) {
this(typeChecker, new Gson());
}

public JsonRpcInvoker(TypeChecker typeChecker, Gson gson) {
this.typeChecker = typeChecker;
this.gson = gson;
}

public <T> T get(final JsonRpcClientTransport transport, final String handle, final Class<T>... classes) {
for (Class<T> clazz : classes) {
typeChecker.isValidInterface(clazz);
}
return (T) Proxy.newProxyInstance(JsonRpcInvoker.class.getClassLoader(), classes, new InvocationHandler() {

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return JsonRpcInvoker.this.invoke(handle, transport, method, args);
}
Expand All @@ -68,8 +78,6 @@ private Object invoke(String handleName,
int id = rand.nextInt(Integer.MAX_VALUE);
String methodName = handleName + "." + method.getName();

Gson gson = new Gson();

JsonObject req = new JsonObject();
req.addProperty("id", id);
req.addProperty("method", methodName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private boolean isValidType(Class<?> clazz, boolean throwException, Set<Class<?>
}

boolean zeroArgConstructor = (clazz.getConstructors().length == 0);
for (Constructor c : clazz.getConstructors()) {
for (Constructor<?> c : clazz.getConstructors()) {
if (c.getParameterTypes().length == 0) {
zeroArgConstructor = true;
break;
Expand All @@ -100,14 +100,12 @@ private boolean isValidType(Class<?> clazz, boolean throwException, Set<Class<?>
}

// avoid circular references
visited = (visited == null ? new HashSet<Class<?>>() : visited);
if (visited.contains(clazz)) {
if (throwException) {
throw new IllegalArgumentException("circular reference detected : " + clazz);
}
if(visited == null) visited = new HashSet<Class<?>>();
if (!visited.add(clazz)) {
if (throwException)
throw new IllegalArgumentException("circular reference detected : " + clazz);
return false;
}
visited.add(clazz);

// Check for fields because Gson uses fields
for (Field f : clazz.getDeclaredFields()) {
Expand Down Expand Up @@ -142,6 +140,7 @@ private boolean isValidType(Class<?> clazz, boolean throwException, Set<Class<?>
}
}

visited.remove(clazz);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,25 @@ public final class JsonRpcExecutor implements RpcIntroSpection {

private final TypeChecker typeChecker;
private volatile boolean locked;

private final Gson gson;

public JsonRpcExecutor() {
this(new GsonTypeChecker());
this(new GsonTypeChecker(), new Gson());
}

public JsonRpcExecutor(Gson gson) {
this(new GsonTypeChecker(), gson);
}

@SuppressWarnings("unchecked")
public JsonRpcExecutor(TypeChecker typeChecker) {
this(typeChecker, new Gson());
}

@SuppressWarnings("unchecked")
public JsonRpcExecutor(TypeChecker typeChecker, Gson gson) {
this.typeChecker = typeChecker;
this.gson = gson;
this.handlers = new HashMap<String, HandleEntry<?>>();
addHandler("system", this, RpcIntroSpection.class);
}
Expand Down Expand Up @@ -239,7 +250,7 @@ private JsonElement executeMethod(String methodName, JsonArray params) throws Th
Object result = executableMethod.invoke(
handleEntry.getHandler(), getParameters(executableMethod, params));

return new Gson().toJsonTree(result);
return gson.toJsonTree(result);
} catch (Throwable t) {
if (t instanceof InvocationTargetException) {
t = ((InvocationTargetException) t).getTargetException();
Expand All @@ -261,7 +272,6 @@ public boolean canExecute(Method method, JsonArray params) {

public Object[] getParameters(Method method, JsonArray params) {
List<Object> list = new ArrayList<Object>();
Gson gson = new Gson();
Class<?>[] types = method.getParameterTypes();
for (int i = 0; i < types.length; i++) {
JsonElement p = params.get(i);
Expand Down