|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| MethodReturnType | Line # 46 | 6 | 0% | 5 | 0 | 100% |
1.0
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| No Tests | |||
| 1 | /* | |
| 2 | * Created on Aug 17, 2007 | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | |
| 5 | * in compliance with the License. You may obtain a copy of the License at | |
| 6 | * | |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | * | |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License | |
| 10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | |
| 11 | * or implied. See the License for the specific language governing permissions and limitations under | |
| 12 | * the License. | |
| 13 | * | |
| 14 | * Copyright @2007-2009 the original author or authors. | |
| 15 | */ | |
| 16 | package org.fest.reflect.method; | |
| 17 | ||
| 18 | import static org.fest.reflect.method.Invoker.newInvoker; | |
| 19 | import static org.fest.reflect.method.MethodParameterTypes.newParameterTypes; | |
| 20 | ||
| 21 | /** | |
| 22 | * Understands the return type of the method to invoke. | |
| 23 | * <p> | |
| 24 | * The following is an example of proper usage of this class: | |
| 25 | * <pre> | |
| 26 | * // Equivalent to call 'person.setName("Luke")' | |
| 27 | * {@link org.fest.reflect.core.Reflection#method(String) method}("setName").{@link MethodName#withParameterTypes(Class...) withParameterTypes}(String.class) | |
| 28 | * .{@link MethodParameterTypes#in(Object) in}(person) | |
| 29 | * .{@link Invoker#invoke(Object...) invoke}("Luke"); | |
| 30 | * | |
| 31 | * // Equivalent to call 'person.concentrate()' | |
| 32 | * {@link org.fest.reflect.core.Reflection#method(String) method}("concentrate").{@link MethodName#in(Object) in}(person).{@link Invoker#invoke(Object...) invoke}(); | |
| 33 | * | |
| 34 | * // Equivalent to call 'person.getName()' | |
| 35 | * String name = {@link org.fest.reflect.core.Reflection#method(String) method}("getName").{@link MethodName#withReturnType(Class) withReturnType}(String.class) | |
| 36 | * .{@link MethodReturnType#in(Object) in}(person) | |
| 37 | * .{@link Invoker#invoke(Object...) invoke}(); | |
| 38 | * </pre> | |
| 39 | * </p> | |
| 40 | * | |
| 41 | * @param <T> the generic type of the method's return type. | |
| 42 | * | |
| 43 | * @author Yvonne Wang | |
| 44 | * @author Alex Ruiz | |
| 45 | */ | |
| 46 | public class MethodReturnType<T> { | |
| 47 | ||
| 48 | 5 |
static <T> MethodReturnType<T> newReturnType(String name, Class<T> type) { |
| 49 | 5 | if (type == null) throw new NullPointerException("The return type of the method to access should not be null"); |
| 50 | 4 | return new MethodReturnType<T>(name); |
| 51 | } | |
| 52 | ||
| 53 | private final String name; | |
| 54 | ||
| 55 | 4 |
private MethodReturnType(String name) { |
| 56 | 4 | this.name = name; |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Creates a new method invoker. | |
| 61 | * @param target the object containing the method to invoke. | |
| 62 | * @return the created method invoker. | |
| 63 | * @throws NullPointerException if the given target is <code>null</code>. | |
| 64 | */ | |
| 65 | 3 |
public Invoker<T> in(Object target) { |
| 66 | 3 | return newInvoker(name, target); |
| 67 | } | |
| 68 | ||
| 69 | /** | |
| 70 | * Specifies the parameter types of the method to invoke. This method call is optional if the method to invoke does | |
| 71 | * not take arguments. | |
| 72 | * @param parameterTypes the parameter types of the method to invoke. | |
| 73 | * @return the created parameter types holder. | |
| 74 | * @throws NullPointerException if the array of parameter types is <code>null</code>. | |
| 75 | */ | |
| 76 | 1 |
public MethodParameterTypes<T> withParameterTypes(Class<?>... parameterTypes) { |
| 77 | 1 | return newParameterTypes(name, parameterTypes); |
| 78 | } | |
| 79 | } | |
|
||||||||||||