1
14
15 package com.liferay.portal.kernel.util;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.Field;
22 import java.lang.reflect.Method;
23
24
29 public class ReflectionUtil {
30
31 public static Field getDeclaredField(Class<?> classObj, String name)
32 throws Exception {
33
34 Field field = classObj.getDeclaredField(name);
35
36 field.setAccessible(true);
37
38 return field;
39 }
40
41 public static Method getDeclaredMethod(
42 Class<?> classObj, String name, Class<?> ... parameterTypes)
43 throws Exception {
44
45 Method method = classObj.getDeclaredMethod(name, parameterTypes);
46
47 method.setAccessible(true);
48
49 return method;
50 }
51
52
55 public static Object getFieldValue(Class<?> classObj, String fieldName) {
56 try {
57 Field field = classObj.getDeclaredField(fieldName);
58
59 field.setAccessible(true);
60
61 return field.get(null);
62 }
63 catch (Exception e) {
64 _log.error(e, e);
65
66 return null;
67 }
68 }
69
70
73 public static Object newInstance(String className, String p1) {
74 try {
75 Class<?> classObject = Class.forName(className);
76
77 Constructor<?> classConstructor = classObject.getConstructor(
78 new Class[] {String.class});
79
80 Object[] arguments = new Object[] {p1};
81
82 return classConstructor.newInstance(arguments);
83 }
84 catch (Exception e) {
85 _log.error(e, e);
86
87 return null;
88 }
89 }
90
91 private static Log _log = LogFactoryUtil.getLog(ReflectionUtil.class);
92
93 }