Coverage Report - net.sf.json.util.NewBeanInstanceStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
NewBeanInstanceStrategy
0%
0/3
N/A
3.5
NewBeanInstanceStrategy$1
N/A
N/A
3.5
NewBeanInstanceStrategy$DefaultNewBeanInstanceStrategy
0%
0/13
0%
0/4
3.5
 
 1  
 /*
 2  
  * Copyright 2002-2009 the original author or authors.
 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  
 
 17  
 package net.sf.json.util;
 18  
 
 19  
 import java.lang.reflect.Constructor;
 20  
 import java.lang.reflect.InvocationTargetException;
 21  
 
 22  
 import net.sf.json.JSONObject;
 23  
 
 24  
 /**
 25  
  * Base class for creating Bean instances.<br>
 26  
  * <ul>
 27  
  * <li>DEFAULT - calls Class.newInstance().</li>
 28  
  * </ul>
 29  
  *
 30  
  * @author Andres Almiray <aalmiray@users.sourceforge.net>
 31  
  */
 32  0
 public abstract class NewBeanInstanceStrategy {
 33  
    /** Calls Class.newInstance() */
 34  0
    public static final NewBeanInstanceStrategy DEFAULT = new DefaultNewBeanInstanceStrategy();
 35  
 
 36  
    /**
 37  
     * Creates a new instance.
 38  
     *
 39  
     * @param target the source class
 40  
     * @param source additional properties that may be needed to create the
 41  
     *        instance
 42  
     */
 43  
    public abstract Object newInstance( Class target, JSONObject source )
 44  
          throws InstantiationException, IllegalAccessException, SecurityException,
 45  
          NoSuchMethodException, InvocationTargetException;
 46  
 
 47  0
    private static final class DefaultNewBeanInstanceStrategy extends NewBeanInstanceStrategy {
 48  0
       private static final Object[] EMPTY_ARGS = new Object[0];
 49  0
       private static final Class[] EMPTY_PARAM_TYPES = new Class[0];
 50  
 
 51  
       public Object newInstance( Class target, JSONObject source ) throws InstantiationException,
 52  
             IllegalAccessException, SecurityException, NoSuchMethodException,
 53  
             InvocationTargetException {
 54  0
          if( target != null ){
 55  0
             Constructor c = target.getDeclaredConstructor( EMPTY_PARAM_TYPES );
 56  0
             c.setAccessible( true );
 57  
             try {
 58  0
                return c.newInstance( EMPTY_ARGS );
 59  0
             } catch ( InstantiationException e ) {
 60  
                // getCause() was added on jdk 1.4
 61  0
                String cause = "";
 62  0
                try { cause = e.getCause() != null ? "\n"+e.getCause().getMessage() : ""; }
 63  0
                catch( Throwable t ) { /* ignore */ }
 64  0
                throw new InstantiationException(
 65  
                      "Instantiation of \"" +  target + "\" failed. " +
 66  
                      "It's probably because class is an interface, " +
 67  
                      "abstract class, array class, primitive type or void." +
 68  
                      cause );
 69  
             }
 70  
          }
 71  0
          return null;
 72  
       }
 73  
    }
 74  
 }