| 1 | /* |
| 2 | * $Id: Reflection.java 562 2010-11-11 00:28:13Z PSpeed $ |
| 3 | * |
| 4 | * The Filament BSD license. |
| 5 | * |
| 6 | * Copyright (c) 2009-2010, the original author or authors |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1) Redistributions of source code must retain the above copyright notice, |
| 13 | * this list of conditions and the following disclaimer. |
| 14 | * 2) Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3) Neither the names "Filament", "fgraph.org", "filamentgraph.org", nor the |
| 18 | * names of its contributors may be used to endorse or promote products |
| 19 | * derived from this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 25 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | * POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | package org.fgraph.steps; |
| 35 | |
| 36 | import java.lang.reflect.Method; |
| 37 | import java.util.Arrays; |
| 38 | |
| 39 | import com.google.common.base.Function; |
| 40 | import com.google.common.base.Predicate; |
| 41 | import com.google.common.base.Predicates; |
| 42 | |
| 43 | import org.progeeks.util.Inspector; |
| 44 | |
| 45 | /** |
| 46 | * Static utility methods for creating reflection-based |
| 47 | * functions. |
| 48 | * |
| 49 | * @version $Revision: 562 $ |
| 50 | * @author Paul Speed |
| 51 | */ |
| 52 | public class Reflection |
| 53 | { |
| 54 | /** |
| 55 | * Creates a function that returns the value of the specified |
| 56 | * property on an applied object. A the property does not |
| 57 | * exist on the applied object then a null is returned. |
| 58 | * |
| 59 | * <p>Property resolution in this case is as loose as possible, |
| 60 | * handling true getters, no-arg methods, and generic Map-like |
| 61 | * get(property) methods.</p> |
| 62 | */ |
| 63 | public static <T> Function<Object,T> property( String property ) |
| 64 | { |
| 65 | // This and propertyEqualTo() were changed from Function<F,T> |
| 66 | // to Function<Object,T> to make using them easier. I took a |
| 67 | // cue from the Google Functions.toStringFunction() which doesn't |
| 68 | // bother to parameterized the input type. |
| 69 | // propertyEqualTo() had to change because this method did. |
| 70 | return new PropertyFunction<Object,T>(property); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Creates a predicate that internally uses a property() function |
| 75 | * to retrieve a value from an applied object and compare it to |
| 76 | * the value provided. |
| 77 | * |
| 78 | * <p>This is a convenience method that composes a property() |
| 79 | * function with a Predicates.equalTo() function using the |
| 80 | * Predicates.compose() method.</p> |
| 81 | */ |
| 82 | public static <T> Predicate<Object> propertyEqualTo( String property, T value ) |
| 83 | { |
| 84 | // I've left the <T> in here even though it doesn't |
| 85 | // have much purpose. It does remind the caller that |
| 86 | // the type of the value passed is not the same as the |
| 87 | // type that will be passed to the resulting predicate. |
| 88 | // I may decide later that that isn't important. |
| 89 | Function<Object,T> prop = property( property ); |
| 90 | Predicate<T> eq = Predicates.equalTo( value ); |
| 91 | return Predicates.compose( eq, prop ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns a function that will return an Iterable for |
| 96 | * all Methods in an applied Class. |
| 97 | */ |
| 98 | public static StepFunction<Class,Method> methods() |
| 99 | { |
| 100 | return MethodsFunction.INSTANCE; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Creates a predicate that returns true if the applied Method |
| 105 | * object has the exact same types as specified. Specifying |
| 106 | * no types, ie: Reflect.hasParameterTypes(), matchs no-arg |
| 107 | * methods. |
| 108 | */ |
| 109 | public static Predicate<Method> hasParameterTypes( Class... types ) |
| 110 | { |
| 111 | return new HasParameterTypes( types ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Function that returns a value for a particular property |
| 116 | * on whatever object is passed to apply. This inherits the |
| 117 | * property resolution of the Inspector class. |
| 118 | */ |
| 119 | private static class PropertyFunction<F,T> implements Function<F,T> |
| 120 | { |
| 121 | private String property; |
| 122 | |
| 123 | public PropertyFunction( String property ) |
| 124 | { |
| 125 | this.property = property; |
| 126 | } |
| 127 | |
| 128 | @SuppressWarnings("unchecked") |
| 129 | public T apply( F obj ) |
| 130 | { |
| 131 | // Have to go one way or the other, either ignore it |
| 132 | // or die. It's possible for the function to be wrapped |
| 133 | // in such a way to ignore nulls but it is not possible to |
| 134 | // wrap it to die without a custom impl... so I'm opting |
| 135 | // for dying. |
| 136 | if( obj == null ) |
| 137 | throw new IllegalArgumentException( "Object cannot be null." ); |
| 138 | |
| 139 | Inspector ins = new Inspector(obj); |
| 140 | return (T)ins.get(property); |
| 141 | } |
| 142 | |
| 143 | public String toString() |
| 144 | { |
| 145 | return "PropertyFunction[" + property + "]"; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Predicate that returns true for objects with the same |
| 151 | * exact parameter types as those specified. |
| 152 | */ |
| 153 | private static class HasParameterTypes implements Predicate<Method> |
| 154 | { |
| 155 | private Class[] types; |
| 156 | |
| 157 | public HasParameterTypes( Class... types ) |
| 158 | { |
| 159 | if( types == null ) |
| 160 | throw new IllegalArgumentException( "Types array cannot be null." ); |
| 161 | this.types = types; |
| 162 | } |
| 163 | |
| 164 | public boolean apply( Method m ) |
| 165 | { |
| 166 | if( m == null ) |
| 167 | return false; |
| 168 | |
| 169 | Class[] parms = m.getParameterTypes(); |
| 170 | if( parms.length != types.length ) |
| 171 | return false; |
| 172 | |
| 173 | for( int i = 0; i < parms.length; i++ ) |
| 174 | { |
| 175 | if( parms[i] != types[i] ) |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | public String toString() |
| 183 | { |
| 184 | return "HasParameterTypes[" + Arrays.asList(types) + "]"; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Returns all of the public methods for a class. |
| 190 | */ |
| 191 | private static class MethodsFunction implements StepFunction<Class,Method> |
| 192 | { |
| 193 | public static final MethodsFunction INSTANCE = new MethodsFunction(); |
| 194 | |
| 195 | public Iterable<Method> apply( Class start ) |
| 196 | { |
| 197 | if( start == null ) |
| 198 | throw new IllegalArgumentException( "Class cannot be null." ); |
| 199 | return Arrays.asList( start.getMethods() ); |
| 200 | } |
| 201 | |
| 202 | public String toString() |
| 203 | { |
| 204 | return "MethodsFunction[]"; |
| 205 | } |
| 206 | } |
| 207 | } |