001    /**
002     * Copyright (c) 2000-2012 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.annotation.Annotation;
018    import java.lang.reflect.Field;
019    import java.lang.reflect.Method;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     * @author Miguel Pastor
024     */
025    public class ReflectionUtil {
026    
027            public static Class<?> getAnnotationDeclaringClass(
028                    Class<? extends Annotation> annotationClass, Class<?> clazz) {
029    
030                    if ((clazz == null) || clazz.equals(Object.class)) {
031                            return null;
032                    }
033    
034                    if (isAnnotationDeclaredInClass(annotationClass, clazz)) {
035                            return clazz;
036                    }
037                    else {
038                            return getAnnotationDeclaringClass(
039                                    annotationClass, clazz.getSuperclass());
040                    }
041            }
042    
043            public static Field getDeclaredField(Class<?> clazz, String name)
044                    throws Exception {
045    
046                    Field field = clazz.getDeclaredField(name);
047    
048                    if (!field.isAccessible()) {
049                            field.setAccessible(true);
050                    }
051    
052                    return field;
053            }
054    
055            public static Method getDeclaredMethod(
056                            Class<?> clazz, String name, Class<?> ... parameterTypes)
057                    throws Exception {
058    
059                    Method method = clazz.getDeclaredMethod(name, parameterTypes);
060    
061                    if (!method.isAccessible()) {
062                            method.setAccessible(true);
063                    }
064    
065                    return method;
066            }
067    
068            public static Class<?>[] getParameterTypes(Object[] arguments) {
069                    if (arguments == null) {
070                            return null;
071                    }
072    
073                    Class<?>[] parameterTypes = new Class<?>[arguments.length];
074    
075                    for (int i = 0; i < arguments.length; i++) {
076                            if (arguments[i] == null) {
077                                    parameterTypes[i] = null;
078                            }
079                            else if (arguments[i] instanceof Boolean) {
080                                    parameterTypes[i] = Boolean.TYPE;
081                            }
082                            else if (arguments[i] instanceof Byte) {
083                                    parameterTypes[i] = Byte.TYPE;
084                            }
085                            else if (arguments[i] instanceof Character) {
086                                    parameterTypes[i] = Character.TYPE;
087                            }
088                            else if (arguments[i] instanceof Double) {
089                                    parameterTypes[i] = Double.TYPE;
090                            }
091                            else if (arguments[i] instanceof Float) {
092                                    parameterTypes[i] = Float.TYPE;
093                            }
094                            else if (arguments[i] instanceof Integer) {
095                                    parameterTypes[i] = Integer.TYPE;
096                            }
097                            else if (arguments[i] instanceof Long) {
098                                    parameterTypes[i] = Long.TYPE;
099                            }
100                            else if (arguments[i] instanceof Short) {
101                                    parameterTypes[i] = Short.TYPE;
102                            }
103                            else {
104                                    parameterTypes[i] = arguments[i].getClass();
105                            }
106                    }
107    
108                    return parameterTypes;
109            }
110    
111            public static boolean isAnnotationDeclaredInClass(
112                    Class<? extends Annotation> annotationClass, Class<?> clazz) {
113    
114                    if ((annotationClass == null) || (clazz == null)) {
115                            throw new IllegalArgumentException();
116                    }
117    
118                    Annotation[] annotations = clazz.getAnnotations();
119    
120                    for (Annotation annotation : annotations) {
121                            if (annotationClass.equals(annotation.annotationType())) {
122                                    return true;
123                            }
124                    }
125    
126                    return false;
127            }
128    
129    }