View Javadoc

1   /*
2    * Copyright 2004-2005, 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.util;
17  
18  import net.sf.morph.reflect.CompositeReflector;
19  import net.sf.morph.reflect.Reflector;
20  
21  /**
22   * Utility functions useful for implementing and interacting with Reflectors.
23   * 
24   * @author Matt Sgarlata
25   * @since Nov 26, 2004
26   */
27  public abstract class ReflectorUtils {
28  
29  	/**
30  	 * Indicates whether the given reflector can support the operations
31  	 * specified in <code>reflectorType</code> when reflecting instances of
32  	 * <code>reflectedType</code>.
33  	 * 
34  	 * @param reflector
35  	 *            the reflector
36  	 * @param reflectedType
37  	 *            the type of the object for which we wish to determine
38  	 *            reflectability
39  	 * @param reflectorType
40  	 *            the interface which defines the operations we would like to
41  	 *            perform on instances of <code>reflectedType</code>
42  	 * @return <code>true</code>, if the given reflector can support the
43  	 *         operations specified in <code>reflectorType</code> when
44  	 *         reflecting instances of <code>reflectedType</code>
45  	 * @throws IllegalArgumentException
46  	 *             if any of the arguments to this function are
47  	 *             <code>null</code>
48  	 * @throws net.sf.morph.reflect.ReflectionException
49  	 *             if reflectability could not be determined
50  	 */
51  	public static boolean isReflectable(Reflector reflector, Class reflectedType,
52  		Class reflectorType) throws IllegalArgumentException {
53  		if (reflector == null) {
54  			throw new IllegalArgumentException("The reflector must be specified");
55  		}
56  		if (reflectedType == null) {
57  			throw new IllegalArgumentException("The reflectedType must be specified");
58  		}
59  		if (reflectorType == null) {
60  			throw new IllegalArgumentException("The reflectorType must be specified");
61  		}
62  		if (reflector instanceof CompositeReflector) {
63  			return ((CompositeReflector) reflector).isReflectable(reflectedType, reflectorType);
64  		}
65  		return reflectorType.isInstance(reflector)
66  				&& ClassUtils.inheritanceContains(reflector.getReflectableClasses(),
67  						reflectedType);
68  	}
69  
70  	/**
71  	 * Indicates whether the given reflector can support the operations
72  	 * specified in <code>reflectorType</code> when reflecting
73  	 * <code>reflectedObject</code>.
74  	 * 
75  	 * @param reflector
76  	 *            the reflector
77  	 * @param reflectedObject
78  	 *            the object for which we wish to determine reflectability
79  	 * @param reflectorType
80  	 *            the interface which defines the operations we would like to
81  	 *            perform on instances of <code>reflectedType</code>
82  	 * @return <code>true</code>, if the given reflector can support the
83  	 *         operations specified in <code>reflectorType</code> when
84  	 *         reflecting instances of <code>reflectedType</code>
85  	 * @throws IllegalArgumentException
86  	 *             if any of the arguments to this function are
87  	 *             <code>null</code>
88  	 * @throws net.sf.morph.reflect.ReflectionException
89  	 *             if reflectability could not be determined
90  	 * @since Morph 1.1
91  	 */
92  	public static boolean isReflectable(Reflector reflector, Object reflectedObject, Class reflectorType) {
93  	    return isReflectable(reflector, ClassUtils.getClass(reflectedObject), reflectorType);
94      }
95  
96  }