Skip to content

Commit

Permalink
fix #133
Browse files Browse the repository at this point in the history
  • Loading branch information
anidotnet committed Feb 18, 2019
1 parent 336df6a commit 3b62983
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public <T> Document asDocument(T object) {
public <T> T asObject(Document document, Class<T> type) {
if (Mappable.class.isAssignableFrom(type)) {
T item = newInstance(type);
((Mappable) item).read(this, document);
if (item != null) {
((Mappable) item).read(this, document);
}
return item;
}
return asObjectInternal(document, type);
Expand Down
26 changes: 18 additions & 8 deletions nitrite/src/main/java/org/dizitart/no2/util/ObjectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import org.dizitart.no2.exceptions.NotIdentifiableException;
import org.dizitart.no2.mapper.NitriteMapper;
import org.dizitart.no2.objects.*;
import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;
import org.objenesis.instantiator.ObjectInstantiator;

import java.lang.reflect.Field;
import java.util.*;
Expand All @@ -36,13 +38,9 @@
import static org.dizitart.no2.exceptions.ErrorCodes.*;
import static org.dizitart.no2.exceptions.ErrorMessage.*;
import static org.dizitart.no2.objects.filters.ObjectFilters.eq;
import static org.dizitart.no2.util.ReflectionUtils.findAnnotations;
import static org.dizitart.no2.util.ReflectionUtils.getField;
import static org.dizitart.no2.util.ReflectionUtils.getFieldsUpto;
import static org.dizitart.no2.util.ReflectionUtils.*;
import static org.dizitart.no2.util.StringUtils.isNullOrEmpty;
import static org.dizitart.no2.util.ValidationUtils.notEmpty;
import static org.dizitart.no2.util.ValidationUtils.notNull;
import static org.dizitart.no2.util.ValidationUtils.validateObjectIndexField;
import static org.dizitart.no2.util.ValidationUtils.*;

/**
* A utility class for {@link Object}.
Expand All @@ -53,6 +51,9 @@
@UtilityClass
@Slf4j
public class ObjectUtils {
private static Map<String, ObjectInstantiator> constructorCache = new HashMap<>();
private static Objenesis objenesis = new ObjenesisStd();

/**
* Generates the name of an {@link org.dizitart.no2.objects.ObjectRepository}.
*
Expand Down Expand Up @@ -215,11 +216,20 @@ public static boolean isKeyedObjectStore(String collectionName) {
}
}

@SuppressWarnings("unchecked")
public static <T> T newInstance(Class<T> type) {
try {
return type.newInstance();
String clazz = type.getName();
ObjectInstantiator instantiator = constructorCache.get(clazz);
if (instantiator == null) {
instantiator = objenesis.getInstantiatorOf(type);
constructorCache.put(clazz, instantiator);
}

return (T) instantiator.newInstance();
} catch (Exception e) {
return new ObjenesisStd().newInstance(type);
log.error("Error while creating instance of " + type.getName(), e);
return null;
}
}

Expand Down

0 comments on commit 3b62983

Please sign in to comment.