Revision 477,
900 bytes
checked in by wojtekp, 13 years ago
(diff) |
|
-
Property svn:mime-type set to
text/plain
|
Line | |
---|
1 | package simulator.utils; |
---|
2 | |
---|
3 | import org.apache.commons.logging.Log; |
---|
4 | import org.apache.commons.logging.LogFactory; |
---|
5 | |
---|
6 | |
---|
7 | public class InstanceFactory { |
---|
8 | |
---|
9 | private static Log log = LogFactory.getLog(InstanceFactory.class); |
---|
10 | |
---|
11 | public static final Object createInstance(String className, Class<?> z){ |
---|
12 | |
---|
13 | if(className == null) |
---|
14 | return null; |
---|
15 | |
---|
16 | Object obj = null; |
---|
17 | |
---|
18 | try { |
---|
19 | |
---|
20 | Class<?> c = Class.forName(className); |
---|
21 | if(!z.isAssignableFrom(c)){ |
---|
22 | log.error(className + " does not implement desired interface " + z.getName()); |
---|
23 | return null; |
---|
24 | } |
---|
25 | |
---|
26 | obj = c.newInstance(); |
---|
27 | |
---|
28 | } catch (ClassNotFoundException e) { |
---|
29 | log.error(e.getMessage() + " Class not found."); |
---|
30 | } catch (InstantiationException e) { |
---|
31 | log.error(e.getMessage() + " Can not create class instance."); |
---|
32 | } catch (IllegalAccessException e) { |
---|
33 | log.error(e.getMessage() + " Illegal Access."); |
---|
34 | } |
---|
35 | |
---|
36 | return obj; |
---|
37 | |
---|
38 | } |
---|
39 | |
---|
40 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.