View Javadoc
1   /*
2    * Copyright 2005 Philipp Erlacher
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This class is part of the command pattern implementation to instantiate an
25   * object. It is used as a command by the command invoker {@link PrimitiveObject}.
26   * 
27   * @author <a href="mailto:philipp DOT erlacher AT gmail DOT com">Philipp
28   *         Erlacher</a>
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          // discover the fromValue Method
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              // do nothing, check valueOf method
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          // backwards compability, check valueOf method to support
58          // "simple" enums without value object
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  }