public <T> ObjectConstructor<T> getConstructor(TypeToken<T> typeToken) { final Type type = typeToken.getType(); Class<? super T> rawType = typeToken.getRawType(); final InstanceCreator<T> creator = (InstanceCreator) this.instanceCreators.get(type); if (creator != null) { return new ObjectConstructor<T>() { public T construct() { return creator.createInstance(type); } }; } ObjectConstructor<T> defaultConstructor = newDefaultConstructor(rawType); if (defaultConstructor != null) { return defaultConstructor; } ObjectConstructor<T> defaultImplementation = newDefaultImplementationConstructor(rawType); if (defaultImplementation != null) { return defaultImplementation; } return newUnsafeAllocator(type, rawType); }
@Test public void testDeserializeAnyBeanWithCreateInstance() { AnyBean expected = new AnyBean("Any Name", 35); expected.setType("Java"); String json = oson.serialize(expected); String jsonExpected = "{\"name\":\"Any Name\",\"age\":35,\"type\":\"Java\"}"; assertEquals(jsonExpected, json); AnyBean result = oson.setClassMappers(AnyBean.class, new ClassMapper() .setConstructor(new InstanceCreator(){ @Override public Object createInstance(Type type) { return new AnyBean(null, 0); } })).deserialize(json, AnyBean.class); assertEquals(expected.toString(), result.toString()); }
private DriftResults loadResultsFromFile(String path) throws IOException { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(path)); Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter( UnivariateFunction.class, new InstanceCreator<PolynomialSplineFunction>() { @Override public PolynomialSplineFunction createInstance(Type type) { return new PolynomialSplineFunction(new double[]{1, 2}, new PolynomialFunction[]{new PolynomialFunction(new double[1])}); } }).create(); return gson.fromJson(reader, DriftResults.class); } finally { if(reader != null) { reader.close(); } } }
@Test(timeout = 1000) public void testInstanceCreatorReturnsSubTypeForTopLevelObject_add1163() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorReturnsSubTypeForTopLevelObject_add1163"); Gson gson = new GsonBuilder().registerTypeAdapter(TestTypes.Base.class, new InstanceCreator<com.google.gson.common.TestTypes.Base>() { public TestTypes.Base createInstance(Type type) { return new TestTypes.Sub(); } }).create(); String json = "{baseName:\'Base\',subName:\'SubRevised\'}"; TestTypes.Base base = gson.fromJson(json, TestTypes.Base.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1647,(base instanceof com.google.gson.common.TestTypes.Sub)); TestTypes.Sub sub = ((TestTypes.Sub)(base)); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1649,"SubRevised",1648,"SubRevised".equals(sub.subName)); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1650,com.google.gson.common.TestTypes.Sub.SUB_NAME); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1651,sub.subName); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
public void testInstanceCreatorReturnsSubTypeForTopLevelObject() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorReturnsSubTypeForTopLevelObject"); Gson gson = new GsonBuilder().registerTypeAdapter(TestTypes.Base.class, new InstanceCreator<com.google.gson.common.TestTypes.Base>() { public TestTypes.Base createInstance(Type type) { return new TestTypes.Sub(); } }).create(); String json = "foo"; TestTypes.Base base = gson.fromJson(json, TestTypes.Base.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1647,(base instanceof com.google.gson.common.TestTypes.Sub)); TestTypes.Sub sub = ((TestTypes.Sub)(base)); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1649,"SubRevised",1648,"SubRevised".equals(sub.subName)); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1650,com.google.gson.common.TestTypes.Sub.SUB_NAME); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1651,sub.subName); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@Test(timeout = 1000) public void testInstanceCreatorReturnsSubTypeForTopLevelObject_remove980() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorReturnsSubTypeForTopLevelObject_remove980"); Gson gson = new GsonBuilder().registerTypeAdapter(TestTypes.Base.class, new InstanceCreator<com.google.gson.common.TestTypes.Base>() { public TestTypes.Base createInstance(Type type) { return new TestTypes.Sub(); } }).create(); String json = "{baseName:\'Base\',subName:\'SubRevised\'}"; TestTypes.Base base = gson.fromJson(json, TestTypes.Base.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1647,(base instanceof com.google.gson.common.TestTypes.Sub)); TestTypes.Sub sub = ((TestTypes.Sub)(base)); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1649,"SubRevised",1648,"SubRevised".equals(sub.subName)); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1650,com.google.gson.common.TestTypes.Sub.SUB_NAME); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1651,sub.subName); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@Test(timeout = 1000) public void testInstanceCreatorForCollectionType_add1155() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType_add1155"); @SuppressWarnings(value = "serial") class SubArrayList<T> extends ArrayList<T> { } InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() { public List<java.lang.String> createInstance(Type type) { return new SubArrayList<java.lang.String>(); } }; Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType(); Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create(); List<java.lang.String> list = gson.fromJson("[\"a\"]", listOfStringType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@Test(timeout = 1000) public void testInstanceCreatorForCollectionType_add1156() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType_add1156"); @SuppressWarnings(value = "serial") class SubArrayList<T> extends ArrayList<T> { } InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() { public List<java.lang.String> createInstance(Type type) { return new SubArrayList<java.lang.String>(); } }; Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType(); Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create(); List<java.lang.String> list = gson.fromJson("[\"a\"]", listOfStringType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@Test(timeout = 1000) public void testInstanceCreatorForCollectionType_add1157() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType_add1157"); @SuppressWarnings(value = "serial") class SubArrayList<T> extends ArrayList<T> { } InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() { public List<java.lang.String> createInstance(Type type) { return new SubArrayList<java.lang.String>(); } }; Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType(); Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create(); List<java.lang.String> list = gson.fromJson("[\"a\"]", listOfStringType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
public void testInstanceCreatorForCollectionType() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType"); @SuppressWarnings(value = "serial") class SubArrayList<T> extends ArrayList<T> { } InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() { public List<java.lang.String> createInstance(Type type) { return new SubArrayList<java.lang.String>(); } }; Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType(); Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create(); List<java.lang.String> list = gson.fromJson("[\"a\"]", listOfStringType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
public void testInstanceCreatorForCollectionType_literalMutation1301() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType_literalMutation1301"); @SuppressWarnings(value = "serial") class SubArrayList<T> extends ArrayList<T> { } InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() { public List<java.lang.String> createInstance(Type type) { return new SubArrayList<java.lang.String>(); } }; Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType(); Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create(); List<java.lang.String> list = gson.fromJson("foo", listOfStringType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@Test(timeout = 1000) public void testInstanceCreatorForCollectionType_remove972() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType_remove972"); @SuppressWarnings(value = "serial") class SubArrayList<T> extends ArrayList<T> { } InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() { public List<java.lang.String> createInstance(Type type) { return new SubArrayList<java.lang.String>(); } }; Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType(); Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create(); List<java.lang.String> list = gson.fromJson("[\"a\"]", listOfStringType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@Test(timeout = 1000) public void testInstanceCreatorForCollectionType_remove973() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType_remove973"); @SuppressWarnings(value = "serial") class SubArrayList<T> extends ArrayList<T> { } InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() { public List<java.lang.String> createInstance(Type type) { return new SubArrayList<java.lang.String>(); } }; Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType(); Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create(); List<java.lang.String> list = gson.fromJson("[\"a\"]", listOfStringType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@Test(timeout = 1000) public void testInstanceCreatorForCollectionType_remove974() { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType_remove974"); @SuppressWarnings(value = "serial") class SubArrayList<T> extends ArrayList<T> { } InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() { public List<java.lang.String> createInstance(Type type) { return new SubArrayList<java.lang.String>(); } }; Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType(); Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create(); List<java.lang.String> list = gson.fromJson("[\"a\"]", listOfStringType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@SuppressWarnings(value = { "unchecked" , "rawtypes" }) @Test(timeout = 1000) public void testInstanceCreatorForParametrizedType_add1158() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForParametrizedType_add1158"); @SuppressWarnings(value = "serial") class SubTreeSet<T> extends TreeSet<T> { } InstanceCreator<java.util.SortedSet> sortedSetCreator = new InstanceCreator<java.util.SortedSet>() { public SortedSet createInstance(Type type) { return new SubTreeSet(); } }; Gson gson = new GsonBuilder().registerTypeAdapter(SortedSet.class, sortedSetCreator).create(); Type sortedSetType = new TypeToken<java.util.SortedSet<java.lang.String>>() { }.getType(); SortedSet<java.lang.String> set = gson.fromJson("[\"a\"]", sortedSetType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1635,set,1634,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1636,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1638,set,1637,set.getClass()); set = gson.fromJson("[\"b\"]", SortedSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1640,set,1639,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1641,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1643,set,1642,set.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@SuppressWarnings(value = { "unchecked" , "rawtypes" }) @Test(timeout = 1000) public void testInstanceCreatorForParametrizedType_add1159() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForParametrizedType_add1159"); @SuppressWarnings(value = "serial") class SubTreeSet<T> extends TreeSet<T> { } InstanceCreator<java.util.SortedSet> sortedSetCreator = new InstanceCreator<java.util.SortedSet>() { public SortedSet createInstance(Type type) { return new SubTreeSet(); } }; Gson gson = new GsonBuilder().registerTypeAdapter(SortedSet.class, sortedSetCreator).create(); Type sortedSetType = new TypeToken<java.util.SortedSet<java.lang.String>>() { }.getType(); SortedSet<java.lang.String> set = gson.fromJson("[\"a\"]", sortedSetType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1635,set,1634,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1636,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1638,set,1637,set.getClass()); set = gson.fromJson("[\"b\"]", SortedSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1640,set,1639,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1641,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1643,set,1642,set.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@SuppressWarnings(value = { "unchecked" , "rawtypes" }) @Test(timeout = 1000) public void testInstanceCreatorForParametrizedType_add1160() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForParametrizedType_add1160"); @SuppressWarnings(value = "serial") class SubTreeSet<T> extends TreeSet<T> { } InstanceCreator<java.util.SortedSet> sortedSetCreator = new InstanceCreator<java.util.SortedSet>() { public SortedSet createInstance(Type type) { return new SubTreeSet(); } }; Gson gson = new GsonBuilder().registerTypeAdapter(SortedSet.class, sortedSetCreator).create(); Type sortedSetType = new TypeToken<java.util.SortedSet<java.lang.String>>() { }.getType(); SortedSet<java.lang.String> set = gson.fromJson("[\"a\"]", sortedSetType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1635,set,1634,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1636,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1638,set,1637,set.getClass()); set = gson.fromJson("[\"b\"]", SortedSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1640,set,1639,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1641,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1643,set,1642,set.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@SuppressWarnings(value = { "foo" , "rawtypes" }) public void testInstanceCreatorForParametrizedType() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForParametrizedType"); @SuppressWarnings(value = "serial") class SubTreeSet<T> extends TreeSet<T> { } InstanceCreator<java.util.SortedSet> sortedSetCreator = new InstanceCreator<java.util.SortedSet>() { public SortedSet createInstance(Type type) { return new SubTreeSet(); } }; Gson gson = new GsonBuilder().registerTypeAdapter(SortedSet.class, sortedSetCreator).create(); Type sortedSetType = new TypeToken<java.util.SortedSet<java.lang.String>>() { }.getType(); SortedSet<java.lang.String> set = gson.fromJson("[\"a\"]", sortedSetType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1635,set,1634,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1636,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1638,set,1637,set.getClass()); set = gson.fromJson("[\"b\"]", SortedSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1640,set,1639,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1641,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1643,set,1642,set.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@SuppressWarnings(value = { "unchecked" , "foo" }) public void testInstanceCreatorForParametrizedType_literalMutation1303() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForParametrizedType_literalMutation1303"); @SuppressWarnings(value = "serial") class SubTreeSet<T> extends TreeSet<T> { } InstanceCreator<java.util.SortedSet> sortedSetCreator = new InstanceCreator<java.util.SortedSet>() { public SortedSet createInstance(Type type) { return new SubTreeSet(); } }; Gson gson = new GsonBuilder().registerTypeAdapter(SortedSet.class, sortedSetCreator).create(); Type sortedSetType = new TypeToken<java.util.SortedSet<java.lang.String>>() { }.getType(); SortedSet<java.lang.String> set = gson.fromJson("[\"a\"]", sortedSetType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1635,set,1634,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1636,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1638,set,1637,set.getClass()); set = gson.fromJson("[\"b\"]", SortedSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1640,set,1639,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1641,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1643,set,1642,set.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@SuppressWarnings(value = { "unchecked" , "rawtypes" }) public void testInstanceCreatorForParametrizedType_literalMutation1304() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForParametrizedType_literalMutation1304"); @SuppressWarnings(value = "serial") class SubTreeSet<T> extends TreeSet<T> { } InstanceCreator<java.util.SortedSet> sortedSetCreator = new InstanceCreator<java.util.SortedSet>() { public SortedSet createInstance(Type type) { return new SubTreeSet(); } }; Gson gson = new GsonBuilder().registerTypeAdapter(SortedSet.class, sortedSetCreator).create(); Type sortedSetType = new TypeToken<java.util.SortedSet<java.lang.String>>() { }.getType(); SortedSet<java.lang.String> set = gson.fromJson("[\"a\"]", sortedSetType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1635,set,1634,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1636,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1638,set,1637,set.getClass()); set = gson.fromJson("[\"b\"]", SortedSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1640,set,1639,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1641,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1643,set,1642,set.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@SuppressWarnings(value = { "unchecked" , "rawtypes" }) public void testInstanceCreatorForParametrizedType_literalMutation1305() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForParametrizedType_literalMutation1305"); @SuppressWarnings(value = "serial") class SubTreeSet<T> extends TreeSet<T> { } InstanceCreator<java.util.SortedSet> sortedSetCreator = new InstanceCreator<java.util.SortedSet>() { public SortedSet createInstance(Type type) { return new SubTreeSet(); } }; Gson gson = new GsonBuilder().registerTypeAdapter(SortedSet.class, sortedSetCreator).create(); Type sortedSetType = new TypeToken<java.util.SortedSet<java.lang.String>>() { }.getType(); SortedSet<java.lang.String> set = gson.fromJson("foo", sortedSetType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1635,set,1634,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1636,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1638,set,1637,set.getClass()); set = gson.fromJson("[\"b\"]", SortedSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1640,set,1639,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1641,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1643,set,1642,set.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@SuppressWarnings(value = { "unchecked" , "rawtypes" }) public void testInstanceCreatorForParametrizedType_literalMutation1306() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForParametrizedType_literalMutation1306"); @SuppressWarnings(value = "serial") class SubTreeSet<T> extends TreeSet<T> { } InstanceCreator<java.util.SortedSet> sortedSetCreator = new InstanceCreator<java.util.SortedSet>() { public SortedSet createInstance(Type type) { return new SubTreeSet(); } }; Gson gson = new GsonBuilder().registerTypeAdapter(SortedSet.class, sortedSetCreator).create(); Type sortedSetType = new TypeToken<java.util.SortedSet<java.lang.String>>() { }.getType(); SortedSet<java.lang.String> set = gson.fromJson("[\"a\"]", sortedSetType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1635,set,1634,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1636,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1638,set,1637,set.getClass()); set = gson.fromJson("foo", SortedSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1640,set,1639,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1641,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1643,set,1642,set.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@SuppressWarnings(value = { "unchecked" , "rawtypes" }) @Test(timeout = 1000) public void testInstanceCreatorForParametrizedType_remove975() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForParametrizedType_remove975"); @SuppressWarnings(value = "serial") class SubTreeSet<T> extends TreeSet<T> { } InstanceCreator<java.util.SortedSet> sortedSetCreator = new InstanceCreator<java.util.SortedSet>() { public SortedSet createInstance(Type type) { return new SubTreeSet(); } }; Gson gson = new GsonBuilder().registerTypeAdapter(SortedSet.class, sortedSetCreator).create(); Type sortedSetType = new TypeToken<java.util.SortedSet<java.lang.String>>() { }.getType(); SortedSet<java.lang.String> set = gson.fromJson("[\"a\"]", sortedSetType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1635,set,1634,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1636,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1638,set,1637,set.getClass()); set = gson.fromJson("[\"b\"]", SortedSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1640,set,1639,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1641,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1643,set,1642,set.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@SuppressWarnings(value = { "unchecked" , "rawtypes" }) @Test(timeout = 1000) public void testInstanceCreatorForParametrizedType_remove976() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForParametrizedType_remove976"); @SuppressWarnings(value = "serial") class SubTreeSet<T> extends TreeSet<T> { } InstanceCreator<java.util.SortedSet> sortedSetCreator = new InstanceCreator<java.util.SortedSet>() { public SortedSet createInstance(Type type) { return new SubTreeSet(); } }; Gson gson = new GsonBuilder().registerTypeAdapter(SortedSet.class, sortedSetCreator).create(); Type sortedSetType = new TypeToken<java.util.SortedSet<java.lang.String>>() { }.getType(); SortedSet<java.lang.String> set = gson.fromJson("[\"a\"]", sortedSetType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1635,set,1634,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1636,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1638,set,1637,set.getClass()); set = gson.fromJson("[\"b\"]", SortedSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1640,set,1639,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1641,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1643,set,1642,set.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
@SuppressWarnings(value = { "unchecked" , "rawtypes" }) @Test(timeout = 1000) public void testInstanceCreatorForParametrizedType_remove977() throws Exception { fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForParametrizedType_remove977"); @SuppressWarnings(value = "serial") class SubTreeSet<T> extends TreeSet<T> { } InstanceCreator<java.util.SortedSet> sortedSetCreator = new InstanceCreator<java.util.SortedSet>() { public SortedSet createInstance(Type type) { return new SubTreeSet(); } }; Gson gson = new GsonBuilder().registerTypeAdapter(SortedSet.class, sortedSetCreator).create(); Type sortedSetType = new TypeToken<java.util.SortedSet<java.lang.String>>() { }.getType(); SortedSet<java.lang.String> set = gson.fromJson("[\"a\"]", sortedSetType); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1635,set,1634,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1636,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1638,set,1637,set.getClass()); set = gson.fromJson("[\"b\"]", SortedSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1640,set,1639,set.first()); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1641,SubTreeSet.class); fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1643,set,1642,set.getClass()); fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread()); }
public void testInstanceCreatorReturnsSubTypeForTopLevelObject() { Gson gson = new GsonBuilder() .registerTypeAdapter(Base.class, new InstanceCreator<Base>() { public Base createInstance(Type type) { return new Sub(); } }) .create(); String json = "{baseName:'Base',subName:'SubRevised'}"; Base base = gson.fromJson(json, Base.class); assertTrue(base instanceof Sub); Sub sub = (Sub) base; assertFalse("SubRevised".equals(sub.subName)); assertEquals(Sub.SUB_NAME, sub.subName); }
@SuppressWarnings("unchecked") public void testInstanceCreatorForParametrizedType() throws Exception { class SubTreeSet<T> extends TreeSet<T> {} InstanceCreator<SortedSet> sortedSetCreator = new InstanceCreator<SortedSet>() { public SortedSet createInstance(Type type) { return new SubTreeSet(); } }; Gson gson = new GsonBuilder() .registerTypeAdapter(SortedSet.class, sortedSetCreator) .create(); Type sortedSetType = new TypeToken<SortedSet<String>>() {}.getType(); SortedSet<String> set = gson.fromJson("[\"a\"]", sortedSetType); assertEquals(set.first(), "a"); assertEquals(SubTreeSet.class, set.getClass()); set = gson.fromJson("[\"b\"]", SortedSet.class); assertEquals(set.first(), "b"); assertEquals(SubTreeSet.class, set.getClass()); }
/** Loads the {@link Configuration} from the given file and sets it to {@code Configuration.i}. */ public void load() throws IOException { String fileName = System.getProperty("ConfigFile"); if (fileName == null) { // just try whatever in the current directory fileName = "config.json"; } log.info("Loading config from: %s", fileName); Gson gson = new GsonBuilder() .registerTypeAdapter(Configuration.class, (InstanceCreator<Configuration>) type -> i) .create(); JsonReader jsonReader = new JsonReader(new FileReader(fileName)); jsonReader.setLenient(true); // allow comments (and a few other things) gson.fromJson(jsonReader, Configuration.class); }
public <T> ObjectConstructor<T> get(TypeToken<T> typeToken) { final Type type = typeToken.getType(); Class<? super T> rawType = typeToken.getRawType(); final InstanceCreator<T> typeCreator = (InstanceCreator) this.instanceCreators.get(type); if (typeCreator != null) { return new ObjectConstructor<T>() { public T construct() { return typeCreator.createInstance(type); } }; } final InstanceCreator<T> rawTypeCreator = (InstanceCreator) this.instanceCreators.get (rawType); if (rawTypeCreator != null) { return new ObjectConstructor<T>() { public T construct() { return rawTypeCreator.createInstance(type); } }; } ObjectConstructor<T> defaultConstructor = newDefaultConstructor(rawType); if (defaultConstructor != null) { return defaultConstructor; } ObjectConstructor<T> defaultImplementation = newDefaultImplementationConstructor(type, rawType); if (defaultImplementation != null) { return defaultImplementation; } return newUnsafeAllocator(type, rawType); }
public GraphAdapterBuilder addType(Type type, InstanceCreator<?> instanceCreator) { if (type == null || instanceCreator == null) { throw new NullPointerException(); } instanceCreators.put(type, instanceCreator); return this; }
public void registerOn(GsonBuilder gsonBuilder) { Factory factory = new Factory(instanceCreators); gsonBuilder.registerTypeAdapterFactory(factory); for (Map.Entry<Type, InstanceCreator<?>> entry : instanceCreators.entrySet()) { gsonBuilder.registerTypeAdapter(entry.getKey(), factory); } }
/** * Hook for the graph adapter to get a reference to a deserialized value * before that value is fully populated. This is useful to deserialize * values that directly or indirectly reference themselves: we can hand * out an instance before read() returns. * * <p> * Gson should only ever call this method when we're expecting it to; * that is only when we've called back into Gson to deserialize a tree. */ @SuppressWarnings("unchecked") public Object createInstance(Type type) { Graph graph = graphThreadLocal.get(); if (graph == null || graph.nextCreate == null) { throw new IllegalStateException("Unexpected call to createInstance() for " + type); } InstanceCreator<?> creator = instanceCreators.get(type); Object result = creator.createInstance(type); graph.nextCreate.value = result; graph.nextCreate = null; return result; }
private InstanceCreator getTypeAdapter(Class type) { ClassMapper classMapper = getClassMapper(type); if (classMapper == null || classMapper.ignore()) { return null; } return classMapper.constructor; }
public void testMapSubclassDeserialization() { Gson gson = new GsonBuilder().registerTypeAdapter(MyMap.class, new InstanceCreator<MyMap>() { public MyMap createInstance(Type type) { return new MyMap(); } }).create(); String json = "{\"a\":1,\"b\":2}"; MyMap map = oson.fromJson(json, MyMap.class); assertEquals(1, map.get("a")); assertEquals(2, map.get("b")); }
public void testInstanceCreatorReturnsBaseType() { Gson gson = new GsonBuilder() .registerTypeAdapter(Base.class, new InstanceCreator<Base>() { @Override public Base createInstance(Type type) { return new Base(); } }) .create(); String json = "{baseName:'BaseRevised',subName:'Sub'}"; Base base = oson.fromJson(json, Base.class); assertEquals("BaseRevised", base.baseName); }
public void testInstanceCreatorForCollectionType() { @SuppressWarnings("serial") class SubArrayList<T> extends ArrayList<T> {} InstanceCreator<List<String>> listCreator = new InstanceCreator<List<String>>() { @Override public List<String> createInstance(Type type) { return new SubArrayList<String>(); } }; Type listOfStringType = new TypeToken<List<String>>() {}.getType(); Gson gson = new GsonBuilder() .registerTypeAdapter(listOfStringType, listCreator) .create(); List<String> list = oson.fromJson("[\"a\"]", SubArrayList.class); //assertEquals(SubArrayList.class, list.getClass()); }
public void testInnerClassDeserialization() { final Parent p = new Parent(); Gson gson = new GsonBuilder().registerTypeAdapter( Parent.Child.class, new InstanceCreator<Parent.Child>() { public Parent.Child createInstance(Type type) { return p.new Child(); } }).create(); String json = "{'value2':3}"; Parent.Child c = oson.setDefaultValue(Parent.Child.class, p.new Child()).fromJson(json, Parent.Child.class); assertEquals(3, c.value2); }
@Override public Response<List<CompletionEntry>> parseResponse(JsonObject json) { String fileName = super.getArguments().getFile(); int line = super.getArguments().getLine(); int offset = super.getArguments().getOffset(); Gson gson = new GsonBuilder() .registerTypeAdapter(CompletionEntry.class, new InstanceCreator<CompletionEntry>() { @Override public CompletionEntry createInstance(Type type) { return factory.create(matcherProvider.getMatcher(), fileName, line, offset, client); } }).create(); return gson.fromJson(json, CompletionsResponse.class); }
@Override public Response<List<NavigationBarItem>> parseResponse(JsonObject json) { Gson gson = GsonHelper.DEFAULT_GSON; if (positionProvider != null) { gson = new GsonBuilder().registerTypeAdapter(Location.class, new InstanceCreator<Location>() { @Override public Location createInstance(Type type) { return new Location(positionProvider); } }).create(); } return gson.fromJson(json, NavBarResponse.class); }
@Override public Response<NavigationBarItem> parseResponse(JsonObject json) { Gson gson = GsonHelper.DEFAULT_GSON; if (positionProvider != null) { gson = new GsonBuilder().registerTypeAdapter(Location.class, new InstanceCreator<Location>() { @Override public Location createInstance(Type type) { return new Location(positionProvider); } }).create(); } return gson.fromJson(json, NavTreeResponse.class); }