#313 - Assert compatibility with JSONPath 0.9 and 1.2.

We now reflectively invoke the JSONPath.compile(…) method as the method signature has changed but we need to stay compatible with 0.9 to allow the Spring Data Evans release train to upgrade to 0.17 without a major third-party dependency upgrade.

Copied the reflective method lookup from Spring's JsonPathExpectationsHelper.

Related ticket: #297, #305.
This commit is contained in:
Oliver Gierke
2015-03-02 16:05:12 +01:00
parent c3dcdf1b9e
commit de59fa40b9
2 changed files with 28 additions and 2 deletions

View File

@@ -17,6 +17,8 @@ package org.springframework.hateoas.core;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -28,6 +30,7 @@ import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkDiscoverer;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import com.jayway.jsonpath.InvalidPathException;
@@ -40,6 +43,29 @@ import com.jayway.jsonpath.JsonPath;
*/
public class JsonPathLinkDiscoverer implements LinkDiscoverer {
private static Method compileMethod;
private static Object emptyFilters;
static {
// Reflective bridging between JsonPath 0.9.x and 1.x
for (Method candidate : JsonPath.class.getMethods()) {
if (candidate.getName().equals("compile")) {
Class<?>[] paramTypes = candidate.getParameterTypes();
if (paramTypes.length == 2 && paramTypes[0].equals(String.class) && paramTypes[1].isArray()) {
compileMethod = candidate;
emptyFilters = Array.newInstance(paramTypes[1].getComponentType(), 0);
break;
}
}
}
Assert.state(compileMethod != null, "Unexpected JsonPath API - no compile(String, ...) method found");
}
private final String pathTemplate;
private final MediaType mediaType;
@@ -119,7 +145,7 @@ public class JsonPathLinkDiscoverer implements LinkDiscoverer {
* @return
*/
private JsonPath getExpression(String rel) {
return JsonPath.compile(String.format(pathTemplate, rel));
return (JsonPath) ReflectionUtils.invokeMethod(compileMethod, null, String.format(pathTemplate, rel), emptyFilters);
}
/**