From 0a10e642954e41ece5d0f8b95438f5588b3d53bd Mon Sep 17 00:00:00 2001 From: Michael Simons Date: Fri, 11 Sep 2020 15:22:00 +0200 Subject: [PATCH] DATAGRAPH-1376 - Make DefaultNeo4jClient compatible with GraalVM native. - Remove the usage of MethodHandles - Allow reflective access to the query runner interface. --- .../data/neo4j/core/DefaultNeo4jClient.java | 20 +++++-------------- .../spring-data-neo4j/native-image.properties | 1 + .../spring-data-neo4j/reflection-config.json | 6 ++++++ 3 files changed, 12 insertions(+), 15 deletions(-) create mode 100644 src/main/resources/META-INF/native-image/org.springframework.data/spring-data-neo4j/native-image.properties create mode 100644 src/main/resources/META-INF/native-image/org.springframework.data/spring-data-neo4j/reflection-config.json diff --git a/src/main/java/org/springframework/data/neo4j/core/DefaultNeo4jClient.java b/src/main/java/org/springframework/data/neo4j/core/DefaultNeo4jClient.java index 77db9b107..2293b835f 100644 --- a/src/main/java/org/springframework/data/neo4j/core/DefaultNeo4jClient.java +++ b/src/main/java/org/springframework/data/neo4j/core/DefaultNeo4jClient.java @@ -15,15 +15,12 @@ */ package org.springframework.data.neo4j.core; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Collection; import java.util.Map; import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Supplier; @@ -94,7 +91,6 @@ class DefaultNeo4jClient implements Neo4jClient { static class AutoCloseableQueryRunnerHandler implements InvocationHandler { - private final Map cachedHandles = new ConcurrentHashMap<>(); private final QueryRunner target; AutoCloseableQueryRunnerHandler(QueryRunner target) { @@ -110,15 +106,7 @@ class DefaultNeo4jClient implements Neo4jClient { } return null; } else { - return cachedHandles.computeIfAbsent(method, this::findHandleFor).invokeWithArguments(args); - } - } - - MethodHandle findHandleFor(Method method) { - try { - return MethodHandles.publicLookup().unreflect(method).bindTo(target); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); + return method.invoke(target, args); } } } @@ -178,7 +166,7 @@ class DefaultNeo4jClient implements Neo4jClient { * Tries to convert the given {@link RuntimeException} into a {@link DataAccessException} but returns the original * exception if the conversation failed. Thus allows safe re-throwing of the return value. * - * @param ex the exception to translate + * @param ex the exception to translate * @param exceptionTranslator the {@link PersistenceExceptionTranslator} to be used for translation * @return */ @@ -293,7 +281,9 @@ class DefaultNeo4jClient implements Neo4jClient { try (AutoCloseableQueryRunner statementRunner = getQueryRunner(this.targetDatabase)) { Result result = runnableStatement.runWith(statementRunner); - return result.hasNext() ? Optional.of(mappingFunction.apply(typeSystem, result.single())) : Optional.empty(); + return result.hasNext() ? + Optional.of(mappingFunction.apply(typeSystem, result.single())) : + Optional.empty(); } catch (RuntimeException e) { throw potentiallyConvertRuntimeException(e, persistenceExceptionTranslator); } diff --git a/src/main/resources/META-INF/native-image/org.springframework.data/spring-data-neo4j/native-image.properties b/src/main/resources/META-INF/native-image/org.springframework.data/spring-data-neo4j/native-image.properties new file mode 100644 index 000000000..a5c05f6cd --- /dev/null +++ b/src/main/resources/META-INF/native-image/org.springframework.data/spring-data-neo4j/native-image.properties @@ -0,0 +1 @@ +Args = -H:ReflectionConfigurationResources=${.}/reflection-config.json diff --git a/src/main/resources/META-INF/native-image/org.springframework.data/spring-data-neo4j/reflection-config.json b/src/main/resources/META-INF/native-image/org.springframework.data/spring-data-neo4j/reflection-config.json new file mode 100644 index 000000000..d4535a335 --- /dev/null +++ b/src/main/resources/META-INF/native-image/org.springframework.data/spring-data-neo4j/reflection-config.json @@ -0,0 +1,6 @@ +[ + { + "name" : "org.neo4j.driver.QueryRunner", + "allDeclaredMethods" : true + } +]