2010-05-25 23:15:35 +02:00
|
|
|
/*
|
2011-08-07 12:14:40 +02:00
|
|
|
* Copyright (c) 2010, 2011
|
2010-05-25 23:15:35 +02:00
|
|
|
*
|
2011-08-07 12:14:40 +02:00
|
|
|
* Robert von Burg <eitch@eitchnet.ch>
|
2010-05-25 23:15:35 +02:00
|
|
|
*
|
2011-08-07 12:14:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Privilege.
|
|
|
|
*
|
|
|
|
* Privilege is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Privilege is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Privilege. If not, see <http://www.gnu.org/licenses/>.
|
2010-05-25 23:15:35 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package ch.eitchnet.privilege.helper;
|
|
|
|
|
|
|
|
import ch.eitchnet.privilege.i18n.PrivilegeException;
|
|
|
|
|
|
|
|
/**
|
2010-11-27 22:54:46 +01:00
|
|
|
* The {@link ClassHelper} class is a helper to instantiate classes using reflection
|
|
|
|
*
|
2011-08-07 12:14:40 +02:00
|
|
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
2010-05-25 23:15:35 +02:00
|
|
|
*/
|
|
|
|
public class ClassHelper {
|
|
|
|
|
2010-11-27 22:54:46 +01:00
|
|
|
/**
|
|
|
|
* Returns an instance of the class' name given by instantiating the class through an empty arguments constructor
|
|
|
|
*
|
|
|
|
* @param <T>
|
|
|
|
* the type of the class to return
|
|
|
|
* @param className
|
|
|
|
* the name of a class to instantiate through an empty arguments constructor
|
|
|
|
*
|
|
|
|
* @return the newly instantiated object from the given class name
|
|
|
|
*
|
|
|
|
* @throws PrivilegeException
|
|
|
|
* if the class could not be instantiated
|
|
|
|
*/
|
2010-05-25 23:15:35 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2010-11-27 22:54:46 +01:00
|
|
|
public static <T> T instantiateClass(String className) throws PrivilegeException {
|
2010-05-25 23:15:35 +02:00
|
|
|
try {
|
|
|
|
|
|
|
|
Class<T> clazz = (Class<T>) Class.forName(className);
|
|
|
|
|
|
|
|
return clazz.getConstructor().newInstance();
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new PrivilegeException("The class " + className + " could not be instantiated: ", e);
|
|
|
|
}
|
|
|
|
}
|
2010-05-30 00:32:53 +02:00
|
|
|
|
2010-11-27 22:54:46 +01:00
|
|
|
/**
|
|
|
|
* Instantiates an object for the given {@link Class} using an empty arguments constructor
|
|
|
|
*
|
|
|
|
* @param <T>
|
|
|
|
* the type of the class to return
|
|
|
|
* @param clazz
|
|
|
|
* the {@link Class} from which a new object is to be instantiated using an empty arguments constructor
|
|
|
|
*
|
|
|
|
* @return the newly instantiated object from the given {@link Class}
|
|
|
|
*
|
|
|
|
* @throws PrivilegeException
|
|
|
|
* if the {@link Class} could not be instantiated
|
|
|
|
*/
|
|
|
|
public static <T> T instantiateClass(Class<T> clazz) throws PrivilegeException {
|
2010-05-30 00:32:53 +02:00
|
|
|
try {
|
|
|
|
|
|
|
|
return clazz.getConstructor().newInstance();
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new PrivilegeException("The class " + clazz.getName() + " could not be instantiated: ", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-27 22:54:46 +01:00
|
|
|
/**
|
|
|
|
* Loads the {@link Class} object for the given class name
|
|
|
|
*
|
|
|
|
* @param <T>
|
|
|
|
* the type of {@link Class} to return
|
|
|
|
* @param className
|
|
|
|
* the name of the {@link Class} to load and return
|
|
|
|
*
|
|
|
|
* @return the {@link Class} object for the given class name
|
|
|
|
*
|
|
|
|
* @throws PrivilegeException
|
|
|
|
* if the class could not be instantiated
|
|
|
|
*/
|
2010-05-30 00:32:53 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2010-11-27 22:54:46 +01:00
|
|
|
public static <T> Class<T> loadClass(String className) throws PrivilegeException {
|
2010-05-30 00:32:53 +02:00
|
|
|
try {
|
|
|
|
|
|
|
|
Class<T> clazz = (Class<T>) Class.forName(className);
|
|
|
|
|
|
|
|
return clazz;
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new PrivilegeException("The class " + className + " could not be instantiated: ", e);
|
|
|
|
}
|
|
|
|
}
|
2010-05-25 23:15:35 +02:00
|
|
|
}
|