Package com.buschmais.xo.spi.reflection

Source Code of com.buschmais.xo.spi.reflection.ClassHelper

package com.buschmais.xo.spi.reflection;

import com.buschmais.xo.api.XOException;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public final class ClassHelper {

    private ClassHelper() {
    }

    public static <T> Class<T> getType(String name) {
        Class<T> type;
        try {
            type = (Class<T>) Class.forName(name);
        } catch (ClassNotFoundException e) {
            throw new XOException("Cannot find class with name '" + name + "'", e);
        }
        return type;
    }

    public static Collection<Class<?>> getTypes(Collection<String> typeNames) {
        Set<Class<?>> types = new HashSet<>();
        for (String typeName : typeNames) {
            types.add(ClassHelper.getType(typeName));
        }
        return types;
    }

    public static <T> T newInstance(Class<T> type) {
        try {
            return type.newInstance();
        } catch (InstantiationException e) {
            throw new XOException("Cannot create instance of type '" + type.getName() + "'", e);
        } catch (IllegalAccessException e) {
            throw new XOException("Access denied to type '" + type.getName() + "'", e);
        }
    }
}
TOP

Related Classes of com.buschmais.xo.spi.reflection.ClassHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.