Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / util / extensionPoints / ExtensionBuilder.java @ 10960

History | View | Annotate | Download (2.8 KB)

1
package org.gvsig.raster.util.extensionPoints;
2

    
3
import java.lang.reflect.Constructor;
4
import java.lang.reflect.InvocationTargetException;
5
import java.util.Map;
6

    
7

    
8
/**
9
 * Clase de utilidad usada para crear las extensiones.
10
 * 
11
 * Esta clase presenta un par de metodos estaticos para
12
 * permitir crear un objeto a partir de una clase.
13
 * 
14
 * @author jjdelcerro
15
 *
16
 */
17
public abstract class ExtensionBuilder implements IExtensionBuilder {
18

    
19
        /**
20
         * Crea un objeto de la clase indicada.
21
         * 
22
         * @param cls Clase de la que crear la instancia
23
         * @return
24
         * 
25
         * @throws InstantiationException
26
         * @throws IllegalAccessException
27
         */
28
        public static Object create(Class cls) throws InstantiationException, IllegalAccessException {
29
                Object obj = null;
30

    
31
                if( cls == null ) {
32
                        return null;
33
                }
34
                obj = cls.newInstance();
35
                return obj;
36
        }
37
        
38
        /**
39
         * Crea un objeto de la clase indicada.
40
         * 
41
         * Crea un objeto de la clase indicada pasandole al constructor
42
         * los argumentos indicados en <i>args</i>.
43
         * <br>
44
         * @param cls Clase de la que crear la instancia
45
         * @param args Argumentos que pasar al constructor.
46
         * @return
47
         * 
48
         * @throws SecurityException
49
         * @throws NoSuchMethodException
50
         * @throws IllegalArgumentException
51
         * @throws InstantiationException
52
         * @throws IllegalAccessException
53
         * @throws InvocationTargetException
54
         */
55
        public static Object create(Class cls, Object [] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
56
                Object obj = null;
57
                Constructor create = null;
58
                Class [] types = new Class[args.length];
59
                
60
                if( cls == null ) {
61
                        return null;
62
                }
63
                for( int n=0 ; n<args.length ; n++ ) {
64
                        Object arg = args[n]; 
65
                        types[n] = arg.getClass();
66
                }
67
                create = cls.getConstructor(types);
68
                obj = create.newInstance(args);
69
                return obj;
70
        }
71
        /**
72
         * Crea un objeto de la clase indicada.
73
         * 
74
         * Crea un objeto de la clase indicada pasandole al constructor
75
         * un como argumento un Map..
76
         * <br>
77
         * @param cls Clase de la que crear la instancia
78
         * @param args Map a pasar como argumento al constructor.
79
         * @return
80
         * 
81
         * @throws SecurityException
82
         * @throws NoSuchMethodException
83
         * @throws IllegalArgumentException
84
         * @throws InstantiationException
85
         * @throws IllegalAccessException
86
         * @throws InvocationTargetException
87
         */
88
        public static Object create(Class cls, Map args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
89
                Object obj = null;
90
                Constructor create = null;
91
                Class [] types = new Class[1];
92
                Object [] argsx = new Object[1];
93
                
94
                if( cls == null ) {
95
                        return null;
96
                }
97
                types[0] = Map.class;
98
                argsx[0] = args;
99
                create = cls.getConstructor(types);
100
                obj = create.newInstance(argsx);
101
                return obj;
102
        }
103
}