code: 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;
              }
  
              val script = IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8);
  
              val clazz = AccessController.doPrivileged((PrivilegedAction<Class<T>>) () -> {
                  val classLoader = new GroovyClassLoader(ScriptingUtils.class.getClassLoader(),
                      new CompilerConfiguration(), true);
                  return classLoader.parseClass(script);
              });
  
              LOGGER.debug("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 result;
          } catch (final Exception e) {
              LOGGER.error(e.getMessage(), e);
          }
          return null;
      }
docs: 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