Skip to content

Commit

Permalink
Polishing: Shield access to Values.NO_VALUE on 3.2.x.
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-simons committed Jul 8, 2022
1 parent 040ccfc commit a60b463
Showing 1 changed file with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
package org.neo4j.ogm.drivers.embedded.driver;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import org.neo4j.graphdb.Label;
Expand All @@ -31,7 +33,6 @@
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.Relationship;
import org.neo4j.ogm.driver.TypeSystem;
import org.neo4j.values.storable.Values;

/**
* Helper methods for embedded graph entities
Expand All @@ -43,6 +44,8 @@ public class EmbeddedEntityAdapter {

private final TypeSystem typeSystem;

private volatile Optional<Object> noValue;

EmbeddedEntityAdapter(TypeSystem typeSystem) {
this.typeSystem = typeSystem;
}
Expand Down Expand Up @@ -117,7 +120,7 @@ public Map<String, Object> getAllProperties(PropertyContainer propertyContainer)

private Object toMapped(Object value) {

if (value == null || Values.NO_VALUE == value) {
if (value == null || value == getNoValue()) {
return null;
}

Expand All @@ -141,4 +144,29 @@ private Object arrayToMapped(Object array) {
}
return newArray;
}

private Object getNoValue() {

Object loadedCypherModification = this.noValue;
if (loadedCypherModification == null) {
synchronized (this) {
loadedCypherModification = this.noValue;
if (loadedCypherModification == null) {
this.noValue = Optional.ofNullable(getNoValueImpl());
loadedCypherModification = this.noValue;
}
}
}
return loadedCypherModification;
}

private static Object getNoValueImpl() {
try {
Class clazz = Class.forName("org.neo4j.values.storable.Values");
Field noValueField = clazz.getField("NO_VALUE");
return noValueField.get(null);
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
return null;
}
}
}

0 comments on commit a60b463

Please sign in to comment.