Coverage Report - org.seasar.teeda.core.util.MarshalUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
MarshalUtil
48%
13/27
50%
7/14
6
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 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,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.teeda.core.util;
 17  
 
 18  
 import java.lang.reflect.Constructor;
 19  
 import java.lang.reflect.InvocationTargetException;
 20  
 
 21  
 import javax.faces.FacesException;
 22  
 
 23  
 import org.seasar.framework.exception.IllegalAccessRuntimeException;
 24  
 import org.seasar.framework.exception.InstantiationRuntimeException;
 25  
 import org.seasar.framework.exception.InvocationTargetRuntimeException;
 26  
 import org.seasar.framework.util.ClassUtil;
 27  
 
 28  
 /**
 29  
  * @author shot
 30  
  */
 31  0
 public class MarshalUtil {
 32  
 
 33  
     public static Object createMarshalInstance(String className,
 34  
             Class beforeClass, Object current) {
 35  31
         return createMarshalInstance(ClassUtil.forName(className), beforeClass,
 36  
                 current);
 37  
     }
 38  
 
 39  
     public static Object createMarshalInstance(Class clazz, Class beforeClass,
 40  
             Object current) {
 41  31
         if (clazz == null) {
 42  0
             if (current == null) {
 43  0
                 throw new IllegalArgumentException();
 44  
             } else {
 45  0
                 return current;
 46  
             }
 47  
         }
 48  31
         Constructor constructor = getConstructorWithNoException(clazz,
 49  
                 new Class[] { beforeClass });
 50  31
         if (constructor != null) {
 51  6
             current = newInstanceByConstructor(constructor,
 52  
                     new Object[] { current });
 53  
         } else {
 54  25
             current = ClassUtil.newInstance(clazz);
 55  
         }
 56  31
         return current;
 57  
     }
 58  
 
 59  
     public static Constructor getConstructorWithNoException(Class clazz,
 60  
             Class[] argTypes) {
 61  31
         if (clazz == null || argTypes == null) {
 62  0
             throw new IllegalArgumentException();
 63  
         }
 64  
         try {
 65  31
             return clazz.getConstructor(argTypes);
 66  25
         } catch (NoSuchMethodException e) {
 67  25
             return null;
 68  
         }
 69  
     }
 70  
 
 71  
     public static Object newInstanceByConstructor(Constructor constructor,
 72  
             Object[] args) {
 73  6
         if (constructor == null || args == null) {
 74  0
             throw new IllegalArgumentException();
 75  
         }
 76  
         try {
 77  6
             return constructor.newInstance(args);
 78  0
         } catch (IllegalArgumentException e) {
 79  0
             throw new FacesException(e);
 80  0
         } catch (InstantiationException e) {
 81  0
             throw new InstantiationRuntimeException(constructor.getClass(), e);
 82  0
         } catch (IllegalAccessException e) {
 83  0
             throw new IllegalAccessRuntimeException(constructor.getClass(), e);
 84  0
         } catch (InvocationTargetException e) {
 85  0
             throw new InvocationTargetRuntimeException(constructor.getClass(),
 86  
                     e);
 87  
         }
 88  
     }
 89  
 
 90  
 }