1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.exolab.castor.xml.parsing.primitive.objects;
17
18 import java.lang.reflect.InvocationTargetException;
19 import java.lang.reflect.Method;
20
21 import org.apache.commons.lang.StringUtils;
22
23
24
25
26
27
28
29
30
31 class PrimitiveEnum extends PrimitiveObject {
32
33 @Override
34 public Object getObject(Class<?> type, String value) {
35 if (StringUtils.isEmpty(value)) {
36 return null;
37 }
38
39
40 try {
41 Method valueOfMethod = type.getMethod("fromValue",
42 new Class[] { String.class });
43 return valueOfMethod.invoke(null, new Object[] { value });
44
45 } catch (NoSuchMethodException e) {
46
47 } catch (IllegalArgumentException e) {
48 throw new IllegalStateException(e.toString());
49 } catch (IllegalAccessException e) {
50 throw new IllegalStateException(e.toString());
51 } catch (InvocationTargetException e) {
52 if (e.getTargetException() instanceof RuntimeException) {
53 throw (RuntimeException) e.getTargetException();
54 }
55 }
56
57
58
59 try {
60 Method valueOfMethod = type.getMethod("valueOf",
61 new Class[] { String.class });
62 return valueOfMethod.invoke(null, new Object[] { value });
63
64 } catch (IllegalAccessException e) {
65 throw new IllegalStateException(e.toString());
66 } catch (InvocationTargetException e) {
67 if (e.getTargetException() instanceof RuntimeException) {
68 throw (RuntimeException) e.getTargetException();
69 }
70 } catch (NoSuchMethodException e) {
71 String err = type.getName()
72 + " does not contain the required method: public static "
73 + type.getName() + " valueOf(String);";
74 throw new IllegalArgumentException(err);
75 }
76
77 return value;
78 }
79
80 }