| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | 0 | public abstract class NewBeanInstanceStrategy { |
| 33 | |
|
| 34 | 0 | public static final NewBeanInstanceStrategy DEFAULT = new DefaultNewBeanInstanceStrategy(); |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 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 | |
|
| 61 | 0 | String cause = ""; |
| 62 | 0 | try { cause = e.getCause() != null ? "\n"+e.getCause().getMessage() : ""; } |
| 63 | 0 | catch( Throwable t ) { } |
| 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 | |
} |