Java tutorial
/* * Copyright 2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.bluecloud.ioc.classloader; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * @author <a href="mailto:hanlu0808@gmail.com">Hanlu</a> * */ public final class ClassHandler { private static Log log = LogFactory.getLog(ClassHandler.class); /** * @param classloader * */ private ClassHandler() { } /** * <h3>Class</h3> ?classNameClass * * @param className * ?Class?? * @param isNewInstance * ? * @return ?ClassLoader?ClassLoader?? * ClassNotFoundException * ???isNewInstancetrueObjectfalseClass<? extends Object> * @throws InstantiationException * @throws IllegalAccessException * @throws ClassNotFoundException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws SecurityException * @throws IllegalArgumentException */ public static Object getClass(String className, boolean isNewInstance) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, SecurityException, InvocationTargetException, NoSuchMethodException { ClassLoader classloader = Thread.currentThread().getContextClassLoader(); Class<?> clazz = classloader.loadClass(className); if (isNewInstance) { return getClassNewInstrance(clazz); } return clazz; } /** * <h3>Class</h3> * * @param clazz * @return * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws SecurityException * @throws IllegalArgumentException */ private static Object getClassNewInstrance(Class<?> clazz) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, SecurityException, InvocationTargetException, NoSuchMethodException { if (clazz == null) { throw new ClassNotFoundException(); } return clazz.getConstructor((Class[]) null).newInstance((Object[]) null); } /** * <h3></h3> ?Class Instance * * @param className * ?? * @param index * Class?constructorIndex?0Class? * 0 * @param initargs * ?? * @return classNameClassObject * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws NoSuchMethodException * @throws SecurityException */ public static Object createObject(String className, int index, Object[] initargs) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchMethodException { Class<?> clazz = (Class<?>) getClass(className, false); if (index < 0 || index > clazz.getConstructors().length - 1) { index = 0; } Constructor<?> constructor = clazz.getConstructors()[index]; return constructor.newInstance(initargs); } /** * <h3></h3> ?Class Instance * * @param className * ?? * @param parameterTypes * ?? * @param initargs * ?? * @return classNameClassObject * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws SecurityException * @throws IllegalArgumentException */ public static Object createObject(String className, Class<?>[] parameterTypes, Object[] initargs) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, SecurityException, InvocationTargetException, NoSuchMethodException { Class<?> clazz = (Class<?>) getClass(className, false); return clazz.getConstructor(parameterTypes).newInstance(initargs); } /** * <h3>Class</h3> * * @param obj * * @param methodName * ?? * @param args * ? * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InvocationTargetException */ public static void invokeMethod(Object obj, String methodName, Object args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class<? extends Object> clazz = obj.getClass(); try { if (args != null) { clazz.getMethod(methodName, args.getClass()).invoke(obj, args); } } catch (SecurityException e) { log.error(e.getMessage(), e); } catch (NoSuchMethodException e) { for (Method method : clazz.getMethods()) { if (method.getName().equals(methodName) && method.getParameterTypes().length == 1) { method.invoke(obj, args); } } } } }