001 /*
002 * Created on Oct 31, 2006
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005 * the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011 * specific language governing permissions and limitations under the License.
012 *
013 * Copyright @2006-2009 the original author or authors.
014 */
015 package org.fest.reflect.method;
016
017 import static org.fest.reflect.util.Accessibles.makeAccessible;
018 import static org.fest.reflect.util.Accessibles.setAccessibleIgnoringExceptions;
019 import static org.fest.reflect.util.Throwables.targetOf;
020 import static org.fest.util.Arrays.format;
021 import static org.fest.util.Strings.concat;
022 import static org.fest.util.Strings.quote;
023
024 import java.lang.reflect.Method;
025
026 import org.fest.reflect.exception.ReflectionError;
027
028 /**
029 * Understands the use of reflection to access a method from an object.
030 * <p>
031 * <pre>
032 * // Equivalent to call 'person.setName("Luke")'
033 * {@link org.fest.reflect.core.Reflection#method(String) method}("setName").{@link MethodName#withParameterTypes(Class...) withParameterTypes}(String.class)
034 * .{@link MethodParameterTypes#in(Object) in}(person)
035 * .{@link Invoker#invoke(Object...) invoke}("Luke");
036 *
037 * // Equivalent to call 'person.concentrate()'
038 * {@link org.fest.reflect.core.Reflection#method(String) method}("concentrate").{@link MethodName#in(Object) in}(person).{@link Invoker#invoke(Object...) invoke}();
039 *
040 * // Equivalent to call 'person.getName()'
041 * String name = {@link org.fest.reflect.core.Reflection#method(String) method}("getName").{@link MethodName#withReturnType(Class) withReturnType}(String.class)
042 * .{@link MethodReturnType#in(Object) in}(person)
043 * .{@link Invoker#invoke(Object...) invoke}();
044 * </pre>
045 * </p>
046 *
047 * @param <T> the return type of the method invocation.
048 *
049 * @author Yvonne Wang
050 */
051 public final class Invoker<T> {
052
053 static <T> Invoker<T> newInvoker(String methodName, Object target, Class<?>... parameterTypes) {
054 return createInvoker(methodName, target, parameterTypes);
055 }
056
057 private static <T> Invoker<T> createInvoker(String methodName, Object target, Class<?>... parameterTypes) {
058 if (target == null) throw new NullPointerException("Target should not be null");
059 Method method = lookupInClassHierarchy(methodName, typeOf(target), parameterTypes);
060 return new Invoker<T>(target, method);
061 }
062
063 private static Class<?> typeOf(Object target) {
064 if (target instanceof Class<?>) return (Class<?>)target;
065 return target.getClass();
066 }
067
068 private static Method lookupInClassHierarchy(String methodName, Class<?> targetType, Class<?>[] parameterTypes) {
069 Method method = null;
070 Class<?> type = targetType;
071 while (type != null) {
072 method = method(methodName, type, parameterTypes);
073 if (method != null) break;
074 type = type.getSuperclass();
075 }
076 if (method == null)
077 throw new ReflectionError(concat("Unable to find method ", quote(methodName), " in ",
078 targetType.getName(), " with parameter type(s) ", format(parameterTypes)));
079 return method;
080 }
081
082 private static Method method(String methodName, Class<?> type, Class<?>[] parameterTypes) {
083 try {
084 return type.getDeclaredMethod(methodName, parameterTypes);
085 } catch (SecurityException e) {
086 return null;
087 } catch (NoSuchMethodException e) {
088 return null;
089 }
090 }
091
092 private final Object target;
093 private final Method method;
094
095 private Invoker(Object target, Method method) {
096 this.target = target;
097 this.method = method;
098 }
099
100 /**
101 * Invokes the method managed by this class using the given arguments.
102 * @param args the arguments to use to call the method managed by this class.
103 * @return the result of the method call.
104 * @throws ReflectionError if the method cannot be invoked.
105 */
106 @SuppressWarnings("unchecked") public T invoke(Object... args) {
107 boolean accessible = method.isAccessible();
108 try {
109 makeAccessible(method);
110 return (T) method.invoke(target, args);
111 } catch (Throwable t) {
112 Throwable cause = targetOf(t);
113 if (cause instanceof RuntimeException) throw (RuntimeException)cause;
114 throw cannotInvokeMethod(cause, args);
115 } finally {
116 setAccessibleIgnoringExceptions(method, accessible);
117 }
118 }
119
120 private ReflectionError cannotInvokeMethod(Throwable cause, Object... args) {
121 String message = concat("Unable to invoke method ", quote(method.getName()), " with arguments ", format(args));
122 throw new ReflectionError(message, cause);
123 }
124
125 /**
126 * Returns the "real" method managed by this class.
127 * @return the "real" method managed by this class.
128 */
129 public java.lang.reflect.Method info() {
130 return method;
131 }
132 }