/* * ClassUtils.java * * * Created on January 25, 2006, 12:08 AM */ /* Copyright (c) 2006, Michael D. Parker All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of StatBuff Studios nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // package com.statbuff.util; /** * Static utility methods for dynamic intantiation of class instances. The methods * here are very basic and use the default class loader. * * @author Michael D. Parker * @version 1.0 */ public final class ClassUtils { // TODO implement optional logging // prevent instantiation private ClassUtils() { } /** * Given a fully qualified Java class name, attempts to instantiate an * instance of that class. Note that no exceptions are thrown by this * method. If any exceptions are thrown during the instantiation process, * this method will simply return null. * @param className fully qualified Java class name of the class that will * be instantiated (ex: java.io.IOException). */ public static Object instantiateObject(String className) { return instantiate(findClass(className)); } /* * Given a Class object, attempts to instantiate an instance of that class. * Note that no exceptions are thrown by this method. If any exceptions are * thrown during the instantiation process, this method will simply return * null. * @param clazz a Class instance which represents the class of which an * instance will be instantiated. */ public static Object instantiateObject(Class clazz) { if(null == clazz) { return null; } Object obj = null; try { obj = clazz.newInstance(); // TODO log exceptions } catch(InstantiationException ie) { ie.printStackTrace(); } catch(IllegalAccessException iae) { iae.printStackTrace(); } return obj; } /** * Given a fully qualified Java class name, attempts to instantiate an instance * of that class if and only if it is part of the inheritance heirarchy of a * super class named superName, which also muse be a fully * qualified Java class name. Note that no exceptions are thrown by this * method. If any exceptions are thrown during the instantiation process, * this method will simply return null. * @param className fully qualified Java class name of the class that will * be instantiated (ex: java.io.IOException). * @param superName fully qualified Java class name of the super class the * class instance should extend (ex: java.lang.Exception) */ public static Object instantiateIfExtends(String className, String superName) { Class clazz = findClass(className); Class superClass = findClass(superName); if(null == clazz || null == superClass) { return null; } Object obj = null; for(Class current = clazz.getSuperclass(); current != null; current = current.getSuperclass()) { if(current == superClass) { obj = instantiate(clazz); if(null == obj) { // the class is extends superName, but instantiation // failed. return null; } break; } } /* if(null == obj) // a null object at this point means that className does not extend // superName - so you can write that to a log here. */ return obj; } /** * Given a fully qualified Java class name, attempts to instantiate an instance * of that class if and only if it or any of its super classes implements the * inteface named ifaceName, which also muse be a fully * qualified Java class name. Note that no exceptions are thrown by this * method. If any exceptions are thrown during the instantiation process, * this method will simply return null. * @param className fully qualified Java class name of the class that will * be instantiated (ex: java.io.IOException). * @param ifaceName fully qualified Java class name of the interface the * class instance should implement (ex: java.lang.Throwable) */ public static Object instantiateIfImplements(String className, String ifaceName) { Class clazz = findClass(className); Class iface = findClass(ifaceName); if(null == clazz || null == iface) { return null; } Object obj = null; Outer: for(Class current = clazz; current != null; current = current.getSuperclass()) { Class[] ifaces = current.getInterfaces(); for(Class c : ifaces) { if(c == iface) { obj = instantiate(clazz); if(null == obj) { // the class is an implementatin of ifaceName, but instantiation // failed. return null; } break Outer; } } } /* if(null == obj) // a null object at this point means that className does not implement // ifaceName - so you can write that to a log here. */ return obj; } /** * Given a fully qualified Java class name, attempts to instantiate an instance * of that class if and only if it directly subclasses the super class named * superName, which also muse be a fully qualified Java class * name. Note that no exceptions are thrown by this method. If any exceptions * are thrown during the instantiation process, this method will simply * return null. * @param className fully qualified Java class name of the class that will * be instantiated (ex: java.io.IOException). * @param superName fully qualified Java class name of the super class the * class instance should extend (ex: java.lang.Exception) */ public static Object instantiateIfSelfExtends(String className, String superName) { Class clazz = findClass(className); Class superClass = findClass(superName); if(null == clazz || null == superClass) { return null; } if(clazz.getSuperclass() == superClass) { return instantiate(clazz); } /* else // className does not extend superName, so you can log that here */ return null; } /** * Given a fully qualified Java class name, attempts to instantiate an instance * of that class if and only if it (not its super classes) implements the * inteface named ifaceName, which also muse be a fully * qualified Java class name. Note that no exceptions are thrown by this * method. If any exceptions are thrown during the instantiation process, * this method will simply return null. * @param className fully qualified Java class name of the class that will * be instantiated (ex: java.io.IOException). * @param ifaceName fully qualified Java class name of the interface the * class instance should implement (ex: java.lang.Throwable) */ public static Object instantiateIfSelfImplements(String className, String ifaceName) { Class clazz = findClass(className); Class iface = findClass(ifaceName); if(null == clazz || null == iface) { return null; } Object obj = null; Class[] ifaces = clazz.getInterfaces(); for(Class c : ifaces) { if(c == iface) { obj = instantiate(clazz); if(null == obj) { // the class is an implementatin of ifaceName, but instantiation // failed. return null; } break; } } return obj; } private static Class findClass(String className) { Class clazz = null; try { clazz = Class.forName(className); } catch(ClassNotFoundException cnfe) { // TODO log me cnfe.printStackTrace(); } return clazz; } }