1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
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  /**
25   * <a href="ReflectionUtil.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Brian Wing Shun Chan
28   */
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      /**
53       * @deprecated
54       */
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      /**
71       * @deprecated
72       */
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  }