Extract ebean-jackson-mapper module - initial step

This commit is contained in:
Rob Bygrave committed 2022-09-08 21:39:00 +12:00
1 parent c53ca6c16e
commit d5f753d110
9 files changed
+163 -79

No files matched your search

@@ -152,7 +152,6 @@ public class DeployBeanDescriptor<T> {
private DocStoreMode docStoreDelete;
private DeployBeanProperty idProperty;
private TableJoin primaryKeyJoin;
private Object jacksonAnnotatedClass;
/**
* Construct the BeanDescriptor.
@@ -1130,14 +1129,4 @@ public class DeployBeanDescriptor<T> {
}
}
/**
* 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;
}
}
@@ -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<Class<?>> metaAnnotationsFilter) {
metaAnnotations = AnnotationUtil.metaFindAllFor(field, metaAnnotationsFilter);
}
@@ -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 &#64;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);
}
/**
@@ -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<T> {
private final DatabaseConfig config;
private final ObjectMapper objectMapper;
private final Class<T> beanType;
DeployBeanObtainJackson(DatabaseConfig config, Class<T> beanType) {
this.config = config;
DeployBeanObtainJackson(ObjectMapper objectMapper, Class<T> 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());
}
@@ -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<Class<?>, 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<Object> {
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<T> cls, TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
Base(Class<T> 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));
}
}
}
@@ -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: