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 public abstract class NewBeanInstanceStrategy {
33
34 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 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
61 String cause = "";
62 try { cause = e.getCause() != null ? "\n"+e.getCause().getMessage() : ""; }
63 catch( Throwable t ) {
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 }