Dynamically loads and creates an object. @param className Name of the class of the object @param argsClass Class(es) of the argument(s) or null if no-argument constructor should be called @param args Argument(s) @return The new object @throws SettingsError if object couldn't be created
(String className, Class<?>[] argsClass, Object[] args)
| 775 | * @throws SettingsError if object couldn't be created |
| 776 | */ |
| 777 | private Object loadObject(String className, Class<?>[] argsClass, |
| 778 | Object[] args) { |
| 779 | Object o = null; |
| 780 | Class<?> objClass = getClass(className); |
| 781 | Constructor<?> constructor; |
| 782 | |
| 783 | try { |
| 784 | if (argsClass != null) { // use a specific constructor |
| 785 | constructor = objClass.getConstructor((Class[])argsClass); |
| 786 | o = constructor.newInstance(args); |
| 787 | } |
| 788 | else { // call empty constructor |
| 789 | o = objClass.newInstance(); |
| 790 | } |
| 791 | } catch (SecurityException e) { |
| 792 | e.printStackTrace(); |
| 793 | throw new SettingsError("Fatal exception " + e, e); |
| 794 | } catch (IllegalArgumentException e) { |
| 795 | e.printStackTrace(); |
| 796 | throw new SettingsError("Fatal exception " + e, e); |
| 797 | } catch (NoSuchMethodException e) { |
| 798 | throw new SettingsError("Class '" + className + |
| 799 | "' doesn't have a suitable constructor", e); |
| 800 | } catch (InstantiationException e) { |
| 801 | throw new SettingsError("Can't create an instance of '" + |
| 802 | className + "'", e); |
| 803 | } catch (IllegalAccessException e) { |
| 804 | e.printStackTrace(); |
| 805 | throw new SettingsError("Fatal exception " + e, e); |
| 806 | } catch (InvocationTargetException e) { |
| 807 | // this exception occurs if initialization of the object fails |
| 808 | if (e.getCause() instanceof SettingsError) { |
| 809 | throw (SettingsError)e.getCause(); |
| 810 | } |
| 811 | else { |
| 812 | e.printStackTrace(); |
| 813 | throw new SimError("Couldn't create settings-accepting object"+ |
| 814 | " for '" + className + "'\n" + "cause: " + e.getCause(), e); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | return o; |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * Returns a Class object for the name of class of throws SettingsError |
no test coverage detected