View Javadoc

1   package fr.ove.utils;
2   
3   /***
4   * A little factory to dynamically instanciate classes.
5   *
6   * @author © 1999 DIRAT Laurent
7   * @version 1.0  29/10/1999
8   */
9   public class Factory implements java.io.Serializable {
10      /***
11      * Returns an instance of the class with the specified name.
12      * @param className the name of the class we want an instance.
13      */
14      public static Object getClassInstance(String className) {
15          Object newClass = null;
16          
17      	try {
18          	Class c = Class.forName(className);
19          	
20          	try {
21          		newClass = c.newInstance();
22          	}
23          	catch (InstantiationException e ) {
24          		System.out.println(className + " cannot be instantiated");
25          		return null;
26          	}
27          	catch (IllegalAccessException e ) {
28          		System.out.println(className + " impossible to access default constructor");
29          		return null;
30          	}
31          	catch (NoClassDefFoundError e ) {
32      			System.out.println("No definition of " + className + " can be found");
33          	}
34  
35          }
36          catch (ClassNotFoundException e) {
37          	System.out.println("Class " + className + " not found");
38          		return null;
39          }
40  
41          return newClass;
42      }
43  }