google-code-export / twig-persist

Automatically exported from code.google.com/p/twig-persist
1 stars 1 forks source link

Issue with Converting EnumSets in CollectionConverter #25

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
There seems to be a problem with the CollectionConverter.
Attached is the failing test case.

I get an IllegalStateException.

See the stack trace:

java.lang.IllegalStateException: Problem translating field private 
com.twig.example.BirthInformation com.twig.example.Person.birthInformation
    at 
com.vercer.engine.persist.translator.ObjectFieldTranslator.activate(ObjectF
ieldTranslator.java:112)
    at 
com.vercer.engine.persist.standard.StrategyObjectDatastore$3.activate(Strat
egyObjectDatastore.java:220)
    at 
com.vercer.engine.persist.translator.ObjectFieldTranslator.propertiesToType
safe(ObjectFieldTranslator.java:57)
    at 
com.vercer.engine.persist.standard.AbstractStatelessObjectDatastore.toTypes
afe(AbstractStatelessObjectDatastore.java:272)
    at 
com.vercer.engine.persist.standard.StrategyObjectDatastore.toTypesafe(Strat
egyObjectDatastore.java:429)
    at 
com.vercer.engine.persist.standard.AbstractStatelessObjectDatastore.keyToIn
stance(AbstractStatelessObjectDatastore.java:310)
    at 
com.vercer.engine.persist.standard.StrategyObjectDatastore.keyToInstance(St
rategyObjectDatastore.java:449)
    at 
com.vercer.engine.persist.standard.AbstractStatelessObjectDatastore.load(Ab
stractStatelessObjectDatastore.java:296)
    at 
com.vercer.engine.persist.standard.AbstractStatelessObjectDatastore.interna
lLoad(AbstractStatelessObjectDatastore.java:232)
    at 
com.vercer.engine.persist.standard.StrategyObjectDatastore.load(StrategyObj
ectDatastore.java:526)
    at 
com.vercer.engine.persist.standard.AbstractStatelessObjectDatastore.load(Ab
stractStatelessObjectDatastore.java:199)
    at com.wulfpak.test.TestPerson.testPerson(TestPerson.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:3
9)
    at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImp
l.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at junit.framework.TestCase.runTest(TestCase.java:164)
    at junit.framework.TestCase.runBare(TestCase.java:130)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.runTest(TestSuite.java:230)
    at junit.framework.TestSuite.run(TestSuite.java:225)
    at 
org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:8
3)
    at 
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestRe
ference.java:46)
    at 
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:
38)
    at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestR
unner.java:467)
    at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestR
unner.java:683)
    at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner
.java:390)
    at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunne
r.java:197)
Caused by: java.lang.IllegalArgumentException: Unsupported Collection type 
java.util.EnumSet<com.twig.example.ProofDocumentTypeCode>. Try declaring 
the interface instead of the concrete collection type.
    at 
com.vercer.engine.persist.conversion.CollectionConverter.convert(Collection
Converter.java:130)
    at 
com.vercer.engine.persist.conversion.CombinedTypeConverter.convert(Combined
TypeConverter.java:37)
    at 
com.vercer.engine.persist.conversion.DefaultTypeConverter.convert(DefaultTy
peConverter.java:56)
    at 
com.vercer.engine.persist.translator.ObjectFieldTranslator.activate(ObjectF
ieldTranslator.java:128)
    at 
com.vercer.engine.persist.standard.StrategyObjectDatastore$3.activate(Strat
egyObjectDatastore.java:220)
    at 
com.vercer.engine.persist.translator.ObjectFieldTranslator.propertiesToType
safe(ObjectFieldTranslator.java:57)
    at 
com.vercer.engine.persist.translator.ListTranslator.propertiesToTypesafe(Li
stTranslator.java:48)
    at 
com.vercer.engine.persist.translator.ObjectFieldTranslator.activate(ObjectF
ieldTranslator.java:107)
    ... 30 more

Original issue reported on code.google.com by harsh.je...@gmail.com on 26 May 2010 at 9:11

Attachments:

GoogleCodeExporter commented 9 years ago
In class CollectionConverter line 130, Twig hits the IllegalArgumentException

IllegalArgumentException("Unsupported Collection type " + type + ". Try 
declaring the 
interface instead of the concrete collection type.");

Original comment by harsh.je...@gmail.com on 26 May 2010 at 10:03

GoogleCodeExporter commented 9 years ago

Original comment by jdpatterson on 26 May 2010 at 10:37

GoogleCodeExporter commented 9 years ago
In line 130 CollectionConverter, I added and fixed the problem.
Can you please add it to the main branch for 1.0.2?

else if(erased.isAssignableFrom(EnumSet.class)){                
@SuppressWarnings("unchecked")
EnumSet result = EnumSet.noneOf((Class<? extends Enum>)componentType);  
for(Object item : convertedItems){
  result.add(Enum.valueOf((Class<? extends Enum>)componentType, item.toString()));
}
return (T) result;              
}

Original comment by harsh.je...@gmail.com on 26 May 2010 at 4:24

GoogleCodeExporter commented 9 years ago
Nice one.  Will add it to hg and it will be included in the next binary.  I'll 
factor out the collection creation code 
so its easy to override to add new collection types.

I had purposely not implemented different types of collections here as it will 
never be possible to add them all.  
The recommended approach was to create a new TypeConverter and register it.  
But there is enough difficult 
code here to make that a pain in the ass (e.g. converting the component types) 
so it makes sense to extend this 
base class.

Original comment by jdpatterson on 26 May 2010 at 4:41

GoogleCodeExporter commented 9 years ago
Yes refactoring this CollectionConverter would help to add new collections.

I was worried about the item.toString() I use. it may cause an issue if someone 
overrides the toString() method of their enum. Not sure what else to do? any 
ideas?

Also please let me know as soon as you add it to hg, i'd like to check that out 
and be 
compatible with your branch :)

Original comment by harsh.je...@gmail.com on 27 May 2010 at 3:06

GoogleCodeExporter commented 9 years ago
thanks! can you close this issue after adding this code snippet to hg.

Original comment by harsh.je...@gmail.com on 27 May 2010 at 1:07

GoogleCodeExporter commented 9 years ago
Hi John 

Can you please add this small fix to hg? I would like to also get the other 
nice 
updates you have and I don't want that to break my code :)

thanks! 
Harsh

Original comment by harsh.je...@gmail.com on 30 May 2010 at 3:50

GoogleCodeExporter commented 9 years ago
Checked in now with unit test.  It is now easier to add additional collection 
types by overridding 
createCollectionInstance(...)

Original comment by jdpatterson on 6 Jun 2010 at 8:03