1 /**
2 * Copyright (C) 2005-2009 Alfresco Software Limited.
3 *
4 * This file is part of the Spring Surf Extension project.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.springframework.extensions.surf.util;
20
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 /**
29 * Static Helper methods for instantiating objects from reflection.
30 *
31 * @author muzquiano
32 */
33 public class ReflectionHelper
34 {
35 private static Log logger = LogFactory.getLog(ReflectionHelper.class);
36
37 private ReflectionHelper()
38 {
39 }
40
41 /**
42 * Constructs a new object for the given class name.
43 * The construction takes no arguments.
44 *
45 * If an exception occurs during construction, null is returned.
46 *
47 * All exceptions are written to the Log instance for this class.
48 *
49 * @param className
50 * @return
51 */
52 public static Object newObject(String className)
53 {
54 Object o = null;
55
56 try
57 {
58 Class clazz = Class.forName(className);
59 o = clazz.newInstance();
60 }
61 catch (ClassNotFoundException cnfe)
62 {
63 logger.debug(cnfe);
64 }
65 catch (InstantiationException ie)
66 {
67 logger.debug(ie);
68 }
69 catch (IllegalAccessException iae)
70 {
71 logger.debug(iae);
72 }
73 return o;
74 }
75
76 /**
77 * Constructs a new object for the given class name and with the given
78 * arguments. The arguments must be specified in terms of their Class[]
79 * types and their Object[] values.
80 *
81 * Example:
82 *
83 * String s = newObject("java.lang.String", new Class[] { String.class},
84 * new String[] { "test"});
85 *
86 * is equivalent to:
87 *
88 * String s = new String("test");
89 *
90 * If an exception occurs during construction, null is returned.
91 *
92 * All exceptions are written to the Log instance for this class.
93
94 * @param className
95 * @param argTypes
96 * @param args
97 * @return
98 */
99 public static Object newObject(String className, Class[] argTypes, Object[] args)
100 {
101 /**
102 * We have some mercy here - if they called and did not pass in any
103 * arguments, then we will call through to the pure newObject() method.
104 */
105 if (args == null || args.length == 0)
106 {
107 return newObject(className);
108 }
109
110 /**
111 * Try to build the object
112 *
113 * If an exception occurs, we log it and return null.
114 */
115 Object o = null;
116 try
117 {
118 // base class
119 Class clazz = Class.forName(className);
120
121 Constructor c = clazz.getDeclaredConstructor(argTypes);
122 o = c.newInstance(args);
123 }
124 catch (ClassNotFoundException cnfe)
125 {
126 logger.debug(cnfe);
127 }
128 catch (InstantiationException ie)
129 {
130 logger.debug(ie);
131 }
132 catch (IllegalAccessException iae)
133 {
134 logger.debug(iae);
135 }
136 catch (NoSuchMethodException nsme)
137 {
138 logger.debug(nsme);
139 }
140 catch (InvocationTargetException ite)
141 {
142 logger.debug(ite);
143 }
144 return o;
145 }
146
147 /**
148 * Invokes a method on the given object by passing the given arguments
149 * into the method.
150 *
151 * @param obj
152 * @param method
153 * @param argTypes
154 * @param args
155 * @return
156 */
157 public static Object invoke(Object obj, String method, Class[] argTypes, Object[] args)
158 {
159 if (obj == null || method == null)
160 {
161 throw new IllegalArgumentException("Object and Method must be supplied.");
162 }
163
164 /**
165 * Try to invoke the method.
166 *
167 * If the method is unable to be invoked, we log and return null.
168 */
169 try
170 {
171 Method m = obj.getClass().getMethod(method, argTypes);
172 if(m != null)
173 {
174 return m.invoke(obj, args);
175 }
176 }
177 catch(NoSuchMethodException nsme)
178 {
179 logger.debug(nsme);
180 }
181 catch(IllegalAccessException iae)
182 {
183 logger.debug(iae);
184 }
185 catch(InvocationTargetException ite)
186 {
187 logger.debug(ite);
188 }
189
190 return null;
191 }
192 }