private static Object newInstance(
      final JBossServiceConfig serviceConfig,
      final List<ClassReflectionIndex> mBeanClassHierarchy,
      final ClassLoader deploymentClassLoader)
      throws DeploymentUnitProcessingException {
    // set TCCL so that the MBean instantiation happens in the deployment's classloader
    final ClassLoader oldTCCL =
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(deploymentClassLoader);
    try {
      final JBossServiceConstructorConfig constructorConfig = serviceConfig.getConstructorConfig();
      final int paramCount =
          constructorConfig != null ? constructorConfig.getArguments().length : 0;
      final Class<?>[] types = new Class<?>[paramCount];
      final Object[] params = new Object[paramCount];

      if (constructorConfig != null) {
        final Argument[] arguments = constructorConfig.getArguments();
        for (int i = 0; i < paramCount; i++) {
          final Argument argument = arguments[i];
          types[i] = ReflectionUtils.getClass(argument.getType(), deploymentClassLoader);
          params[i] =
              newValue(
                  ReflectionUtils.getClass(argument.getType(), deploymentClassLoader),
                  argument.getValue());
        }
      }
      final Constructor<?> constructor = mBeanClassHierarchy.get(0).getConstructor(types);
      final Object mBeanInstance = ReflectionUtils.newInstance(constructor, params);

      return mBeanInstance;
    } finally {
      // switch back the TCCL
      WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldTCCL);
    }
  }
 private static Value<?> getValue(
     final ValueFactory valueFactory,
     final List<ClassReflectionIndex> mBeanClassHierarchy,
     final ClassLoader classLoader)
     throws DeploymentUnitProcessingException {
   final String methodName = valueFactory.getMethodName();
   final ValueFactoryParameter[] parameters = valueFactory.getParameters();
   final List<Class<?>> paramTypes = new ArrayList<Class<?>>(parameters.length);
   final List<Value<?>> paramValues = new ArrayList<Value<?>>(parameters.length);
   for (ValueFactoryParameter parameter : parameters) {
     final Class<?> attributeTypeValue =
         ReflectionUtils.getClass(parameter.getType(), classLoader);
     paramTypes.add(attributeTypeValue);
     paramValues.add(
         new ImmediateValue<Object>(newValue(attributeTypeValue, parameter.getValue())));
   }
   final Value<Method> methodValue =
       new ImmediateValue<Method>(
           ReflectionUtils.getMethod(
               mBeanClassHierarchy, methodName, paramTypes.toArray(new Class<?>[0]), true));
   return cached(new MethodValue<Object>(methodValue, Values.injectedValue(), paramValues));
 }