001 /* 002 * Created on Aug 17, 2007 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with 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 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 * 014 * Copyright @2007-2009 the original author or authors. 015 */ 016 package org.fest.reflect.method; 017 018 import static org.fest.reflect.method.Invoker.newInvoker; 019 import static org.fest.reflect.method.MethodParameterTypes.newParameterTypes; 020 021 /** 022 * Understands the return type of the method to invoke. 023 * <p> 024 * The following is an example of proper usage of this class: 025 * <pre> 026 * // Equivalent to call 'person.setName("Luke")' 027 * {@link org.fest.reflect.core.Reflection#method(String) method}("setName").{@link MethodName#withParameterTypes(Class...) withParameterTypes}(String.class) 028 * .{@link MethodParameterTypes#in(Object) in}(person) 029 * .{@link Invoker#invoke(Object...) invoke}("Luke"); 030 * 031 * // Equivalent to call 'person.concentrate()' 032 * {@link org.fest.reflect.core.Reflection#method(String) method}("concentrate").{@link MethodName#in(Object) in}(person).{@link Invoker#invoke(Object...) invoke}(); 033 * 034 * // Equivalent to call 'person.getName()' 035 * String name = {@link org.fest.reflect.core.Reflection#method(String) method}("getName").{@link MethodName#withReturnType(Class) withReturnType}(String.class) 036 * .{@link MethodReturnType#in(Object) in}(person) 037 * .{@link Invoker#invoke(Object...) invoke}(); 038 * </pre> 039 * </p> 040 * 041 * @param <T> the generic type of the method's return type. 042 * 043 * @author Yvonne Wang 044 * @author Alex Ruiz 045 */ 046 public class MethodReturnType<T> { 047 048 static <T> MethodReturnType<T> newReturnType(String name, Class<T> type) { 049 if (type == null) throw new NullPointerException("The return type of the method to access should not be null"); 050 return new MethodReturnType<T>(name); 051 } 052 053 private final String name; 054 055 private MethodReturnType(String name) { 056 this.name = name; 057 } 058 059 /** 060 * Creates a new method invoker. 061 * @param target the object containing the method to invoke. 062 * @return the created method invoker. 063 * @throws NullPointerException if the given target is <code>null</code>. 064 */ 065 public Invoker<T> in(Object target) { 066 return newInvoker(name, target); 067 } 068 069 /** 070 * Specifies the parameter types of the method to invoke. This method call is optional if the method to invoke does 071 * not take arguments. 072 * @param parameterTypes the parameter types of the method to invoke. 073 * @return the created parameter types holder. 074 * @throws NullPointerException if the array of parameter types is <code>null</code>. 075 */ 076 public MethodParameterTypes<T> withParameterTypes(Class<?>... parameterTypes) { 077 return newParameterTypes(name, parameterTypes); 078 } 079 }