HCOLL-250 providing better error message for failure and attempts to create using reflection

This commit is contained in:
Rob Austin
2014-12-15 12:05:19 +00:00
parent 84a77e89f5
commit 76cbbc19ff
4 changed files with 70 additions and 9 deletions
@@ -59,7 +59,7 @@ final class CloseablesManager implements Closeable {
try {
closeable.close();
} catch (IOException e) {
LOG.error("", e);
LOG.debug("", e);
ex = e;
}
}
@@ -663,9 +663,32 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
}
static <T> T newInstance(Class<T> interfaceClass, boolean isKey) {
return isKey ? DataValueClasses.newInstance(interfaceClass) :
DataValueClasses.newDirectInstance(interfaceClass);
static <T> T newInstance(Class<T> aClass, boolean isKey) {
try {
return isKey ? DataValueClasses.newInstance(aClass) :
DataValueClasses.newDirectInstance(aClass);
} catch (Exception e) {
if (aClass.isInterface())
throw new IllegalStateException("It not possible to create a instance from " +
"interface=" + aClass.getSimpleName() + " we recommend you create an " +
"instance in the usual way.");
try {
return (T) aClass.newInstance();
} catch (Exception e1) {
throw new IllegalStateException("It has not been possible to create a instance " +
"of class=" + aClass.getSimpleName() +
", Note : its more efficient if your chronicle map is configured with " +
"interface key " +
"and value types rather than classes, as this method is able to use " +
"interfaces to generate off heap proxies that point straight at your data. " +
"In this case you have used a class and chronicle is unable to create an " +
"instance of this class has it does not have a default constructor. " +
"If your class is mutable, we " +
"recommend you create and instance of your class=" + aClass.getSimpleName() +
" in the usual way, rather than using this method.");
}
}
}
private XStreamConverter xStreamConverter;
@@ -332,8 +332,8 @@ public class CHMUseCasesTest {
}
}
@Ignore("HCOLL-250 map.newKeyInstance() throws NPE for simple types like Long")
@Test
@Test(expected = IllegalStateException.class)
public void testLongNewKeyValueInstance() {
try (ChronicleMap map = ChronicleMapBuilder
.of(Long.class, Long.class)
@@ -344,6 +344,42 @@ public class CHMUseCasesTest {
}
}
@Test(expected = IllegalStateException.class)
public void testNewKeyValueInstanceWithMapType() {
try (ChronicleMap map = ChronicleMapBuilder
.of(Map.class, Map.class)
.create()) {
map.newKeyInstance();
map.newValueInstance();
}
}
@Test
public void testNewKeyValueInstanceWithHahMapType() {
try (ChronicleMap map = ChronicleMapBuilder
.of(HashMap.class, HashMap.class)
.create()) {
map.newKeyInstance();
map.newValueInstance();
}
}
@Test(expected = IllegalStateException.class)
public void testNewKeyValueInstanceWithByteArray() {
try (ChronicleMap map = ChronicleMapBuilder
.of(byte[].class, byte[].class)
.create()) {
map.newKeyInstance();
map.newValueInstance();
}
}
/**
* StringValue represents any bean which contains a String Value
*/