diff --git a/ebean-core-type/src/main/java/io/ebean/core/type/ScalarJsonManager.java b/ebean-core-type/src/main/java/io/ebean/core/type/ScalarJsonManager.java new file mode 100644 index 000000000..f0e645d1d --- /dev/null +++ b/ebean-core-type/src/main/java/io/ebean/core/type/ScalarJsonManager.java @@ -0,0 +1,24 @@ +package io.ebean.core.type; + +import io.ebean.annotation.MutationDetection; + +/** + * Holds the configured mapper and default mutation detection mode to be used. + */ +public interface ScalarJsonManager { + + /** + * Return the default mutation detection mode. + */ + MutationDetection mutationDetection(); + + /** + * Return the object mapper supplied in DatabaseConfig. + */ + Object mapper(); + + /** + * Return the Postgres type given the jdbc type. + */ + String postgresType(int dbType); +} diff --git a/ebean-core-type/src/main/java/io/ebean/core/type/ScalarJsonMapper.java b/ebean-core-type/src/main/java/io/ebean/core/type/ScalarJsonMapper.java new file mode 100644 index 000000000..de38bd942 --- /dev/null +++ b/ebean-core-type/src/main/java/io/ebean/core/type/ScalarJsonMapper.java @@ -0,0 +1,4 @@ +package io.ebean.core.type; + +public interface ScalarJsonMapper { +} diff --git a/ebean-core-type/src/main/java/io/ebean/core/type/ScalarJsonRequest.java b/ebean-core-type/src/main/java/io/ebean/core/type/ScalarJsonRequest.java new file mode 100644 index 000000000..c47681fd1 --- /dev/null +++ b/ebean-core-type/src/main/java/io/ebean/core/type/ScalarJsonRequest.java @@ -0,0 +1,67 @@ +package io.ebean.core.type; + +import io.ebean.annotation.MutationDetection; + +/** + * A request to create a ScalarType for a bean property (typically annotated with {@code @DbJson}). + */ +public final class ScalarJsonRequest { + + private final ScalarJsonManager manager; + private final int dbType; + private final DocPropertyType docType; + private final Class beanType; + private final MutationDetection mode; + private final String name; + + public ScalarJsonRequest(ScalarJsonManager manager, int dbType, DocPropertyType docType, Class beanType, MutationDetection mode, String name) { + this.manager = manager; + this.dbType = dbType; + this.docType = docType; + this.beanType = beanType; + this.mode = mode; + this.name = name; + } + + /** + * Return the manager. + */ + public ScalarJsonManager manager() { + return manager; + } + + /** + * Return the jdbc type this property maps to (e.g. JSON, JSONB etc). + */ + public int dbType() { + return dbType; + } + + /** + * Return the document store docType. + */ + public DocPropertyType docType() { + return docType; + } + + /** + * Return the type of the bean that this property belongs to. + */ + public Class beanType() { + return beanType; + } + + /** + * Return the mutation detection mode the property should use. + */ + public MutationDetection mode() { + return mode; + } + + /** + * Return the property name. + */ + public String name() { + return name; + } +} diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanDescriptor.java b/ebean-core/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanDescriptor.java index 49767439f..dfeacbd83 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanDescriptor.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanDescriptor.java @@ -152,7 +152,6 @@ public class DeployBeanDescriptor { private DocStoreMode docStoreDelete; private DeployBeanProperty idProperty; private TableJoin primaryKeyJoin; - private Object jacksonAnnotatedClass; /** * Construct the BeanDescriptor. @@ -1130,14 +1129,4 @@ public class DeployBeanDescriptor { } } - /** - * Returns the jackson annotated class, if jackson is present. - */ - @SuppressWarnings("unchecked") - Object /*AnnotatedClass*/ getJacksonAnnotatedClass() { - if (jacksonAnnotatedClass == null) { - jacksonAnnotatedClass = new DeployBeanObtainJackson(config, beanType).obtain(); - } - return jacksonAnnotatedClass; - } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java b/ebean-core/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java index 847401b1c..2507387ab 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java @@ -1042,20 +1042,6 @@ public class DeployBeanProperty { this.elementProperty = true; } - /** - * Returns the jackson annotated field, if jackson is present. - */ - public Object /*AnnotatedField*/ getJacksonField() { - com.fasterxml.jackson.databind.introspect.AnnotatedClass jac = - (com.fasterxml.jackson.databind.introspect.AnnotatedClass) getDesc().getJacksonAnnotatedClass(); - for (com.fasterxml.jackson.databind.introspect.AnnotatedField candidate : jac.fields()) { - if (candidate.getName().equals(getName())) { - return candidate; - } - } - return null; - } - public void initMetaAnnotations(Set> metaAnnotationsFilter) { metaAnnotations = AnnotationUtil.metaFindAllFor(field, metaAnnotationsFilter); } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/type/DefaultTypeManager.java b/ebean-core/src/main/java/io/ebeaninternal/server/type/DefaultTypeManager.java index 29685ea99..e4df81962 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/type/DefaultTypeManager.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/type/DefaultTypeManager.java @@ -65,7 +65,7 @@ public final class DefaultTypeManager implements TypeManager { private final Object objectMapper; private final boolean objectMapperPresent; private final boolean postgres; - private final TypeJsonManager jsonManager; + private final ScalarJsonManager jsonManager; private final boolean offlineMigrationGeneration; private final EnumType defaultEnumType; private final DatabasePlatform databasePlatform; @@ -302,7 +302,7 @@ public final class DefaultTypeManager implements TypeManager { if (type.equals(List.class)) { DocPropertyType docType = docType(genericType); if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) { - return ScalarTypeJsonList.typeFor(postgres, dbType, docType, prop.isNullable(), jsonManager.keepSource(prop)); + return ScalarTypeJsonList.typeFor(postgres, dbType, docType, prop.isNullable(), keepSource(prop)); } else { return createJsonObjectMapperType(prop, dbType, docType); } @@ -310,14 +310,14 @@ public final class DefaultTypeManager implements TypeManager { if (type.equals(Set.class)) { DocPropertyType docType = docType(genericType); if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) { - return ScalarTypeJsonSet.typeFor(postgres, dbType, docType, prop.isNullable(), jsonManager.keepSource(prop)); + return ScalarTypeJsonSet.typeFor(postgres, dbType, docType, prop.isNullable(), keepSource(prop)); } else { return createJsonObjectMapperType(prop, dbType, docType); } } if (type.equals(Map.class)) { if (!hasJacksonAnnotations && isMapValueTypeObject(genericType)) { - return ScalarTypeJsonMap.typeFor(postgres, dbType, jsonManager.keepSource(prop)); + return ScalarTypeJsonMap.typeFor(postgres, dbType, keepSource(prop)); } else { return createJsonObjectMapperType(prop, dbType, DocPropertyType.OBJECT); } @@ -331,6 +331,13 @@ public final class DefaultTypeManager implements TypeManager { return createJsonObjectMapperType(prop, dbType, DocPropertyType.OBJECT); } + private boolean keepSource(DeployBeanProperty prop) { + if (prop.getMutationDetection() == MutationDetection.DEFAULT) { + prop.setMutationDetection(jsonManager.mutationDetection()); + } + return prop.getMutationDetection() == MutationDetection.SOURCE; + } + /** * Returns TRUE, if there is any jackson annotation on that property. All jackson annotations * are annotated with the @JacksonAnnotation meta annotation. So detection is easy. @@ -370,11 +377,14 @@ public final class DefaultTypeManager implements TypeManager { } private ScalarType createJsonObjectMapperType(DeployBeanProperty prop, int dbType, DocPropertyType docType) { - Class type = prop.getPropertyType(); if (objectMapper == null) { - throw new IllegalArgumentException("Type [" + type + "] unsupported for @DbJson mapping - Jackson ObjectMapper not present"); + throw new IllegalArgumentException("Unsupported @DbJson mapping - Jackson ObjectMapper not present for " + prop); } - return ScalarTypeJsonObjectMapper.createTypeFor(jsonManager, prop, dbType, docType); + if (MutationDetection.DEFAULT == prop.getMutationDetection()) { + prop.setMutationDetection(jsonManager.mutationDetection()); + } + var req = new ScalarJsonRequest(jsonManager, dbType, docType, prop.getDesc().getBeanType(), prop.getMutationDetection(), prop.getName()); + return ScalarTypeJsonObjectMapper.createTypeFor(req); } /** diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanObtainJackson.java b/ebean-core/src/main/java/io/ebeaninternal/server/type/DeployBeanObtainJackson.java similarity index 69% rename from ebean-core/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanObtainJackson.java rename to ebean-core/src/main/java/io/ebeaninternal/server/type/DeployBeanObtainJackson.java index 9bbfb6007..943422aa3 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanObtainJackson.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/type/DeployBeanObtainJackson.java @@ -1,28 +1,27 @@ -package io.ebeaninternal.server.deploy.meta; +package io.ebeaninternal.server.type; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.introspect.AnnotatedClass; import com.fasterxml.jackson.databind.introspect.AnnotatedClassResolver; -import io.ebean.config.DatabaseConfig; /** * Used to obtain the Jackson AnnotatedClass for a given bean type utlimately to obtain field level Jackson annotations. */ final class DeployBeanObtainJackson { - private final DatabaseConfig config; + private final ObjectMapper objectMapper; private final Class beanType; - DeployBeanObtainJackson(DatabaseConfig config, Class beanType) { - this.config = config; + DeployBeanObtainJackson(ObjectMapper objectMapper, Class beanType) { + this.objectMapper = objectMapper; this.beanType = beanType; } /** * Return the Jackson AnnotatedClass for the given bean type. */ - Object obtain() { - ObjectMapper objectMapper = (ObjectMapper) config.getObjectMapper(); + AnnotatedClass obtain() { JavaType javaType = objectMapper.getTypeFactory().constructType(beanType); return AnnotatedClassResolver.resolve(objectMapper.getDeserializationConfig(), javaType, objectMapper.getDeserializationConfig()); } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonObjectMapper.java b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonObjectMapper.java index 2615e5e28..86f5d9c82 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonObjectMapper.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/type/ScalarTypeJsonObjectMapper.java @@ -6,11 +6,11 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; +import com.fasterxml.jackson.databind.introspect.AnnotatedClass; import com.fasterxml.jackson.databind.introspect.AnnotatedField; import io.ebean.annotation.MutationDetection; import io.ebean.core.type.*; import io.ebean.text.TextException; -import io.ebeaninternal.server.deploy.meta.DeployBeanProperty; import javax.persistence.PersistenceException; import java.io.DataInput; @@ -18,30 +18,43 @@ import java.io.DataOutput; import java.io.IOException; import java.sql.SQLException; import java.sql.Types; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * Supports @DbJson properties using Jackson ObjectMapper. */ final class ScalarTypeJsonObjectMapper { + static final Map, AnnotatedClass> jacksonClasses = new ConcurrentHashMap<>(); + /** * Create and return the appropriate ScalarType. */ - static ScalarType createTypeFor(TypeJsonManager jsonManager, DeployBeanProperty prop, int dbType, DocPropertyType docType) { - AnnotatedField field = (AnnotatedField) prop.getJacksonField(); - MutationDetection mode = prop.getMutationDetection(); - if (mode == MutationDetection.NONE) { - return new NoMutationDetection(jsonManager, field, dbType, docType); - } else if (mode != MutationDetection.DEFAULT) { - return new GenericObject(jsonManager, field, dbType, docType); + static ScalarType createTypeFor(ScalarJsonRequest req) { + AnnotatedClass annotatedClass = annotatedClass(req); + AnnotatedField field = annotatedField(annotatedClass, req); + if (req.mode() == MutationDetection.NONE) { + return new NoMutationDetection(req.manager(), field, req.dbType(), req.docType()); + } else { + return new GenericObject(req.manager(), field, req.dbType(), req.docType()); } - // using the global default MutationDetection mode (defaults to HASH) - final MutationDetection defaultMode = jsonManager.mutationDetection(); - prop.setMutationDetection(defaultMode); - if (MutationDetection.NONE == defaultMode) { - return new NoMutationDetection(jsonManager, field, dbType, docType); + } + + private static AnnotatedField annotatedField(AnnotatedClass annotatedClass, ScalarJsonRequest req) { + for (AnnotatedField field : annotatedClass.fields()) { + if (field.getName().equals(req.name())) { + return field; + } } - return new GenericObject(jsonManager, field, dbType, docType); + throw new IllegalStateException("AnnotatedField not found to match " + req.name()); + } + + private static AnnotatedClass annotatedClass(ScalarJsonRequest req) { + return jacksonClasses.computeIfAbsent(req.beanType(), key -> { + ObjectMapper mapper = (ObjectMapper) req.manager().mapper(); + return new DeployBeanObtainJackson(mapper, req.beanType()).obtain(); + }); } /** @@ -49,7 +62,7 @@ final class ScalarTypeJsonObjectMapper { */ private static final class NoMutationDetection extends Base { - NoMutationDetection(TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) { + NoMutationDetection(ScalarJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) { super(Object.class, jsonManager, field, dbType, docType); } } @@ -61,7 +74,7 @@ final class ScalarTypeJsonObjectMapper { private final boolean jsonb; - GenericObject(TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) { + GenericObject(ScalarJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) { super(Object.class, jsonManager, field, dbType, docType); this.jsonb = "jsonb".equals(pgType); } @@ -126,9 +139,9 @@ final class ScalarTypeJsonObjectMapper { protected final String pgType; private final DocPropertyType docType; - Base(Class cls, TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) { + Base(Class cls, ScalarJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) { super(cls, false, dbType); - this.objectReader = jsonManager.objectMapper(); + this.objectReader = (ObjectMapper)jsonManager.mapper(); this.pgType = jsonManager.postgresType(dbType); this.docType = docType; final JacksonTypeHelper helper = new JacksonTypeHelper(field, objectReader); @@ -223,7 +236,8 @@ final class ScalarTypeJsonObjectMapper { if (value == null) { dataOutput.writeBoolean(false); } else { - ScalarHelp.writeUTF(dataOutput, format(value)); + dataOutput.writeBoolean(true); + dataOutput.writeUTF(format(value)); } } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/type/TypeJsonManager.java b/ebean-core/src/main/java/io/ebeaninternal/server/type/TypeJsonManager.java index 84ceac12b..2b5a61432 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/type/TypeJsonManager.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/type/TypeJsonManager.java @@ -1,44 +1,35 @@ package io.ebeaninternal.server.type; -import com.fasterxml.jackson.databind.ObjectMapper; import io.ebean.ModifyAwareType; import io.ebean.annotation.MutationDetection; import io.ebean.config.dbplatform.DbPlatformType; import io.ebean.core.type.PostgresHelper; -import io.ebeaninternal.server.deploy.meta.DeployBeanProperty; +import io.ebean.core.type.ScalarJsonManager; -final class TypeJsonManager { +final class TypeJsonManager implements ScalarJsonManager { private final boolean postgres; - private final ObjectMapper objectMapper; + private final Object mapper; private final MutationDetection mutationDetection; - TypeJsonManager(boolean postgres, Object objectMapper, MutationDetection mutationDetection) { + TypeJsonManager(boolean postgres, Object mapper, MutationDetection mutationDetection) { this.postgres = postgres; - this.objectMapper = (ObjectMapper) objectMapper; + this.mapper = mapper; this.mutationDetection = mutationDetection; } - MutationDetection mutationDetection() { + @Override + public MutationDetection mutationDetection() { return mutationDetection; } - ObjectMapper objectMapper() { - return objectMapper; + @Override + public Object mapper() { + return mapper; } - boolean keepSource(DeployBeanProperty prop) { - if (prop.getMutationDetection() == MutationDetection.SOURCE) { - return true; - } else if (prop.getMutationDetection() == MutationDetection.DEFAULT) { - prop.setMutationDetection(mutationDetection); - return mutationDetection == MutationDetection.SOURCE; - } else { - return false; - } - } - - String postgresType(int dbType) { + @Override + public String postgresType(int dbType) { if (postgres) { switch (dbType) { case DbPlatformType.JSON: