Java tutorial
/** * Flamingo HDFS File Uploader - a tool to upload from datasource to datasource and schedule jobs * * Copyright (C) 2011-2012 Cloudine. * * This file is part of Flamingo HDFS File Uploader. * * Flamingo HDFS File Uploader is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Flamingo HDFS File Uploader 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.openflamingo.uploader.util; import org.apache.commons.beanutils.PropertyUtils; import org.openflamingo.uploader.exception.SystemException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.beans.PropertyDescriptor; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Collection; import java.util.Set; /** * Java Reflection ?. ? ? Java Reflection ? API ? . * * @author Edward KIM * @version 0.1 */ public class ReflectionUtils { /** * SLF4J Logging */ private final static Logger logger = LoggerFactory.getLogger(ReflectionUtils.class); /** * ? ? {@link Class} ? . ? <tt>false</tt> . * * @param className ? * @param classLoader ? ?(<tt>null</tt>? ? ?) * @return ? */ public static boolean isPresent(String className, ClassLoader classLoader) { return org.springframework.util.ClassUtils.isPresent(className, classLoader); } /** * ? ?("int" ?) ? ?("String[]" ?)? Class ?? <tt>Class.forName()</tt>? . ? * ? ? . Thread Context Class Loader ? ClassUtils Class Loader?. * * @param className ? * @return ?? Class ? * @throws SystemException ? ? * @see Class#forName(String, boolean, ClassLoader) * @see #getDefaultClassLoader() */ public static Class forName(String className) throws SystemException { try { return org.springframework.util.ClassUtils.forName(className); } catch (ClassNotFoundException e) { throw new SystemException( "[" + className + "] ? ? ?? CLASSPATH? ? .", e); } } /** * ? ?("int" ?) ? ?("String[]" ?)? Class ?? <tt>Class.forName()</tt>? . * * @param className ? * @param classLoader ? ?(<tt>null</tt>? ? ?) * @return ?? Class ? * @throws ClassNotFoundException ? ? * @throws LinkageError ? ?? * @see Class#forName(String, boolean, ClassLoader) */ public static Class forName(String className, ClassLoader classLoader) throws ClassNotFoundException, LinkageError { return org.springframework.util.ClassUtils.forName(className, classLoader); } /** * ?? Class ? . ? "int" ? ? ? ? "String[]" ? ?? ?. * <p/><pre> * Class clazz = ReflectionUtils.resolveClassName("com.skt.ccme.pilot.util.ReflectionUtils", * Thread.currentThread().getContextClassLoader()); * String className = clazz.getName(); // Name is "com.skt.ccme.pilot.util.ReflectionUtils" * </pre> * * @param className ? * @param classLoader ? ?(<tt>null</tt>? ? ?) * @return ?? Class ? * @throws IllegalArgumentException ?? (, ? ?? ) * @see #forName(String, ClassLoader) */ public static Class resolveClassName(String className, ClassLoader classLoader) throws IllegalArgumentException { return org.springframework.util.ClassUtils.resolveClassName(className, classLoader); } /** * ? ? ? ? . * <p/><pre> * Class clazz = ReflectionUtils.resolvePrimitiveClassName("int"); * String className = clazz.getName(); // Name is "int" * </pre> * * @param name ? ? ? * @return ? ? ? ? ? ? ? <tt>null</tt> */ public static Class resolvePrimitiveClassName(String name) { return org.springframework.util.ClassUtils.resolvePrimitiveClassName(name); } /** * ? ? ? ?? . * * @param className ? * @return ? ? */ public static String getShortName(String className) { return org.springframework.util.ClassUtils.getShortName(className); } /** * ? ? ? ?? . * * @param clazz ? ?? ? * @return ? ?? ? ? */ public static String getShortName(Class clazz) { return org.springframework.util.ClassUtils.getShortName(clazz); } /** * ? ? Property ? ? ?? ? ? ?? . "FooBah" "fooBah", "X" "x" ?. "URL"? "URL"? * . * * @param clazz ? * @return ? ? Property ? ??? ? ? * @see java.beans.Introspector#decapitalize(String) */ public static String getShortNameAsProperty(Class clazz) { return org.springframework.util.ClassUtils.getShortNameAsProperty(clazz); } /** * ?? ? ?? . "Foo"? "Foo.class" ?. * * @param clazz ? * @return ".class" ?? ? */ public static String getClassFileName(Class clazz) { return org.springframework.util.ClassUtils.getClassFileName(clazz); } /** * ?? qualified ? . "ClassUtils"? "com.skt.ccme.pilot.util.ClassUtils" . * * @param clazz ? * @return ?? qualified */ public static String getQualifiedName(Class clazz) { return org.springframework.util.ClassUtils.getQualifiedName(clazz); } /** * ? qualified ? . ?? qualified ??/? + "." + ?. * * @param method * @return qualified */ public static String getQualifiedMethodName(Method method) { return org.springframework.util.ClassUtils.getQualifiedMethodName(method); } /** * ?? signature ?? . ? <tt>NoSuchMethodException</tt>? * "<tt>false</tt>" . * * @param clazz ? * @param paramTypes ? ? * @return ?? ?? */ public static boolean hasConstructor(Class clazz, Class[] paramTypes) { return org.springframework.util.ClassUtils.hasConstructor(clazz, paramTypes); } /** * ?? signature ?? . ?? <tt>null</tt>? . ? * <tt>NoSuchMethodException</tt>? "<tt>null</tt>" . * * @param clazz ? * @param paramTypes ? ? * @return ?? ??, <tt>null</tt> */ public static Constructor getConstructorIfAvailable(Class clazz, Class[] paramTypes) { return org.springframework.util.ClassUtils.getConstructorIfAvailable(clazz, paramTypes); } /** * ?? signature . ? <tt>NoSuchMethodException</tt>? "<tt>false</tt>" * . * * @param clazz ? * @param methodName * @param paramTypes ? ? * @return ?? */ public static boolean hasMethod(Class clazz, String methodName, Class[] paramTypes) { return org.springframework.util.ClassUtils.hasMethod(clazz, methodName, paramTypes); } /** * ?? signature . <tt>null</tt>? . ? * <tt>NoSuchMethodException</tt>? "<tt>null</tt>" . * * @param clazz ? * @param methodName * @param paramTypes ? ? * @return , <tt>null</tt> */ public static Method getMethodIfAvailable(Class clazz, String methodName, Class[] paramTypes) { return org.springframework.util.ClassUtils.getMethodIfAvailable(clazz, methodName, paramTypes); } /** * ? ? ?? 1 ?? ?. ? non-public ?. * * @param clazz ? * @param methodName * @return ?? ? <tt>true</tt> */ public static boolean hasAtLeastOneMethodWithName(Class clazz, String methodName) { return org.springframework.util.ClassUtils.hasAtLeastOneMethodWithName(clazz, methodName); } /** * ?? static . * * @param methodName static * @param clazz ?? ? * @param args ? * @return static static , <tt>null</tt> * @throws IllegalArgumentException ? ? <tt>null</tt>? */ public static Method getStaticMethod(Class clazz, String methodName, Class[] args) { return org.springframework.util.ClassUtils.getStaticMethod(clazz, methodName, args); } /** * ?/?? ? ?? ? . * * @param classes ? * @return "[com.foo.Bar, com.foo.Baz]" ?? ? */ public static String classNamesToString(Class[] classes) { return org.springframework.util.ClassUtils.classNamesToString(classes); } /** * Collection ? ?/??? ?? ? . * * @param collection ? ?? Collection * @return "[com.foo.Bar, com.foo.Baz]" ?? ? */ public static String classNamesToString(Collection collection) { return org.springframework.util.ClassUtils.classNamesToString(collection); } /** * ? ?? . * * @param instance ? * @return ?? */ public static Class[] getAllInterfaces(Object instance) { return org.springframework.util.ClassUtils.getAllInterfaces(instance); } /** * ?? ?? . * * @param clazz ? * @return ? ?? */ public static Class[] getAllInterfacesForClass(Class clazz) { return org.springframework.util.ClassUtils.getAllInterfacesForClass(clazz); } /** * ? ?? . * * @param instance ? * @return ? ??? {@link java.util.Set} */ public static Set getAllInterfacesAsSet(Object instance) { return org.springframework.util.ClassUtils.getAllInterfacesAsSet(instance); } /** * ? ?? . * * @param clazz ? * @return ? ??? {@link java.util.Set} */ public static Set getAllInterfacesForClassAsSet(Class clazz) { return org.springframework.util.ClassUtils.getAllInterfacesForClassAsSet(clazz); } /** * ? ? . * * @return ? ?( <tt>null</tt>? ? ?) */ public static ClassLoader getDefaultClassLoader() { return org.springframework.util.ClassUtils.getDefaultClassLoader(); } /** * ?? ?? . <tt>null</tt>? . * * @param type ? * @param name * @param paramTypes ? * @return */ public static Method findMethod(Class type, String name, Class[] paramTypes) { return org.springframework.util.ReflectionUtils.findMethod(type, name, paramTypes); } /** * ?? . <tt>null</tt>? . * * @param type ? * @param name * @return */ public static Method findMethod(Class type, String name) { return org.springframework.util.ReflectionUtils.findMethod(type, name); } /** * ?? ?? . static ? null? . * * @param method * @param instance ? * @return */ public static Object invokeMethod(Method method, Object instance) { if (logger.isTraceEnabled()) { String className = instance.getClass().getName(); logger.trace("{} ?? {} ?.", className, getMethodSignature(method)); } return org.springframework.util.ReflectionUtils.invokeMethod(method, instance); } /** * ? Signature . * * @param method * @return ? Signature */ private static String getMethodSignature(Method method) { String methodName = method.getName(); Class<?>[] classes = method.getParameterTypes(); StringBuilder builder = new StringBuilder(); builder.append("[ "); builder.append(methodName); builder.append("("); for (int i = 0; i < classes.length; i++) { Class<?> aClass = classes[i]; builder.append(aClass.getName()); if (i != (classes.length - 1)) { builder.append(", "); } } builder.append(")"); builder.append(" ]"); return builder.toString(); } /** * ? Signature . * * @param method * @return ? Signature */ private static String getMethodSignature(Method method, Object[] args) { String methodName = method.getName(); Class<?>[] classes = method.getParameterTypes(); StringBuilder builder = new StringBuilder(); builder.append("[ "); builder.append(methodName); builder.append("("); builder.append(getMethodParameter(classes, args)); builder.append(")"); builder.append(" ]"); return builder.toString(); } /** * @param classes * @param args * @return */ private static String getMethodParameter(Class<?>[] classes, Object[] args) { if (args == null) { return ""; } StringBuilder builder = new StringBuilder(); for (int i = 0; i < classes.length; i++) { Class<?> aClass = classes[i]; builder.append(aClass.getName()); builder.append("("); builder.append(args[i]); builder.append(")"); if (i != (classes.length - 1)) { builder.append(", "); } } return builder.toString(); } /** * ? ? ? . * * @param args ? * @return ? ? */ private static String getMethodParameter(Object[] args) { if (args == null) { return ""; } StringBuilder builder = new StringBuilder(); for (int i = 0; i < args.length; i++) { Class<?> aClass = args[i].getClass(); builder.append(aClass.getName()); builder.append("("); builder.append(args[i]); builder.append(")"); if (i != (args.length - 1)) { builder.append(", "); } } return builder.toString(); } /** * ?? ?? . <tt>static</tt> ? <tt>null</tt>? . * * @param method * @param instance ? * @param args ? ?? * @return */ public static Object invokeMethod(Method method, Object instance, Object[] args) { if (logger.isTraceEnabled()) { String className = instance.getClass().getName(); logger.trace("{} ?? {} ?.", className, getMethodSignature(method, args)); } return org.springframework.util.ReflectionUtils.invokeMethod(method, instance, args); } /** * . * * @param className ? * @param methodName * @param parameterType ? * @param parameterValue ? * @return * @throws SystemException */ public static Object invokeMethod(String className, String methodName, String parameterType, Object parameterValue) throws SystemException { if (logger.isTraceEnabled()) { logger.trace("{} ?? [{}({}({}))] ?.", new Object[] { className, methodName, parameterType, parameterValue }); } Class clazz = null; try { clazz = ReflectionUtils.forName(className); Class paramType = ReflectionUtils.forName(parameterType); Method invokableMethod = ReflectionUtils.findMethod(clazz, methodName, new Class[] { paramType }); return ReflectionUtils.invokeMethod(invokableMethod, clazz.newInstance(), new Object[] { parameterValue }); } catch (IllegalAccessException e) { throw new SystemException("[" + className + "]? .", e); } catch (InstantiationException e) { throw new SystemException("[" + className + "]? ? ? .", e); } } /** * ?? . * * @param instance ? * @param name * @param arg ?? * @return * @throws NoSuchMethodException * @throws IllegalAccessException ? * @throws java.lang.reflect.InvocationTargetException * ? ? */ public static Object invokeMethod(Object instance, String name, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { if (logger.isTraceEnabled()) { logger.trace("{} ?? [{}({}({}))] ?.", new Object[] { instance.getClass().getName(), name, arg.getClass().getName(), arg }); } return org.apache.commons.beanutils.MethodUtils.invokeMethod(instance, name, arg); } /** * @param instance ? * @param name * @param args ?? * @return * @throws NoSuchMethodException * @throws IllegalAccessException ? * @throws java.lang.reflect.InvocationTargetException * ? ? */ public static Object invokeMethod(Object instance, String name, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { if (logger.isTraceEnabled()) { String methodParameter = getMethodParameter(args); logger.trace("{} ?? [{}({})] ?.", new Object[] { instance.getClass().getName(), name, methodParameter }); } return org.apache.commons.beanutils.MethodUtils.invokeMethod(instance, name, args); } /** * @param instance ? * @param name * @param args ?? * @param paramTypes ? * @return * @throws NoSuchMethodException * @throws IllegalAccessException ? * @throws java.lang.reflect.InvocationTargetException * ? ? */ public static Object invokeMethod(Object instance, String name, Object[] args, Class[] paramTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { if (logger.isTraceEnabled()) { String methodParameter = getMethodParameter(paramTypes, args); logger.trace("{} ?? [{}({})] ?.", new Object[] { instance.getClass().getName(), name, methodParameter }); } return org.apache.commons.beanutils.MethodUtils.invokeMethod(instance, name, args, paramTypes); } /** * @param instance ? * @param name * @param arg ?? * @return * @throws NoSuchMethodException * @throws IllegalAccessException ? * @throws java.lang.reflect.InvocationTargetException * ? ? */ public static Object invokeExactMethod(Object instance, String name, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return org.apache.commons.beanutils.MethodUtils.invokeExactMethod(instance, name, arg); } /** * @param instance ? * @param name * @param args ?? * @return * @throws NoSuchMethodException * @throws IllegalAccessException ? * @throws java.lang.reflect.InvocationTargetException * ? ? */ public static Object invokeExactMethod(Object instance, String name, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return org.apache.commons.beanutils.MethodUtils.invokeExactMethod(instance, name, args); } /** * @param instance ? * @param name * @param args ?? * @param paramTypes ? * @return * @throws NoSuchMethodException * @throws IllegalAccessException ? * @throws java.lang.reflect.InvocationTargetException * ? ? */ public static Object invokeExactMethod(Object instance, String name, Object[] args, Class[] paramTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return org.apache.commons.beanutils.MethodUtils.invokeExactMethod(instance, name, args, paramTypes); } /** * ? . * * @param clazz ? * @return * @throws IllegalArgumentException ? */ public static Method[] getAllDeclaredMethods(Class clazz) throws IllegalArgumentException { return org.springframework.util.ReflectionUtils.getAllDeclaredMethods(clazz); } /** * ?? ? ?. * * @param className Qualified Full Class Name * @return ? * @throws SystemException ? ? */ public static Object newInstance(String className) throws SystemException { Class clazz = null; try { clazz = ReflectionUtils.forName(className); return clazz.newInstance(); } catch (Exception e) { throw new SystemException("[" + className + "]? ? ? .", e); } } /** * ?? ? Setter . * * @param instance ? * @param fieldName ? * @return Setter * @throws SystemException Setter */ public static Method getSetterMethod(Object instance, String fieldName) throws SystemException { try { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(instance, fieldName); return propertyDescriptor.getWriteMethod(); } catch (Exception e) { throw new SystemException("[" + instance.getClass().getName() + "] ?? [" + fieldName + "] ? Setter ? .", e); } } /** * ?? ? Getter . * * @param instance ? * @param fieldName ? * @return Getter * @throws SystemException Getter */ public static Method getGetterMethod(Object instance, String fieldName) throws SystemException { try { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(instance, fieldName); return propertyDescriptor.getReadMethod(); } catch (Exception e) { throw new SystemException("[" + instance.getClass().getName() + "] ?? [" + fieldName + "] ? Getter ? .", e); } } }