Code example for Constructor

Methods: newInstance, setAccessible

0
 
    private S prepareInstance(Class<? extends S> serviceClass) {
        try { 
            // TODO Support the SM 
            Constructor<? extends S> constructor = serviceClass.getDeclaredConstructor();
            constructor.setAccessible(true);
            return constructor.newInstance();
        } catch (NoClassDefFoundError e) {
            log.warn("Could not instantiate service class " + serviceClass.getName(), e);
            return null; 
        } catch (InvocationTargetException e) {
            throw new RuntimeException("Error instantiating " + serviceClass, e.getCause());
        } catch (IllegalArgumentException e) {
            throw new RuntimeException("Error instantiating " + serviceClass, e);
        } catch (InstantiationException e) {
            throw new RuntimeException("Error instantiating " + serviceClass, e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Error instantiating " + serviceClass, e);
        } catch (SecurityException e) {
            throw new RuntimeException("Error instantiating " + serviceClass, e);
        } catch (NoSuchMethodException e) {