View Javadoc

1   /*
2    *  Copyright 2010 Stephen Colebourne
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.joda.convert;
17  
18  import java.lang.reflect.InvocationTargetException;
19  import java.lang.reflect.Method;
20  
21  /**
22   * Conversion to and from a string using reflection.
23   * <p>
24   * The toString method must meet the following signature:<br />
25   * {@code String anyName()} on Class T.
26   * <p>
27   * ReflectionStringConverter is abstract, but all known implementations are thread-safe and immutable.
28   * 
29   * @param <T>  the type of the converter
30   */
31  abstract class ReflectionStringConverter<T> implements StringConverter<T> {
32  
33      /** The converted class. */
34      final Class<T> cls;
35      /** Conversion to a string. */
36      final Method toString;
37  
38      /**
39       * Creates an instance using two methods.
40       * @param cls  the class this converts for, not null
41       * @param toString  the toString method, not null
42       * @throws RuntimeException (or subclass) if the method signatures are invalid
43       */
44      ReflectionStringConverter(Class<T> cls, Method toString) {
45          if (toString.getParameterTypes().length != 0) {
46              throw new IllegalStateException("ToString method must have no parameters");
47          }
48          if (toString.getReturnType() != String.class) {
49              throw new IllegalStateException("ToString method must return a String");
50          }
51          this.cls = cls;
52          this.toString = toString;
53      }
54  
55      //-----------------------------------------------------------------------
56      /**
57       * Converts the object to a {@code String}.
58       * @param object  the object to convert, not null
59       * @return the converted string, may be null but generally not
60       */
61      public String convertToString(T object) {
62          try {
63              return (String) toString.invoke(object);
64          } catch (IllegalAccessException ex) {
65              throw new IllegalStateException("Method is not accessible");
66          } catch (InvocationTargetException ex) {
67              if (ex.getCause() instanceof RuntimeException) {
68                  throw (RuntimeException) ex.getCause();
69              }
70              throw new RuntimeException(ex.getMessage(), ex.getCause());
71          }
72      }
73  
74      //-----------------------------------------------------------------------
75      @Override
76      public String toString() {
77          return "RefectionStringConverter[" + cls.getSimpleName() + "]";
78      }
79  
80  }