svn-gvsig-desktop / trunk / libraries / libIverUtiles / src / com / iver / utiles / extensionPoints / ExtensionBuilder.java @ 4676
History | View | Annotate | Download (2.79 KB)
1 |
package com.iver.utiles.extensionPoints; |
---|---|
2 |
|
3 |
import java.lang.reflect.Constructor; |
4 |
import java.lang.reflect.InvocationTargetException; |
5 |
import java.util.Map; |
6 |
|
7 |
/**
|
8 |
* Clase de utilidad usada para crear las extensiones.
|
9 |
*
|
10 |
* Esta clase presenta un par de metodos estaticos para
|
11 |
* permitir crear un objeto a partir de una clase.
|
12 |
*
|
13 |
* @author jjdelcerro
|
14 |
*
|
15 |
*/
|
16 |
public abstract class ExtensionBuilder implements IExtensionBuilder { |
17 |
|
18 |
/**
|
19 |
* Crea un objeto de la clase indicada.
|
20 |
*
|
21 |
* @param cls Clase de la que crear la instancia
|
22 |
* @return
|
23 |
*
|
24 |
* @throws InstantiationException
|
25 |
* @throws IllegalAccessException
|
26 |
*/
|
27 |
public static Object create(Class cls) throws InstantiationException, IllegalAccessException { |
28 |
Object obj = null; |
29 |
|
30 |
if( cls == null ) { |
31 |
return null; |
32 |
} |
33 |
obj = cls.newInstance(); |
34 |
return obj;
|
35 |
} |
36 |
|
37 |
/**
|
38 |
* Crea un objeto de la clase indicada.
|
39 |
*
|
40 |
* Crea un objeto de la clase indicada pasandole al constructor
|
41 |
* los argumentos indicados en <i>args</i>.
|
42 |
* <br>
|
43 |
* @param cls Clase de la que crear la instancia
|
44 |
* @param args Argumentos que pasar al constructor.
|
45 |
* @return
|
46 |
*
|
47 |
* @throws SecurityException
|
48 |
* @throws NoSuchMethodException
|
49 |
* @throws IllegalArgumentException
|
50 |
* @throws InstantiationException
|
51 |
* @throws IllegalAccessException
|
52 |
* @throws InvocationTargetException
|
53 |
*/
|
54 |
public static Object create(Class cls, Object [] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { |
55 |
Object obj = null; |
56 |
Constructor create = null; |
57 |
Class [] types = new Class[args.length]; |
58 |
|
59 |
if( cls == null ) { |
60 |
return null; |
61 |
} |
62 |
for( int n=0 ; n<args.length ; n++ ) { |
63 |
Object arg = args[n];
|
64 |
types[n] = arg.getClass(); |
65 |
} |
66 |
create = cls.getConstructor(types); |
67 |
obj = create.newInstance(args); |
68 |
return obj;
|
69 |
} |
70 |
/**
|
71 |
* Crea un objeto de la clase indicada.
|
72 |
*
|
73 |
* Crea un objeto de la clase indicada pasandole al constructor
|
74 |
* un como argumento un Map..
|
75 |
* <br>
|
76 |
* @param cls Clase de la que crear la instancia
|
77 |
* @param args Map a pasar como argumento al constructor.
|
78 |
* @return
|
79 |
*
|
80 |
* @throws SecurityException
|
81 |
* @throws NoSuchMethodException
|
82 |
* @throws IllegalArgumentException
|
83 |
* @throws InstantiationException
|
84 |
* @throws IllegalAccessException
|
85 |
* @throws InvocationTargetException
|
86 |
*/
|
87 |
public static Object create(Class cls, Map args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { |
88 |
Object obj = null; |
89 |
Constructor create = null; |
90 |
Class [] types = new Class[1]; |
91 |
Object [] argsx = new Object[1]; |
92 |
|
93 |
if( cls == null ) { |
94 |
return null; |
95 |
} |
96 |
types[0] = Map.class; |
97 |
argsx[0] = args;
|
98 |
create = cls.getConstructor(types); |
99 |
obj = create.newInstance(argsx); |
100 |
return obj;
|
101 |
} |
102 |
} |