001
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 import java.lang.reflect.Modifier;
021
022 import java.util.List;
023
024
028 public class ReflectionUtil {
029
030 public static Class<?> getAnnotationDeclaringClass(
031 Class<? extends Annotation> annotationClass, Class<?> clazz) {
032
033 if ((clazz == null) || clazz.equals(Object.class)) {
034 return null;
035 }
036
037 if (isAnnotationDeclaredInClass(annotationClass, clazz)) {
038 return clazz;
039 }
040 else {
041 return getAnnotationDeclaringClass(
042 annotationClass, clazz.getSuperclass());
043 }
044 }
045
046 public static Field getDeclaredField(Class<?> clazz, String name)
047 throws Exception {
048
049 Field field = clazz.getDeclaredField(name);
050
051 if (!field.isAccessible()) {
052 field.setAccessible(true);
053 }
054
055 int modifiers = field.getModifiers();
056
057 if ((modifiers & Modifier.FINAL) == Modifier.FINAL) {
058 Field modifiersField = ReflectionUtil.getDeclaredField(
059 Field.class, "modifiers");
060
061 modifiersField.setInt(field, modifiers & ~Modifier.FINAL);
062 }
063
064 return field;
065 }
066
067 public static Method getDeclaredMethod(
068 Class<?> clazz, String name, Class<?> ... parameterTypes)
069 throws Exception {
070
071 Method method = clazz.getDeclaredMethod(name, parameterTypes);
072
073 if (!method.isAccessible()) {
074 method.setAccessible(true);
075 }
076
077 return method;
078 }
079
080 public static Class<?>[] getInterfaces(Object object) {
081 return getInterfaces(object, null);
082 }
083
084 public static Class<?>[] getInterfaces(
085 Object object, ClassLoader classLoader) {
086
087 List<Class<?>> interfaceClasses = new UniqueList<Class<?>>();
088
089 Class<?> clazz = object.getClass();
090
091 _getInterfaces(interfaceClasses, clazz, classLoader);
092
093 Class<?> superClass = clazz.getSuperclass();
094
095 while (superClass != null) {
096 _getInterfaces(interfaceClasses, superClass, classLoader);
097
098 superClass = superClass.getSuperclass();
099 }
100
101 return interfaceClasses.toArray(new Class<?>[interfaceClasses.size()]);
102 }
103
104 public static Class<?>[] getParameterTypes(Object[] arguments) {
105 if (arguments == null) {
106 return null;
107 }
108
109 Class<?>[] parameterTypes = new Class<?>[arguments.length];
110
111 for (int i = 0; i < arguments.length; i++) {
112 if (arguments[i] == null) {
113 parameterTypes[i] = null;
114 }
115 else if (arguments[i] instanceof Boolean) {
116 parameterTypes[i] = Boolean.TYPE;
117 }
118 else if (arguments[i] instanceof Byte) {
119 parameterTypes[i] = Byte.TYPE;
120 }
121 else if (arguments[i] instanceof Character) {
122 parameterTypes[i] = Character.TYPE;
123 }
124 else if (arguments[i] instanceof Double) {
125 parameterTypes[i] = Double.TYPE;
126 }
127 else if (arguments[i] instanceof Float) {
128 parameterTypes[i] = Float.TYPE;
129 }
130 else if (arguments[i] instanceof Integer) {
131 parameterTypes[i] = Integer.TYPE;
132 }
133 else if (arguments[i] instanceof Long) {
134 parameterTypes[i] = Long.TYPE;
135 }
136 else if (arguments[i] instanceof Short) {
137 parameterTypes[i] = Short.TYPE;
138 }
139 else {
140 parameterTypes[i] = arguments[i].getClass();
141 }
142 }
143
144 return parameterTypes;
145 }
146
147 public static boolean isAnnotationDeclaredInClass(
148 Class<? extends Annotation> annotationClass, Class<?> clazz) {
149
150 if ((annotationClass == null) || (clazz == null)) {
151 throw new IllegalArgumentException();
152 }
153
154 Annotation[] annotations = clazz.getAnnotations();
155
156 for (Annotation annotation : annotations) {
157 if (annotationClass.equals(annotation.annotationType())) {
158 return true;
159 }
160 }
161
162 return false;
163 }
164
165 private static void _getInterfaces(
166 List<Class<?>> interfaceClasses, Class<?> clazz,
167 ClassLoader classLoader) {
168
169 for (Class<?> interfaceClass : clazz.getInterfaces()) {
170 try {
171 if (classLoader != null) {
172 interfaceClasses.add(
173 classLoader.loadClass(interfaceClass.getName()));
174 }
175 else {
176 interfaceClasses.add(interfaceClass);
177 }
178 }
179 catch (ClassNotFoundException cnfe) {
180 }
181 }
182 }
183
184 }