|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| MethodName | Line # 56 | 11 | 0% | 9 | 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 | import static org.fest.reflect.method.MethodReturnType.newReturnType; | |
| 21 | import static org.fest.reflect.method.MethodReturnTypeRef.newReturnTypeRef; | |
| 22 | import static org.fest.util.Strings.isEmpty; | |
| 23 | ||
| 24 | import org.fest.reflect.reference.TypeRef; | |
| 25 | ||
| 26 | ||
| 27 | ||
| 28 | /** | |
| 29 | * Understands the name of a method to invoke using Java Reflection. | |
| 30 | * <p> | |
| 31 | * The following is an example of proper usage of this class: | |
| 32 | * <pre> | |
| 33 | * // Equivalent to call 'person.setName("Luke")' | |
| 34 | * {@link org.fest.reflect.core.Reflection#method(String) method}("setName").{@link MethodName#withParameterTypes(Class...) withParameterTypes}(String.class) | |
| 35 | * .{@link MethodParameterTypes#in(Object) in}(person) | |
| 36 | * .{@link Invoker#invoke(Object...) invoke}("Luke"); | |
| 37 | * | |
| 38 | * // Equivalent to call 'person.concentrate()' | |
| 39 | * {@link org.fest.reflect.core.Reflection#method(String) method}("concentrate").{@link MethodName#in(Object) in}(person).{@link Invoker#invoke(Object...) invoke}(); | |
| 40 | * | |
| 41 | * // Equivalent to call 'person.getName()' | |
| 42 | * String name = {@link org.fest.reflect.core.Reflection#method(String) method}("getName").{@link MethodName#withReturnType(Class) withReturnType}(String.class) | |
| 43 | * .{@link MethodReturnType#in(Object) in}(person) | |
| 44 | * .{@link Invoker#invoke(Object...) invoke}(); | |
| 45 | * | |
| 46 | * // Equivalent to call 'jedi.getPowers()' | |
| 47 | * List<String> powers = {@link org.fest.reflect.core.Reflection#method(String) method}("getPowers").{@link MethodName#withReturnType(TypeRef) withReturnType}(new {@link TypeRef TypeRef}<List<String>>() {}) | |
| 48 | * .{@link MethodReturnTypeRef#in(Object) in}(person) | |
| 49 | * .{@link Invoker#invoke(Object...) invoke}(); | |
| 50 | * </pre> | |
| 51 | * </p> | |
| 52 | * | |
| 53 | * @author Yvonne Wang | |
| 54 | * @author Alex Ruiz | |
| 55 | */ | |
| 56 | public final class MethodName { | |
| 57 | ||
| 58 | /** | |
| 59 | * Creates a new <code>{@link MethodName}</code>: the starting point of the fluent interface for accessing methods | |
| 60 | * using Java Reflection. | |
| 61 | * @param name the name of the method to invoke using Java Reflection. | |
| 62 | * @return the created <code>MethodName</code>. | |
| 63 | * @throws NullPointerException if the given name is <code>null</code>. | |
| 64 | * @throws IllegalArgumentException if the given name is empty. | |
| 65 | */ | |
| 66 | 20 |
public static MethodName startMethodAccess(String name) { |
| 67 | 20 | validateIsNotNullOrEmpty(name); |
| 68 | 18 | return new MethodName(name); |
| 69 | } | |
| 70 | ||
| 71 | 20 |
private static void validateIsNotNullOrEmpty(String name) { |
| 72 | 20 | if (name == null) |
| 73 | 1 | throw new NullPointerException("The name of the method to access should not be null"); |
| 74 | 19 | if (isEmpty(name)) |
| 75 | 1 | throw new IllegalArgumentException("The name of the method to access should not be empty"); |
| 76 | } | |
| 77 | ||
| 78 | private final String name; | |
| 79 | ||
| 80 | 18 |
private MethodName(String name) { |
| 81 | 18 | this.name = name; |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * Specifies the return type of the method to invoke. This method call is optional if the return type of the method to | |
| 86 | * invoke is <code>void</code>. | |
| 87 | * @param <T> the generic type of the method's return type. | |
| 88 | * @param type the return type of the method to invoke. | |
| 89 | * @return the created return type holder. | |
| 90 | * @throws NullPointerException if the given type is <code>null</code>. | |
| 91 | */ | |
| 92 | 5 |
public <T> MethodReturnType<T> withReturnType(Class<T> type) { |
| 93 | 5 | return newReturnType(name, type); |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * Specifies the return type reference of the method to invoke. This method call is optional if the return type of the | |
| 98 | * method to invoke is <code>void</code>. | |
| 99 | * @param <T> the generic type of the method's return type. | |
| 100 | * @param type the return type reference of the method to invoke. | |
| 101 | * @return the created return type holder. | |
| 102 | * @throws NullPointerException if the given type reference is <code>null</code>. | |
| 103 | * @since 1.1 | |
| 104 | */ | |
| 105 | 3 |
public <T> MethodReturnTypeRef<T> withReturnType(TypeRef<T> type) { |
| 106 | 3 | return newReturnTypeRef(name, type); |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * Specifies the parameter types of the method to invoke. This method call is optional if the method to invoke does | |
| 111 | * not take arguments. | |
| 112 | * @param parameterTypes the parameter types of the method to invoke. | |
| 113 | * @return the created parameter types holder. | |
| 114 | * @throws NullPointerException if the array of parameter types is <code>null</code>. | |
| 115 | */ | |
| 116 | 4 |
public MethodParameterTypes<Void> withParameterTypes(Class<?>... parameterTypes) { |
| 117 | 4 | return newParameterTypes(name, parameterTypes); |
| 118 | } | |
| 119 | ||
| 120 | /** | |
| 121 | * Creates a new invoker for a method that takes no parameters and return value <code>void</code>. | |
| 122 | * @param target the object containing the method to invoke. | |
| 123 | * @return the created method invoker. | |
| 124 | * @throws NullPointerException if the given target is <code>null</code>. | |
| 125 | */ | |
| 126 | 5 |
public Invoker<Void> in(Object target) { |
| 127 | 5 | return newInvoker(name, target); |
| 128 | } | |
| 129 | } | |
|
||||||||||||