Thanks Kevin for your reply.
The 1st option is impossible for me. My enum is built by xjc in maven jaxb plugin against simpleType defined in xsd. I am looking at option 2, but I want to have a generic enum type handler for all my enums with different values. I am trying option 2 by copying default EnumTypeHandler, but I am getting the exception.
org.beanio.BeanIOConfigurationException: Failed to create type handler named 'enumHandler'
at org.beanio.internal.compiler.StreamCompiler.createTypeHandlerFactory(StreamCompiler.java:216)
at org.beanio.internal.compiler.StreamCompiler.createStreamDefinitions(StreamCompiler.java:142)
at org.beanio.internal.compiler.StreamCompiler.loadMapping(StreamCompiler.java:103)
at org.beanio.internal.DefaultStreamFactory.load(DefaultStreamFactory.java:58)
at org.beanio.StreamFactory.load(StreamFactory.java:267)
at org.beanio.StreamFactory.load(StreamFactory.java:243)
at org.beanio.StreamFactory.load(StreamFactory.java:232)
at com.chase.ccs.ess.mw.cpsca.messaging.CAAATest.testUnMarshallCAAAInput(CAAATest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.jdt.internal.launching.LongCommandLineLauncher.main(LongCommandLineLauncher.java:43)
Caused by: org.beanio.BeanIOConfigurationException: Cound not instantiate class 'class com.chase.ccs.ess.mw.cpsca.beanio.handler.EnumTypeHandler'
at org.beanio.internal.util.BeanUtil.createBean(BeanUtil.java:95)
at org.beanio.internal.util.BeanUtil.createBean(BeanUtil.java:60)
at org.beanio.internal.compiler.StreamCompiler.createTypeHandlerFactory(StreamCompiler.java:212)
... 35 more
Caused by: java.lang.InstantiationException: com.chase.ccs.ess.mw.cpsca.beanio.handler.EnumTypeHandler
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at org.beanio.internal.util.BeanUtil.createBean(BeanUtil.java:92)
... 37 more
Here is my EnumTypeHandler by copying default EnumTypeHandler and changed parse to take fromValue() instead of Enum.valueOf()... Please advise
import org.beanio.types.TypeConversionException;
import org.beanio.types.TypeHandler;
@SuppressWarnings({"unchecked", "rawtypes"})
public class EnumTypeHandler implements TypeHandler {
private Class<Enum> type;
/**
* Constructs a new <tt>EnumTypeHandler</tt>.
* @param type the Enum class
*/
public EnumTypeHandler(Class<Enum> type) {
this.type = type;
}
/*
* (non-Javadoc)
* @see org.beanio.types.TypeHandler#parse(java.lang.String)
*/
public Object parse(String text) throws TypeConversionException {
if (text == null || "".equals(text)) {
return null;
}
try {
Method m = type.getDeclaredMethod("fromValue", String.class);
return m.invoke(null, text);
}
catch (IllegalArgumentException ex) {
throw new TypeConversionException("Invalid " + getType().getSimpleName() +
" enum value '" + text + "'", ex);
}
catch (Exception ex) {
throw new TypeConversionException("Enum reflection error ", ex);
}
}
/*
* (non-Javadoc)
* @see org.beanio.types.TypeHandler#format(java.lang.Object)
*/
public String format(Object value) {
if (value == null) {
return null;
}
return ((Enum)value).name();
}
/*
* (non-Javadoc)
* @see org.beanio.types.TypeHandler#getType()
*/
public Class<?> getType() {
return type;
}
public void setType(Class<Enum> type) {
this.type = type;
}
}
Thanks
-Lei