View Javadoc

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  public abstract class NewBeanInstanceStrategy {
33     /** Calls Class.newInstance() */
34     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     private static final class DefaultNewBeanInstanceStrategy extends NewBeanInstanceStrategy {
48        private static final Object[] EMPTY_ARGS = new Object[0];
49        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           if( target != null ){
55              Constructor c = target.getDeclaredConstructor( EMPTY_PARAM_TYPES );
56              c.setAccessible( true );
57              try {
58                 return c.newInstance( EMPTY_ARGS );
59              } catch ( InstantiationException e ) {
60                 // getCause() was added on jdk 1.4
61                 String cause = "";
62                 try { cause = e.getCause() != null ? "\n"+e.getCause().getMessage() : ""; }
63                 catch( Throwable t ) { /* ignore */ }
64                 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           return null;
72        }
73     }
74  }