99package com .github .g3force .instanceables ;
1010
1111import java .lang .reflect .Constructor ;
12- import java .lang .reflect .InvocationTargetException ;
1312import java .util .ArrayList ;
1413import java .util .Arrays ;
1514import java .util .List ;
1615
1716
1817/**
1918 * An {@link InstanceableClass} can be used to create an object from a class and a set of parameters.
20- *
21- * @author Nicolai Ommer <[email protected] > 2219 */
2320public class InstanceableClass
2421{
25- // --------------------------------------------------------------------------
26- // --- variables and constants ----------------------------------------------
27- // --------------------------------------------------------------------------
28- private final Class <?> impl ;
29- private final List <InstanceableParameter > params ;
30-
31-
32- // --------------------------------------------------------------------------
33- // --- constructors ---------------------------------------------------------
34- // --------------------------------------------------------------------------
35-
36- /**
37- * @param impl
38- * @param params
39- */
22+ private final Class <?> impl ;
23+ private final List <InstanceableParameter > params ;
24+
25+
4026 public InstanceableClass (final Class <?> impl , final InstanceableParameter ... params )
4127 {
4228 this .impl = impl ;
4329 this .params = Arrays .asList (params );
4430 }
45-
46-
47- // --------------------------------------------------------------------------
48- // --- methods --------------------------------------------------------------
49- // --------------------------------------------------------------------------
50-
31+
32+
5133 /**
5234 * Create a new instance with the specified arguments
53- *
35+ *
5436 * @param args
5537 * @return
5638 * @throws NotCreateableException
@@ -62,35 +44,20 @@ public Object newInstance(final Object... args) throws NotCreateableException
6244 {
6345 Constructor <?> con = getConstructor ();
6446 result = con .newInstance (args );
65- } catch (final SecurityException err )
66- {
67- throw new NotCreateableException ("" , err );
68- } catch (final InstantiationException err )
69- {
70- throw new NotCreateableException ("" , err );
71- } catch (final IllegalAccessException err )
72- {
73- throw new NotCreateableException ("" , err );
74- } catch (final IllegalArgumentException err )
75- {
76- throw new NotCreateableException ("" , err );
77- } catch (final InvocationTargetException err )
78- {
79- throw new NotCreateableException ("" , err );
80- } catch (final IllegalStateException err )
81- {
82- throw new NotCreateableException ("" , err );
8347 } catch (NoSuchMethodException err )
8448 {
8549 throw new NotCreateableException ("Wrong constructor types." , err );
50+ } catch (final Exception err )
51+ {
52+ throw new NotCreateableException ("Can not create instance" , err );
8653 }
8754 return result ;
8855 }
89-
90-
56+
57+
9158 /**
9259 * Create a new instance with default parameters (as defined in enum)
93- *
60+ *
9461 * @return
9562 * @throws NotCreateableException
9663 */
@@ -100,79 +67,62 @@ public Object newDefaultInstance() throws NotCreateableException
10067 {
10168 return newInstance ();
10269 }
103- List <Object > objParams = new ArrayList <Object >(getParams ().size ());
70+ List <Object > objParams = new ArrayList <>(getParams ().size ());
10471 for (InstanceableParameter param : getParams ())
10572 {
10673 Object objParam = param .parseString (param .getDefaultValue ());
10774 objParams .add (objParam );
10875 }
10976 return newInstance (objParams .toArray ());
11077 }
111-
112-
78+
79+
11380 /**
11481 * Add parameter
115- *
82+ *
11683 * @param param
11784 */
11885 public void addParam (final InstanceableParameter param )
11986 {
12087 params .add (param );
12188 }
122-
123-
124- // --------------------------------------------------------------------------
125- // --- getter/setter --------------------------------------------------------
126- // --------------------------------------------------------------------------
127-
128-
89+
90+
12991 /**
13092 * Returns the first public constructor of the Play.
131- *
132- * @return
133- * @throws NoSuchMethodException
134- * @throws SecurityException
13593 */
13694 public Constructor <?> getConstructor () throws NoSuchMethodException
13795 {
138- Class <?> paramTypes [] = new Class <?>[params .size ()];
96+ Class <?>[] paramTypes = new Class <?>[params .size ()];
13997 for (int i = 0 ; i < params .size (); i ++)
14098 {
14199 paramTypes [i ] = params .get (i ).getImpl ();
142100 }
143101 return impl .getConstructor (paramTypes );
144102 }
145-
146-
103+
104+
147105 /**
148106 * @return the params
149107 */
150108 public final List <InstanceableParameter > getParams ()
151109 {
152110 return params ;
153111 }
154-
155-
156- /**
157- * @author Nicolai Ommer <[email protected] > 158- */
112+
113+
159114 public static class NotCreateableException extends Exception
160115 {
161- /** */
162- private static final long serialVersionUID = 89775383135278930L ;
163-
164-
165- /**
166- * @param message
167- * @param cause
168- */
116+ private static final long serialVersionUID = 89775383135278930L ;
117+
118+
169119 public NotCreateableException (final String message , final Throwable cause )
170120 {
171121 super (message , cause );
172122 }
173123 }
174-
175-
124+
125+
176126 /**
177127 * @return the impl
178128 */
0 commit comments