Builds a class instance using reflection, by using its classname. The
class must have a zero-arg constructor.

@param classname
the class to build an instance of.
@param params
the parameters
@return the class instance
556315
public static Object buildObjectInstance(String classname, Object[] params) {
		Object rets = null;

		Class<?>[] paramTypes = new Class[params.length];
		for (int x = 0; x < params.length; x++) {
			paramTypes[x] = params[x].getClass();
		}

		try {
			Class<?> clazz = getClass(classname);
			rets = clazz.getConstructor(paramTypes).newInstance(params);

		} catch (Exception e) {
			throw new BundlingProcessException(e.getMessage() + " [The custom class " + classname
					+ " could not be instantiated, check wether it is available on the classpath and"
					+ " verify that it has a zero-arg constructor].\n" + " The specific error message is: "
					+ e.getClass().getName() + ":" + e.getMessage(), e);
		}
		return rets;
	}
769514
public static <T> T newInstance(Class<T> theClass, Class<?>[] parameterTypes,
      Object[] initargs) {
    // Perform some sanity checks on the arguments.
    if (parameterTypes.length != initargs.length) {
      throw new IllegalArgumentException(
          "Constructor parameter types don't match constructor arguments");
    }
    for (int i = 0; i < parameterTypes.length; i++) {
      Class<?> clazz = parameterTypes[i];
      if (!(clazz.isInstance(initargs[i]))) {
        throw new IllegalArgumentException("Object : " + initargs[i]
            + " is not an instance of " + clazz);
      }
    }

    try {
      Constructor<T> meth = theClass.getDeclaredConstructor(parameterTypes);
      meth.setAccessible(true);
      return meth.newInstance(initargs);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
981333
protected function getInstance($classname, $params)
    {
        $components = UIComponentsRegistry::get('ui-components');
        if (is_null($components)) {
            return false;
        }
        $object = $components->first(function ($value, $key) use($classname) {
            return $value instanceof $classname;
        });
        if (!$object) {
            return false;
        }

        $attributes       = array_only(array_get($params, 'data'), ['x', 'y', 'width', 'height', 'disabled', 'id']);
        $attributes['id'] = isset($params['id']) ? $params['id'] : null;

        $object->setAttributes($attributes);
        return $object;
    }
1200002
public static <T> T newInstance(Class<T> clazz, Object... params) throws UtilException {
		if (ArrayUtil.isEmpty(params)) {
			final Constructor<T> constructor = getConstructor(clazz);
			try {
				return constructor.newInstance();
			} catch (Exception e) {
				throw new UtilException(e, "Instance class [{}] error!", clazz);
			}
		}

		final Class<?>[] paramTypes = ClassUtil.getClasses(params);
		final Constructor<T> constructor = getConstructor(clazz, paramTypes);
		if (null == constructor) {
			throw new UtilException("No Constructor matched for parameter types: [{}]", new Object[] { paramTypes });
		}
		try {
			return constructor.newInstance(params);
		} catch (Exception e) {
			throw new UtilException(e, "Instance class [{}] error!", clazz);
		}
	}
62429
private static CustomCommandLine<?> loadCustomCommandLine(String className, Object... params) throws IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, NoSuchMethodException {

		Class<? extends CustomCommandLine> customCliClass =
			Class.forName(className).asSubclass(CustomCommandLine.class);

		// construct class types from the parameters
		Class<?>[] types = new Class<?>[params.length];
		for (int i = 0; i < params.length; i++) {
			Preconditions.checkNotNull(params[i], "Parameters for custom command-lines may not be null.");
			types[i] = params[i].getClass();
		}

		Constructor<? extends CustomCommandLine> constructor = customCliClass.getConstructor(types);

		return constructor.newInstance(params);
	}
108940
public Object newInstance( String name, Object[] params, String[] types ) throws ClassLoaderException {
        Class<?> clazz;
        try {
            clazz = loadClass(name);
        } catch (ClassNotFoundException e) {
            throw new ClassLoaderException(e);
        }
        
        List<Class> paramTypes = new ArrayList<Class>();
        
        if ( types != null ) {
            for( String type: types ) {
                try {
                    paramTypes.add( loadClass(type) );
                } catch (ClassNotFoundException e) {
                    throw new ClassLoaderException(e);
                }
            }
        } else if ( params != null ) {
            for ( Object param: params ) {
                paramTypes.add( param.getClass() );
            }
        } 
        
        if ( params != null ) {
            Constructor<?> constructor;
            try {
                constructor = clazz.getConstructor( paramTypes.toArray( new Class[paramTypes.size()] ) );
            } catch (SecurityException e) {
                throw new ClassLoaderException("Private constructor for " + clazz.getSimpleName(), e);
            } catch (NoSuchMethodException e) {
                throw new ClassLoaderException("No such constructor for " + clazz.getSimpleName(), e);
            } catch (Exception e) {
                throw new ClassLoaderException("Attempted to access constructor for " + clazz.getSimpleName(), e);
            }
            
            try {
                return constructor.newInstance(params);
            } catch (IllegalArgumentException e) {
                throw new ClassLoaderException("Attempted to created new instance of " + clazz.getSimpleName(),e);
            } catch (InstantiationException e) {
                throw new ClassLoaderException("Attempted to created new instance of " + clazz.getSimpleName(),e);
            } catch (IllegalAccessException e) {
                throw new ClassLoaderException("Attempted to created new instance of " + clazz.getSimpleName(),e);
            } catch (InvocationTargetException e) {
                throw new ClassLoaderException("Attempted to created new instance of " + clazz.getSimpleName(),e);
            } catch (Exception e) {
                throw new ClassLoaderException("Attempted to created new instance of " + clazz.getSimpleName(),e);
            }

        } else {
            Constructor<?> constructor;
            try {
                constructor = clazz.getConstructor();
            } catch (SecurityException e) {
                throw new ClassLoaderException("Private constructor for " + clazz.getSimpleName(),e);
            } catch (NoSuchMethodException e) {
                throw new ClassLoaderException("No such constructor for " + clazz.getSimpleName(),e);
            } catch (Exception e) {
                throw new ClassLoaderException("Attempted to access constructor for " + clazz.getSimpleName(), e);
            }
            
            try {
                return constructor.newInstance();
            } catch (IllegalArgumentException e) {
                throw new ClassLoaderException("Attempted to created new instance of " + clazz.getSimpleName(),e);
            } catch (InstantiationException e) {
                throw new ClassLoaderException("Attempted to created new instance of " + clazz.getSimpleName(),e);
            } catch (IllegalAccessException e) {
                throw new ClassLoaderException("Attempted to created new instance of " + clazz.getSimpleName(),e);
            } catch (InvocationTargetException e) {
                throw new ClassLoaderException("Attempted to created new instance of " + clazz.getSimpleName(),e);
            } catch (Exception e) {
                throw new ClassLoaderException("Attempted to created new instance of " + clazz.getSimpleName(),e);
            }
        }
    }
1081977
@SuppressWarnings("unchecked")
	public static <T> T get(Class<T> clazz, Object... params) {
		T obj = (T) pool.get(clazz);

		if (null == obj) {
			synchronized (Singleton.class) {
				obj = (T) pool.get(clazz);
				if (null == obj) {
					obj = ReflectUtil.newInstance(clazz, params);
					pool.put(clazz, obj);
				}
			}
		}

		return obj;
	}
1014179
public JMethod getConstructor(JClass []param)
  {
    JMethod []ctors = getConstructors();

    loop:
    for (int i = 0; i < ctors.length; i++) {
      JClass []args = ctors[i].getParameterTypes();

      if (args.length != param.length)
        continue loop;

      for (int j = 0; j < args.length; j++)
        if (! args[i].equals(param[j]))
          continue loop;

      return ctors[i];
    }

    return null;
  }
665427
public Function build(Class<?> cls) {
        EdmFunction edmFunction = cls.getAnnotation(EdmFunction.class);
        EdmReturnType edmReturnType = cls.getAnnotation(EdmReturnType.class);
        if (edmReturnType == null) {
            throw new IllegalArgumentException(
                    "The class must have EdmReturnType: " + cls.getName());
        }
        Set<Parameter> parameters = new HashSet<>();
        for (Field field : cls.getDeclaredFields()) {
            EdmParameter parameterAnnotation = field.getAnnotation(EdmParameter.class);
            if (parameterAnnotation != null) {
                String parameterName = isNullOrEmpty(parameterAnnotation.name())
                        ? field.getName() : parameterAnnotation.name();
                String parameterType = isNullOrEmpty(parameterAnnotation.type())
                        ? field.getType().getSimpleName() : parameterAnnotation.type();
                parameters.add(new ParameterImpl.Builder()
                        .setMaxLength(parameterAnnotation.maxLength())
                        .setName(parameterName)
                        .setNullable(parameterAnnotation.nullable())
                        .setPrecision(parameterAnnotation.precision())
                        .setScale(parameterAnnotation.scale())
                        .setSRID(parameterAnnotation.srid())
                        .setType(parameterType)
                        .setUnicode(parameterAnnotation.unicode())
                        .setJavaField(field)
                        .build());
            }
        }

        return new FunctionImpl.Builder()
                .setBound(edmFunction.isBound())
                .setComposable(edmFunction.isComposable())
                .setEntitySetPath(edmFunction.entitySetPath())
                .setName(edmFunction.name())
                .setParameters(parameters)
                .setReturnType(edmReturnType.type())
                .setNamespace(edmFunction.namespace())
                .setJavaClass(cls)
                .build();
    }
829311
@VisibleForTesting
    static Method getMethod(Class<?> cls, String name, Class<?>... params) {
        Method m;
        try {
            m = cls.getDeclaredMethod(name, params);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        m.setAccessible(true);
        return m;
    }
6979
public function buildFromClass($classname)
	{
		$reflectionClass = new ReflectionClass($classname);

		$classDefinition = new ClassDefinition();
		$classDefinition->name = $reflectionClass->getName();
		$classDefinition->namespace = $reflectionClass->getNamespaceName();

		$reflectionProperties = $this->classPropertyFinder->findProperties($reflectionClass);

		foreach ($reflectionProperties as $reflectionProperty) {
			$classDefinition->properties[$reflectionProperty->getName()] = $this->buildPropertyDefinition($reflectionProperty);
		}

		return $classDefinition;
	}
1230228
public function getOrCreate($className, $params = array())
    {
        $key = $className . join('-', $params);

        if (isset($this->mapping[$key])) {
            return $this->mapping[$key];
        }

        if (method_exists($this, $this->getMethodName($className))) {
            $callback = array($this, $this->getMethodName($className));
            $instance =  call_user_func_array($callback, $params);
        } else {
            $instance = new $className;
        }

        return $this->mapping[$key] = $instance;
    }
437548
static <T> Optional<Class<? extends T>> getClass(final String className) {
        try {
            @SuppressWarnings("unchecked")
            final Optional<Class<? extends T>> clazz = Optional.<Class<? extends T>>of((Class<T>) Class.forName(className));
            return clazz;
        } catch (final ClassNotFoundException e) {
            return Optional.empty();
        }
    }
286810
public Object newInstance( String name, Object... params ) throws ClassLoaderException {
        return newInstance( name, params, null );
    }
47478
public function newInstance($className, array $args = array())
    {
        // create and return a new instance
        $reflectionClass = $this->newReflectionClass($className);
        return $reflectionClass->newInstanceArgs($args);
    }
636048
public function newInstance($className, array $args = array())
    {

        // load the reflection data for the passed class name
        $reflectionClass = $this->getReflectionClass($className);

        // query whether or not the class has a constructor
        if ($reflectionClass->hasMethod($methodName = '__construct') &&
            $reflectionClass->getMethod($methodName)->getParameters() &&
            sizeof($args) > 0
        ) {
            return $reflectionClass->newInstanceArgs($args);
        }

        // return the instance without calling the constructor
        return $reflectionClass->newInstance();
    }
704824
public QueueReference build(Map<String, String> params) throws IOException {
        return build(params, null,false);
    }