View Javadoc
1   /*
2    *  Copyright 2010, 2011 Chris Pheby
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.jadira.bindings.core.general.binding;
17  
18  import java.lang.reflect.Constructor;
19  import java.lang.reflect.InvocationTargetException;
20  import java.lang.reflect.Method;
21  import java.lang.reflect.Modifier;
22  
23  import org.jadira.bindings.core.api.Binding;
24  import org.jadira.bindings.core.api.BindingException;
25  import org.jadira.bindings.core.general.marshaller.MethodToMarshaller;
26  
27  /**
28   * Binding that supports a to contract, and a constructor parameterised by a S
29   * @param <S> Source type for the conversion
30   * @param <T> Target type
31   */
32  public final class MethodConstructorBinding<S, T> extends MethodToMarshaller<S, T> implements Binding<S, T> {
33  
34      private final Constructor<S> unmarshal;
35  
36      /**
37       * Constructs a binding that supports a marshal method and an unmarshalling constructor
38       * @param unmarshal The Constructor
39       * @param targetClass The target class for marshalling to
40       * @param marshal The marshalling method
41       */
42      public MethodConstructorBinding(Constructor<S> unmarshal, Class<T> targetClass, Method marshal) {
43          
44          super(unmarshal.getDeclaringClass(), targetClass, marshal);
45          
46          if (getBoundClass().isInterface() 
47                  || Modifier.isAbstract(getBoundClass().getModifiers()) 
48                  || getBoundClass().isLocalClass() 
49                  || getBoundClass().isMemberClass()) {
50              throw new IllegalStateException("unmarshal constructor must have an instantiable target class");
51          }
52          
53          this.unmarshal = unmarshal;
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      public S unmarshal(T str) {
61          try {
62              return unmarshal.newInstance(str);
63          } catch (IllegalAccessException ex) {
64              throw new IllegalStateException("Constructor is not accessible");
65          } catch (InstantiationException ex) {
66              throw new IllegalStateException("Constructor is not valid");
67          } catch (InvocationTargetException ex) {
68              if (ex.getCause() instanceof RuntimeException) {
69                  throw (RuntimeException) ex.getCause();
70              }
71              throw new BindingException(ex.getMessage(), ex.getCause());
72          }
73      }
74  }