Java tutorial
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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 io.uengine.util; import io.uengine.common.exception.ServiceException; import org.apache.commons.beanutils.PropertyUtils; import org.slf4j.helpers.MessageFormatter; 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 Utilty. ? ? Java Reflection ? API ? . * * @author Byoung Gon, Kim * @version 0.1 */ public class ReflectionUtils { /** * ? ? {@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 io.uengine.common.exception.ServiceException ? ? * @see Class#forName(String, boolean, ClassLoader) * @see #getDefaultClassLoader() */ public static Class forName(String className) throws ServiceException { try { return org.springframework.util.ClassUtils.forName(className, Thread.currentThread().getContextClassLoader()); } catch (ClassNotFoundException e) { String message = MessageFormatter.format("Cannot find '{}' in CLASSPATH", className).getMessage(); throw new ServiceException(message, 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("ReflectionUtils", * Thread.currentThread().getContextClassLoader()); * String className = clazz.getName(); // Name is "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 name? . "<tt>ClassUtils</tt>"? "org.opencloudengine.flamingo2.manager.util.ClassUtils" . * * @param clazz ? * @return ?? qualified name */ 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 Set} */ public static Set getAllInterfacesAsSet(Object instance) { return org.springframework.util.ClassUtils.getAllInterfacesAsSet(instance); } /** * ? ?? . * * @param clazz ? * @return ? ??? {@link 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) { 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) { return org.springframework.util.ReflectionUtils.invokeMethod(method, instance, args); } /** * . * * @param className ? * @param methodName * @param parameterType ? * @param parameterValue ? * @return * @throws io.uengine.common.exception.ServiceException */ public static Object invokeMethod(String className, String methodName, String parameterType, Object parameterValue) throws ServiceException { 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) { String message = MessageFormatter.format("Cannot access '{}'", className).getMessage(); throw new ServiceException(message, e); } catch (InstantiationException e) { String message = MessageFormatter.format("Cannot create a instance of '{}'", className).getMessage(); throw new ServiceException(message, e); } } /** * ?? . * * @param instance ? * @param name * @param arg ?? * @return * @throws NoSuchMethodException * @throws IllegalAccessException ? * @throws InvocationTargetException ? ? */ public static Object invokeMethod(Object instance, String name, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return org.apache.commons.beanutils.MethodUtils.invokeMethod(instance, name, arg); } /** * ?? . * * @param instance ? * @param name * @param args ?? * @return * @throws NoSuchMethodException * @throws IllegalAccessException ? * @throws InvocationTargetException ? ? */ public static Object invokeMethod(Object instance, String name, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return org.apache.commons.beanutils.MethodUtils.invokeMethod(instance, name, args); } /** * ?? . * * @param instance ? * @param name * @param args ?? * @param paramTypes ? * @return * @throws NoSuchMethodException * @throws IllegalAccessException ? * @throws InvocationTargetException ? ? */ public static Object invokeMethod(Object instance, String name, Object[] args, Class[] paramTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return org.apache.commons.beanutils.MethodUtils.invokeMethod(instance, name, args, paramTypes); } /** * ?? . * * @param instance ? * @param name * @param arg ?? * @return * @throws NoSuchMethodException * @throws IllegalAccessException ? * @throws 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 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 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 io.uengine.common.exception.ServiceException ? ? */ public static Object newInstance(String className) throws ServiceException { Class clazz = null; try { clazz = ReflectionUtils.forName(className); return clazz.newInstance(); } catch (Exception e) { String message = MessageFormatter.format("Cannot create a instance of '{}'", className).getMessage(); throw new ServiceException(message, e); } } /** * ?? ? Setter . * * @param instance ? * @param fieldName ? * @return Setter * @throws io.uengine.common.exception.ServiceException Setter */ public static Method getSetterMethod(Object instance, String fieldName) throws ServiceException { try { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(instance, fieldName); return propertyDescriptor.getWriteMethod(); } catch (Exception e) { String message = MessageFormatter .format("Cannot find setter method of '{}' in '{}'.", fieldName, instance.getClass().getName()) .getMessage(); throw new ServiceException(message, e); } } /** * ?? ? Getter . * * @param instance ? * @param fieldName ? * @return Getter * @throws io.uengine.common.exception.ServiceException Getter */ public static Method getGetterMethod(Object instance, String fieldName) throws ServiceException { try { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(instance, fieldName); return propertyDescriptor.getReadMethod(); } catch (Exception e) { String message = MessageFormatter .format("Cannot find getter method of '{}' in '{}'.", fieldName, instance.getClass().getName()) .getMessage(); throw new ServiceException(message, e); } } }