View Javadoc

1   // Copyright 2004, FreeHEP.
2   package hep.wired.plugin;
3   
4   import java.lang.reflect.*;
5   
6   import org.freehep.xml.io.XMLIOFactory;
7   
8   /***
9    * Factory to create objects by calling the default constructor.
10   *
11   * Should maybe move to FreeHEP (xml.io)
12   *
13   * @author Mark Donszelmann
14   * @version $Id: AbstractXMLIOFactory.java 266 2004-06-08 23:37:39Z duns $
15   */
16  public abstract class AbstractXMLIOFactory implements XMLIOFactory {
17  
18      public Object createObject(Class cls) {
19          try {
20              Constructor ctr = cls.getDeclaredConstructor(null);
21              ctr.setAccessible(true);
22              return ctr.newInstance(null);
23          } catch (NoSuchMethodException e) {
24              throw new IllegalArgumentException(cls.getName()+" does not have no-arg constructor.");
25          } catch (InvocationTargetException e) {
26              throw new IllegalArgumentException(cls.getName()+" cannot call no-arg constructor.");
27          } catch (InstantiationException e) {
28              throw new IllegalArgumentException(cls.getName()+" cannot be instantiated.");
29          } catch (IllegalAccessException e) {
30              throw new IllegalArgumentException(cls.getName()+" cannot be instantiated due to access problems.");
31          } 
32      }
33  }