001: /* ReflectionUtilsBean.java
002: *
003: * DDSteps - Data Driven JUnit Test Steps
004: * Copyright (C) 2005 Jayway AB
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License version 2.1 as published by the Free Software Foundation.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, visit
017: * http://www.opensource.org/licenses/lgpl-license.php
018: */
019: package org.ddsteps.mock;
020:
021: import java.lang.reflect.Method;
022: import java.lang.reflect.Modifier;
023: import java.util.Arrays;
024: import java.util.HashSet;
025: import java.util.Set;
026:
027: import org.apache.commons.lang.ArrayUtils;
028: import org.apache.commons.lang.ObjectUtils;
029: import org.apache.commons.lang.StringUtils;
030: import org.apache.commons.lang.Validate;
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: /**
035: * Static utilities for working with classes and methods.
036: *
037: * @author adam
038: * @version $Id: ReflectionUtilsBean.java,v 1.1 2005/12/03 12:51:40 adamskogman Exp $
039: */
040: public class ReflectionUtilsBean implements ReflectionUtils {
041:
042: private static final Log LOG = LogFactory
043: .getLog(MockUtilsBean.class);
044:
045: /**
046: * No-args.
047: */
048: public ReflectionUtilsBean() {
049: super ();
050: }
051:
052: /** TODO Comment
053: * @see org.ddsteps.mock.ReflectionUtils#findAllImplementedMethods(java.lang.Class)
054: */
055: public Set findAllImplementedMethods(final Class clazz) {
056:
057: Set methodSet = new HashSet();
058: Class currentClass = clazz;
059:
060: // Object has superclass null
061: while (currentClass != null) {
062:
063: // Get methods
064: Method[] methods = currentClass.getDeclaredMethods();
065:
066: // Add any abstract methods
067: for (int i = 0; i < methods.length; i++) {
068: Method method = methods[i];
069:
070: // not abstract? Add it!
071: if ((method.getModifiers() & Modifier.ABSTRACT) == 0) {
072: methodSet.add(method);
073: }
074:
075: }
076:
077: // move on to superclass
078: currentClass = currentClass.getSuperclass();
079: }
080:
081: return methodSet;
082: }
083:
084: /** TODO Comment
085: * @see org.ddsteps.mock.ReflectionUtils#findAllMethodsInSuperclasses(java.lang.Class)
086: */
087: public Set findAllMethodsInSuperclasses(final Class clazz) {
088: return findAllMethods(clazz.getSuperclass());
089: }
090:
091: /** TODO Comment
092: * @see org.ddsteps.mock.ReflectionUtils#findInterfaceMethods(java.lang.Class)
093: */
094: public Set findInterfaceMethods(final Class clazz) {
095:
096: Set methodSet = new HashSet();
097: Class currentClass = clazz;
098:
099: // Object has superclass null
100: while (currentClass != null) {
101:
102: // Get methods
103: Class[] interfaces = currentClass.getInterfaces();
104:
105: for (int i = 0; i < interfaces.length; i++) {
106: Class face = interfaces[i];
107:
108: // Add all methods in the interface
109: methodSet.addAll(Arrays.asList(face.getMethods()));
110: }
111:
112: // move on to superclass
113: currentClass = currentClass.getSuperclass();
114: }
115:
116: return methodSet;
117: }
118:
119: // /**
120: // * Find ALL interfaces supported by this class.
121: // *
122: // * @param clazz A class, not an interface.
123: // * @return (link Set) of (@link Class)
124: // */
125: // public Set findAllInterfaces(Class clazz) {
126: //
127: // Set interfaceSet = new HashSet();
128: //
129: // Class[] implementedInterfaces = clazz.getInterfaces();
130: //
131: // if (implementedInterfaces == null || implementedInterfaces.length == 0) {
132: // // fail fast
133: // return interfaceSet;
134: // }
135: //
136: // // Add root interfaces to work list
137: // LinkedList workList = new LinkedList(Arrays.asList(implementedInterfaces));
138: //
139: // // Work until list is empty
140: // while (!workList.isEmpty()) {
141: //
142: // Class face = (Class) workList.removeFirst();
143: //
144: // // Add to set of handled interfaces
145: // if (interfaceSet.add(face)) {
146: // // New interface, process
147: // }
148: //
149: // }
150: //
151: // return interfaceSet;
152: //
153: // }
154:
155: /** TODO Comment
156: * @see org.ddsteps.mock.ReflectionUtils#findAllAbstractMethods(java.lang.Class)
157: */
158: public Set findAllAbstractMethods(final Class clazz) {
159:
160: Set methodSet = new HashSet();
161: Class currentClass = clazz;
162:
163: // Object has superclass null
164: while (currentClass != null) {
165:
166: // Get methods
167: Method[] methods = currentClass.getDeclaredMethods();
168:
169: // Add any abstract methods
170: for (int i = 0; i < methods.length; i++) {
171: Method method = methods[i];
172:
173: // abstract
174: if ((method.getModifiers() & Modifier.ABSTRACT) > 0) {
175: methodSet.add(method);
176: }
177:
178: }
179:
180: // move on to superclass
181: currentClass = currentClass.getSuperclass();
182: }
183:
184: return methodSet;
185: }
186:
187: /** TODO Comment
188: * @see org.ddsteps.mock.ReflectionUtils#findLocalImplementedMethods(java.lang.Class)
189: */
190: public Set findLocalImplementedMethods(final Class clazz) {
191:
192: Set methodSet = new HashSet();
193:
194: // Get methods
195: Method[] methods = clazz.getDeclaredMethods();
196:
197: // Add any NON abstract methods
198: for (int i = 0; i < methods.length; i++) {
199: Method method = methods[i];
200:
201: // not abstract
202: if ((method.getModifiers() & Modifier.ABSTRACT) == 0) {
203: methodSet.add(method);
204: }
205:
206: }
207:
208: return methodSet;
209: }
210:
211: /** TODO Comment
212: * @see org.ddsteps.mock.ReflectionUtils#findMethodByName(java.lang.Class, java.lang.String)
213: */
214: public Method findMethodByName(Class clazz, String methodName)
215: throws NoSuchMethodException {
216:
217: Method[] methods = clazz.getDeclaredMethods();
218:
219: // Add all methods
220: for (int i = 0; i < methods.length; i++) {
221: Method method = methods[i];
222:
223: if (StringUtils.equals(method.getName(), methodName)) {
224:
225: if (LOG.isInfoEnabled()) {
226: LOG.info("Not mocking method. (Method = " + method
227: + ")");
228: }
229:
230: return method;
231: }
232:
233: }
234:
235: throw new NoSuchMethodException("Method '" + methodName
236: + "' not found in class '" + clazz.getName() + "'");
237:
238: }
239:
240: /** TODO Comment
241: * @see org.ddsteps.mock.ReflectionUtils#findAllMethods(java.lang.Class)
242: */
243: public Set findAllMethods(final Class clazz) {
244:
245: Set methodSet = new HashSet();
246: Class currentClass = clazz;
247:
248: // Object has superclass null
249: while (currentClass != null) {
250:
251: // Get methods (never null)
252: Method[] methods = currentClass.getDeclaredMethods();
253:
254: // Add all methods
255: methodSet.addAll(Arrays.asList(methods));
256:
257: // move on to superclass
258: currentClass = currentClass.getSuperclass();
259: }
260:
261: return methodSet;
262: }
263:
264: /** TODO Comment
265: * @see org.ddsteps.mock.ReflectionUtils#isFinal(java.lang.reflect.Method)
266: */
267: public boolean isFinal(Method method) {
268: Validate.notNull(method, "Argument method must not be null");
269:
270: return (method.getModifiers() & Modifier.FINAL) > 0;
271: }
272:
273: /** TODO Comment
274: * @see org.ddsteps.mock.ReflectionUtils#isNative(java.lang.reflect.Method)
275: */
276: public boolean isNative(Method method) {
277: Validate.notNull(method, "Argument method must not be null");
278:
279: return (method.getModifiers() & Modifier.NATIVE) > 0;
280: }
281:
282: /** TODO Comment
283: * @see org.ddsteps.mock.ReflectionUtils#isOverride(java.lang.reflect.Method, java.lang.reflect.Method)
284: */
285: public boolean isOverride(Method possibleOverride, Method reference) {
286:
287: return StringUtils.equals(possibleOverride.getName(), reference
288: .getName())
289: && ArrayUtils.isEquals(possibleOverride
290: .getParameterTypes(), reference
291: .getParameterTypes())
292: && ObjectUtils.equals(possibleOverride.getReturnType(),
293: reference.getReturnType());
294:
295: }
296:
297: }
|