All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.reprezen.genflow.api.template.builders.BuilderUtil Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright © 2013, 2016 Modelsolv, Inc.
 * All Rights Reserved.
 *
 * NOTICE: All information contained herein is, and remains the property
 * of ModelSolv, Inc. See the file license.html in the root directory of
 * this project for further information.
 *******************************************************************************/
package com.reprezen.genflow.api.template.builders;

import java.lang.reflect.InvocationTargetException;
import java.util.Optional;

public class BuilderUtil {
	public static Optional> getClass(String className) {
		try {
			return Optional.>of(Class.forName(className));
		} catch (ClassNotFoundException e) {
			try {
				return Optional.>of(Thread.currentThread().getContextClassLoader().loadClass(className));
			} catch (ClassNotFoundException e1) {
				return Optional.empty();
			}
		}
	}

	public static  Optional getInstance(String className) {
		return getInstance(className, BuilderUtil.class.getClassLoader());
	}

	public static  Optional getInstance(String className, ClassLoader classLoader) {
		Class clazz = null;
		try {
			clazz = classLoader.loadClass(className);
		} catch (ClassNotFoundException e) {
		}
		Object instance = null;
		if (clazz != null) {
			try {
				instance = clazz.getConstructor().newInstance();
			} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
					| InvocationTargetException | NoSuchMethodException | SecurityException e) {
				e.printStackTrace();
			}
		}
		@SuppressWarnings("unchecked")
		T castInstance = (T) instance;
		return Optional.ofNullable(castInstance);
	}

	public static String simpleName(Object o) {
		return o.getClass().getSimpleName();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy