public static <T> T getObjectInstanceFromGroovyResource(final Resource resource,
                                                            final Class[] constructorArgs,
                                                            final Object[] args,
                                                            final Class<T> expectedType) {
        try {
            if (resource == null) {
                LOGGER.debug("No groovy script is defined");
                return null;
            }
            try (val inputStream = resource.getInputStream();
                 val classLoader = ScriptingUtils.newGroovyClassLoader()) {
                val script = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
                val clazz = classLoader.parseClass(script);
                LOGGER.trace("Preparing constructor arguments [{}] for resource [{}]", args, resource);
                val ctor = clazz.getDeclaredConstructor(constructorArgs);
                val result = ctor.newInstance(args);
                if (!expectedType.isAssignableFrom(result.getClass())) {
                    throw new ClassCastException("Result [" + result
                        + " is of type " + result.getClass()
                        + " when we were expecting " + expectedType);
                }
                return (T) result;
            }
        } catch (final Exception e) {
            LoggingUtils.error(LOGGER, e);
        }
        return null;
    }
Gets object instance from groovy resource.

@param <T>             the type parameter
@param resource        the resource
@param constructorArgs the constructor args
@param args            the args
@param expectedType    the expected type
@return the object instance from groovy resource
core/cas-server-core-scripting/src/main/java/org/apereo/cas/util/scripting/ScriptingUtils.java