Skip to content

[DE-216] field annotations #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/main/java/com/arangodb/ArangoDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,7 @@ public Builder setDeserializer(final ArangoDeserializer deserializer) {
*
* @param serialization custom serializer/deserializer
* @return {@link ArangoDB.Builder}
* @deprecated Use {@link com.arangodb.mapping.ArangoJack} instead and register custom serializers and deserializers by implementing {@link com.fasterxml.jackson.databind.JsonSerializer} and {@link com.fasterxml.jackson.databind.JsonDeserializer}.
* @see <a href="https://www.arangodb.com/docs/stable/drivers/java-reference-serialization.html#custom-serializer">Reference Documentation</a>
*/
@Deprecated
public Builder serializer(final ArangoSerialization serialization) {
setSerializer(serialization);
return this;
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/arangodb/async/ArangoDBAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -846,10 +846,7 @@ public Builder setDeserializer(final ArangoDeserializer deserializer) {
*
* @param serialization custom serializer/deserializer
* @return {@link ArangoDBAsync.Builder}
* @deprecated Use {@link com.arangodb.mapping.ArangoJack} instead and register custom serializers and deserializers by implementing {@link com.fasterxml.jackson.databind.JsonSerializer} and {@link com.fasterxml.jackson.databind.JsonDeserializer}.
* @see <a href="https://www.arangodb.com/docs/stable/drivers/java-reference-serialization.html#custom-serializer">Reference Documentation</a>
*/
@Deprecated
public Builder serializer(final ArangoSerialization serialization) {
setSerializer(serialization);
return this;
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/arangodb/entity/BaseDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

package com.arangodb.entity;

import com.arangodb.entity.DocumentField.Type;
import com.arangodb.internal.DocumentFields;

import java.io.Serializable;
import java.util.HashMap;
Expand All @@ -33,11 +33,11 @@ public class BaseDocument implements Serializable {

private static final long serialVersionUID = -1824742667228719116L;

@DocumentField(Type.ID)
@Id
protected String id;
@DocumentField(Type.KEY)
@Key
protected String key;
@DocumentField(Type.REV)
@Rev
protected String revision;
protected Map<String, Object> properties;

Expand All @@ -53,15 +53,15 @@ public BaseDocument(final String key) {

public BaseDocument(final Map<String, Object> properties) {
this();
final Object tmpId = properties.remove(DocumentField.Type.ID.getSerializeName());
final Object tmpId = properties.remove(DocumentFields.ID);
if (tmpId != null) {
id = tmpId.toString();
}
final Object tmpKey = properties.remove(DocumentField.Type.KEY.getSerializeName());
final Object tmpKey = properties.remove(DocumentFields.KEY);
if (tmpKey != null) {
key = tmpKey.toString();
}
final Object tmpRev = properties.remove(DocumentField.Type.REV.getSerializeName());
final Object tmpRev = properties.remove(DocumentFields.REV);
if (tmpRev != null) {
revision = tmpRev.toString();
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/arangodb/entity/BaseEdgeDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

package com.arangodb.entity;

import com.arangodb.entity.DocumentField.Type;
import com.arangodb.internal.DocumentFields;

import java.util.Map;

Expand All @@ -31,9 +31,9 @@ public class BaseEdgeDocument extends BaseDocument {

private static final long serialVersionUID = 6904923804449368783L;

@DocumentField(Type.FROM)
@From
private String from;
@DocumentField(Type.TO)
@To
private String to;

public BaseEdgeDocument() {
Expand All @@ -54,11 +54,11 @@ public BaseEdgeDocument(final String key, final String from, final String to) {

public BaseEdgeDocument(final Map<String, Object> properties) {
super(properties);
final Object tmpFrom = properties.remove(DocumentField.Type.FROM.getSerializeName());
final Object tmpFrom = properties.remove(DocumentFields.FROM);
if (tmpFrom != null) {
from = tmpFrom.toString();
}
final Object tmpTo = properties.remove(DocumentField.Type.TO.getSerializeName());
final Object tmpTo = properties.remove(DocumentFields.TO);
if (tmpTo != null) {
to = tmpTo.toString();
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/arangodb/entity/DocumentEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@

package com.arangodb.entity;

import com.arangodb.entity.DocumentField.Type;

/**
* @author Mark Vollmary
*/
public class DocumentEntity implements Entity {

@DocumentField(Type.KEY)
@Key
private String key;
@DocumentField(Type.ID)
@Id
private String id;
@DocumentField(Type.REV)
@Rev
private String rev;

public DocumentEntity() {
Expand Down
34 changes: 33 additions & 1 deletion src/main/java/com/arangodb/entity/DocumentField.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,45 @@

/**
* @author Mark Vollmary
* @deprecated Use {@link Id}, {@link Key}, {@link Rev}, {@link From} or {@link To} instead.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
@Deprecated
public @interface DocumentField {

@Deprecated
enum Type {
ID("_id"), KEY("_key"), REV("_rev"), FROM("_from"), TO("_to");

/**
* @deprecated Use {@link Id} instead.
*/
@Deprecated
ID("_id"),

/**
* @deprecated Use {@link Key} instead.
*/
@Deprecated
KEY("_key"),

/**
* @deprecated Use {@link Rev} instead.
*/
@Deprecated
REV("_rev"),

/**
* @deprecated Use {@link From} instead.
*/
@Deprecated
FROM("_from"),

/**
* @deprecated Use {@link To} instead.
*/
@Deprecated
TO("_to");

private final String serializeName;

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/arangodb/entity/From.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.arangodb.entity;

import com.arangodb.internal.DocumentFields;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Michele Rastelli
*/
// TODO: in v7 add targets ElementType.METHOD and ElementType.PARAMETER
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonProperty(DocumentFields.FROM)
@JsonInclude(JsonInclude.Include.NON_NULL)
public @interface From {
}
23 changes: 23 additions & 0 deletions src/main/java/com/arangodb/entity/Id.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.arangodb.entity;

import com.arangodb.internal.DocumentFields;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Michele Rastelli
*/
// TODO: in v7 add targets ElementType.METHOD and ElementType.PARAMETER
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonProperty(DocumentFields.ID)
@JsonInclude(JsonInclude.Include.NON_NULL)
public @interface Id {
}
23 changes: 23 additions & 0 deletions src/main/java/com/arangodb/entity/Key.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.arangodb.entity;

import com.arangodb.internal.DocumentFields;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Michele Rastelli
*/
// TODO: in v7 add targets ElementType.METHOD and ElementType.PARAMETER
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonProperty(DocumentFields.KEY)
@JsonInclude(JsonInclude.Include.NON_NULL)
public @interface Key {
}
23 changes: 23 additions & 0 deletions src/main/java/com/arangodb/entity/Rev.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.arangodb.entity;

import com.arangodb.internal.DocumentFields;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Michele Rastelli
*/
// TODO: in v7 add targets ElementType.METHOD and ElementType.PARAMETER
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonProperty(DocumentFields.REV)
@JsonInclude(JsonInclude.Include.NON_NULL)
public @interface Rev {
}
23 changes: 23 additions & 0 deletions src/main/java/com/arangodb/entity/To.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.arangodb.entity;

import com.arangodb.internal.DocumentFields;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Michele Rastelli
*/
// TODO: in v7 add targets ElementType.METHOD and ElementType.PARAMETER
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonProperty(DocumentFields.TO)
@JsonInclude(JsonInclude.Include.NON_NULL)
public @interface To {
}
59 changes: 37 additions & 22 deletions src/main/java/com/arangodb/internal/DocumentCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
package com.arangodb.internal;

import com.arangodb.ArangoDBException;
import com.arangodb.entity.DocumentField;
import com.arangodb.entity.DocumentField.Type;
import com.arangodb.entity.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
Expand All @@ -34,17 +34,17 @@
*/
public class DocumentCache {

private final Map<Class<?>, Map<DocumentField.Type, Field>> cache;
private final Map<Class<?>, Map<String, Field>> cache;

public DocumentCache() {
super();
cache = new HashMap<>();
}

public void setValues(final Object doc, final Map<DocumentField.Type, String> values) throws ArangoDBException {
public void setValues(final Object doc, final Map<String, String> values) throws ArangoDBException {
try {
final Map<DocumentField.Type, Field> fields = getFields(doc.getClass());
for (final Entry<DocumentField.Type, String> value : values.entrySet()) {
final Map<String, Field> fields = getFields(doc.getClass());
for (final Entry<String, String> value : values.entrySet()) {
final Field field = fields.get(value.getKey());
if (field != null) {
field.set(doc, value.getValue());
Expand All @@ -55,8 +55,8 @@ public void setValues(final Object doc, final Map<DocumentField.Type, String> va
}
}

private Map<DocumentField.Type, Field> getFields(final Class<?> clazz) {
Map<DocumentField.Type, Field> fields = new HashMap<>();
private Map<String, Field> getFields(final Class<?> clazz) {
Map<String, Field> fields = new HashMap<>();
if (!isTypeRestricted(clazz)) {
fields = cache.get(clazz);
if (fields == null) {
Expand All @@ -71,11 +71,10 @@ private boolean isTypeRestricted(final Class<?> type) {
return Map.class.isAssignableFrom(type) || Collection.class.isAssignableFrom(type);
}

private Map<DocumentField.Type, Field> createFields(final Class<?> clazz) {
final Map<DocumentField.Type, Field> fields = new HashMap<>();
private Map<String, Field> createFields(final Class<?> clazz) {
final Map<String, Field> fields = new HashMap<>();
Class<?> tmp = clazz;
final Collection<DocumentField.Type> values = new ArrayList<>(
Arrays.asList(DocumentField.Type.values()));
final Collection<String> values = new ArrayList<>(DocumentFields.values());
while (tmp != null && tmp != Object.class && values.size() > 0) {
final Field[] declaredFields = tmp.getDeclaredFields();
for (int i = 0; i < declaredFields.length && values.size() > 0; i++) {
Expand All @@ -87,17 +86,33 @@ private Map<DocumentField.Type, Field> createFields(final Class<?> clazz) {
}

private void findAnnotation(
final Collection<Type> values,
final Map<DocumentField.Type, Field> fields,
final Collection<String> values,
final Map<String, Field> fields,
final Field field) {
final DocumentField annotation = field.getAnnotation(DocumentField.class);
if (annotation != null && !field.isSynthetic() && !Modifier.isStatic(field.getModifiers())
&& String.class.isAssignableFrom(field.getType())) {
final Type value = annotation.value();
if (values.contains(value)) {
field.setAccessible(true);
fields.put(value, field);
values.remove(value);

for (Annotation annotation : field.getAnnotations()) {
if (annotation != null && !field.isSynthetic() && !Modifier.isStatic(field.getModifiers())
&& String.class.isAssignableFrom(field.getType())) {
String value = null;
if (annotation instanceof DocumentField) {
value = ((DocumentField) annotation).value().getSerializeName();
} else if (annotation instanceof Id) {
value = DocumentFields.ID;
} else if (annotation instanceof Key) {
value = DocumentFields.KEY;
} else if (annotation instanceof Rev) {
value = DocumentFields.REV;
} else if (annotation instanceof From) {
value = DocumentFields.FROM;
} else if (annotation instanceof To) {
value = DocumentFields.TO;
}

if (values.contains(value)) {
field.setAccessible(true);
fields.put(value, field);
values.remove(value);
}
}
}
}
Expand Down
Loading