001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.lang.reflect.Field;
018    import java.lang.reflect.Method;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     */
023    public class ReflectionUtil {
024    
025            public static Field getDeclaredField(Class<?> clazz, String name)
026                    throws Exception {
027    
028                    Field field = clazz.getDeclaredField(name);
029    
030                    if (!field.isAccessible()) {
031                            field.setAccessible(true);
032                    }
033    
034                    return field;
035            }
036    
037            public static Method getDeclaredMethod(
038                            Class<?> clazz, String name, Class<?> ... parameterTypes)
039                    throws Exception {
040    
041                    Method method = clazz.getDeclaredMethod(name, parameterTypes);
042    
043                    if (!method.isAccessible()) {
044                            method.setAccessible(true);
045                    }
046    
047                    return method;
048            }
049    
050            public static Class<?>[] getParameterTypes(Object[] arguments) {
051                    if (arguments == null) {
052                            return null;
053                    }
054    
055                    Class<?>[] parameterTypes = new Class<?>[arguments.length];
056    
057                    for (int i = 0; i < arguments.length; i++) {
058                            if (arguments[i] == null) {
059                                    parameterTypes[i] = null;
060                            }
061                            else if (arguments[i] instanceof Boolean) {
062                                    parameterTypes[i] = Boolean.TYPE;
063                            }
064                            else if (arguments[i] instanceof Byte) {
065                                    parameterTypes[i] = Byte.TYPE;
066                            }
067                            else if (arguments[i] instanceof Character) {
068                                    parameterTypes[i] = Character.TYPE;
069                            }
070                            else if (arguments[i] instanceof Double) {
071                                    parameterTypes[i] = Double.TYPE;
072                            }
073                            else if (arguments[i] instanceof Float) {
074                                    parameterTypes[i] = Float.TYPE;
075                            }
076                            else if (arguments[i] instanceof Integer) {
077                                    parameterTypes[i] = Integer.TYPE;
078                            }
079                            else if (arguments[i] instanceof Long) {
080                                    parameterTypes[i] = Long.TYPE;
081                            }
082                            else if (arguments[i] instanceof Short) {
083                                    parameterTypes[i] = Short.TYPE;
084                            }
085                            else {
086                                    parameterTypes[i] = arguments[i].getClass();
087                            }
088                    }
089    
090                    return parameterTypes;
091            }
092    
093    }