001 /* 002 * Created on Jan 25, 2009 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 @2009 the original author or authors. 014 */ 015 package org.fest.reflect.method; 016 017 import static org.fest.reflect.method.Invoker.newInvoker; 018 import static org.fest.reflect.method.MethodParameterTypes.newParameterTypes; 019 020 import org.fest.reflect.reference.TypeRef; 021 022 /** 023 * Understands the return type reference of the method to invoke. 024 * <p> 025 * The following is an example of proper usage of this class: 026 * <pre> 027 * // Equivalent to call 'jedi.getPowers()' 028 * List<String> powers = {@link org.fest.reflect.core.Reflection#method(String) method}("getPowers").{@link MethodName#withReturnType(TypeRef) withReturnType}(new {@link TypeRef TypeRef}<List<String>>() {}) 029 * .{@link MethodReturnTypeRef#in(Object) in}(person) 030 * .{@link Invoker#invoke(Object...) invoke}(); 031 * </pre> 032 * </p> 033 * 034 * @param <T> the generic type of the method's return type reference. 035 * 036 * @author Alex Ruiz 037 * 038 * @since 1.1 039 */ 040 public class MethodReturnTypeRef<T> { 041 042 static <T> MethodReturnTypeRef<T> newReturnTypeRef(String name, TypeRef<T> type) { 043 if (type == null) 044 throw new NullPointerException("The return type reference of the method to access should not be null"); 045 return new MethodReturnTypeRef<T>(name); 046 } 047 048 private final String name; 049 050 private MethodReturnTypeRef(String name) { 051 this.name = name; 052 } 053 054 /** 055 * Creates a new method invoker. 056 * @param target the object containing the method to invoke. 057 * @return the created method invoker. 058 * @throws NullPointerException if the given target is <code>null</code>. 059 */ 060 public Invoker<T> in(Object target) { 061 return newInvoker(name, target); 062 } 063 064 /** 065 * Specifies the parameter types of the method to invoke. This method call is optional if the method to invoke does 066 * not take arguments. 067 * @param parameterTypes the parameter types of the method to invoke. 068 * @return the created parameter types holder. 069 * @throws NullPointerException if the array of parameter types is <code>null</code>. 070 */ 071 public MethodParameterTypes<T> withParameterTypes(Class<?>... parameterTypes) { 072 return newParameterTypes(name, parameterTypes); 073 } 074 }