Skip to content
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

Fix Map/List/Set deserialization compatibility with 3.0+ API. #79

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
import com.datastax.driver.mapping.option.WriteOptions;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.reflect.TypeToken;

import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.datastax.driver.core.querybuilder.QueryBuilder.*;
Expand Down Expand Up @@ -452,7 +454,7 @@ public static Object getValueFromRow(Row row, EntityFieldMetaData field) {
if (value == null) {
value = new HashMap<Object, Object>();
}
Map<Object, Object> data = row.getMap(field.getColumnName(), Object.class, Object.class);
Map<?, ?> data = row.getMap(field.getColumnName(), TypeToken.of(field.getTypeParameters()[0]), TypeToken.of(field.getTypeParameters()[1]));
if (!data.isEmpty()) {
((Map<Object, Object>) value).putAll(data);
}
Expand All @@ -461,7 +463,7 @@ public static Object getValueFromRow(Row row, EntityFieldMetaData field) {
if (value == null) {
value = new ArrayList<Object>();
}
List<Object> lst = row.getList(field.getColumnName(), Object.class);
List<?> lst = row.getList(field.getColumnName(), TypeToken.of(field.getTypeParameters()[0]));
if (!lst.isEmpty()) {
((List<Object>) value).addAll(lst);
}
Expand All @@ -470,7 +472,7 @@ public static Object getValueFromRow(Row row, EntityFieldMetaData field) {
if (value == null) {
value = new HashSet<Object>();
}
Set<Object> set = row.getSet(field.getColumnName(), Object.class);
Set<?> set = row.getSet(field.getColumnName(), TypeToken.of(field.getTypeParameters()[0]));
if (!set.isEmpty()) {
((Set<Object>) value).addAll(set);
}
Expand All @@ -479,7 +481,7 @@ public static Object getValueFromRow(Row row, EntityFieldMetaData field) {
break;
}
} catch (Exception ex) {
// swallow any mapping discrepancies.
log.log(Level.WARNING, "Failed to read field [" + field.getColumnName() + "]", ex);
}
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package com.datastax.driver.mapping.meta;

import java.lang.reflect.Field;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.logging.Logger;

import com.datastax.driver.core.DataType;
Expand All @@ -27,78 +29,86 @@
*/
public class EntityFieldMetaData {
private static final Logger log = Logger.getLogger(EntityFieldMetaData.class.getName());
private Field field;
private Method getter;
private Method setter;
private final Field field;
private final Method getter;
private final Method setter;
private String genericDef;
private Class<?> collectionType;

private Name dataType;
private String columnName;
private final Name dataType;
private final String columnName;
private boolean isPrimary;
private boolean isPartition;
private boolean isStatic;
private boolean autoGenerate;
public EntityFieldMetaData(Field field, DataType.Name dataType, Method getter, Method setter, String columnName) {

public EntityFieldMetaData(final Field field, final DataType.Name dataType, final Method getter, final Method setter, final String columnName) {
this.field = field;
this.getter = getter;
this.setter = setter;
this.dataType = dataType;
this.columnName = columnName;
}

public Class<?> getType() {
return field.getType();
}


public Type[] getTypeParameters() {
Type genericType = field.getGenericType();
if (!(genericType instanceof ParameterizedType)) {
return null;
}
return ((ParameterizedType) genericType).getActualTypeArguments();
}

public DataType.Name getDataType() {
return dataType;
}

public String getName() {
return field.getName();
}

/**
* get the value from given object using reflection on public getter method
* @param entity - object instance the value will be retrieved from
*/
public <E> Object getValue(E entity) {
*/
public <E> Object getValue(final E entity) {
try {
Object ret = getter.invoke(entity, new Object[]{});
Object ret = getter.invoke(entity, new Object[] {});
if (field.getType().isEnum()) {
return ((Enum<?>)ret).name();
return ((Enum<?>) ret).name();
}
return ret;
return ret;
} catch (Exception e) {
log.info("Can't get value for obj:"+entity+", method:"+getter.getName());
log.info("Can't get value for obj:" + entity + ", method:" + getter.getName());
}
return null;
}

/**
* set the value on given object using reflection on public setter method
* @param entity - object instance the value will be set to
* @param value
*/
public <E> void setValue(E entity, Object value) {
public <E> void setValue(final E entity, final Object value) {
try {
if (field.getType().isEnum()) {
Object eval = Enum.valueOf((Class<Enum>)field.getType(), (String)value);
setter.invoke(entity, new Object[]{eval});
Object eval = Enum.valueOf((Class<Enum>) field.getType(), (String) value);
setter.invoke(entity, new Object[] { eval });
} else {
setter.invoke(entity, new Object[]{value});
setter.invoke(entity, new Object[] { value });
}
} catch (Exception e) {
log.info("Can't set value for obj:"+entity+", method:"+setter.getName());
log.info("Can't set value for obj:" + entity + ", method:" + setter.getName());
}
}

/**
* String representation of generic modifier on the field
*
* @return
*
* @return
*/
public String getGenericDef() {
return genericDef;
Expand All @@ -109,13 +119,13 @@ public String getGenericDef() {
* samples: list<text>, set<float>, map<bigint>
* @param genericDef
*/
public void setGenericDef(String genericDef) {
public void setGenericDef(final String genericDef) {
this.genericDef = genericDef;
}

/**
* indicates if the field has generic modifier
*
*
*/
public boolean isGenericType() {
return genericDef != null;
Expand All @@ -133,43 +143,43 @@ public boolean isPrimary() {
return isPrimary;
}

public void setPrimary(boolean isPrimary) {
public void setPrimary(final boolean isPrimary) {
this.isPrimary = isPrimary;
}

public boolean isPartition() {
return isPartition;
}

public void setPartition(boolean isPartition) {
public void setPartition(final boolean isPartition) {
this.isPartition = isPartition;
}

public Class<?> getCollectionType() {
return collectionType;
}

public void setCollectionType(Class<?> collectionType) {
public void setCollectionType(final Class<?> collectionType) {
this.collectionType = collectionType;
}

public boolean hasCollectionType() {
return collectionType != null;
}

public boolean isStatic() {
return isStatic;
}
public boolean isStatic() {
return isStatic;
}

public void setStatic(boolean isStatic) {
this.isStatic = isStatic;
}
public void setStatic(final boolean isStatic) {
this.isStatic = isStatic;
}

public boolean isAutoGenerate() {
return autoGenerate;
}
public boolean isAutoGenerate() {
return autoGenerate;
}

public void setAutoGenerate(boolean autoGenerate) {
this.autoGenerate = autoGenerate;
}
public void setAutoGenerate(final boolean autoGenerate) {
this.autoGenerate = autoGenerate;
}
}