From de59fa40b932f398f8281b1810b5c0209851e031 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 2 Mar 2015 16:05:12 +0100 Subject: [PATCH] #313 - Assert compatibility with JSONPath 0.9 and 1.2. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- pom.xml | 2 +- .../hateoas/core/JsonPathLinkDiscoverer.java | 28 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 0c6bbbce..fffb6fcf 100644 --- a/pom.xml +++ b/pom.xml @@ -63,7 +63,7 @@ 2.4.3 1.0 2.1.0 - 1.2.0 + 0.9.1 2.1 1.7.10 1.2.1 diff --git a/src/main/java/org/springframework/hateoas/core/JsonPathLinkDiscoverer.java b/src/main/java/org/springframework/hateoas/core/JsonPathLinkDiscoverer.java index 7a10fce3..1c340dd8 100644 --- a/src/main/java/org/springframework/hateoas/core/JsonPathLinkDiscoverer.java +++ b/src/main/java/org/springframework/hateoas/core/JsonPathLinkDiscoverer.java @@ -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); } /**