org.fest.reflect.method.MethodReturnType Maven / Gradle / Ivy
/*
* Created on Aug 17, 2007
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
* Copyright @2007-2009 the original author or authors.
*/
package org.fest.reflect.method;
import static org.fest.reflect.method.Invoker.newInvoker;
import static org.fest.reflect.method.MethodParameterTypes.newParameterTypes;
/**
* Understands the return type of the method to invoke.
*
* The following is an example of proper usage of this class:
*
*
* // Equivalent to call 'person.setName("Luke")'
* {@link org.fest.reflect.core.Reflection#method(String) method}("setName").{@link MethodName#withParameterTypes(Class...) withParameterTypes}(String.class)
* .{@link MethodParameterTypes#in(Object) in}(person)
* .{@link Invoker#invoke(Object...) invoke}("Luke");
*
* // Equivalent to call 'person.concentrate()'
* {@link org.fest.reflect.core.Reflection#method(String) method}("concentrate").{@link MethodName#in(Object) in}(person).{@link Invoker#invoke(Object...) invoke}();
*
* // Equivalent to call 'person.getName()'
* String name = {@link org.fest.reflect.core.Reflection#method(String) method}("getName").{@link MethodName#withReturnType(Class) withReturnType}(String.class)
* .{@link MethodReturnType#in(Object) in}(person)
* .{@link Invoker#invoke(Object...) invoke}();
*
*
*
* @param the generic type of the method's return type.
*
* @author Yvonne Wang
* @author Alex Ruiz
*/
public class MethodReturnType {
static MethodReturnType newReturnType(String name, Class type) {
if (type == null) throw new NullPointerException("The return type of the method to access should not be null");
return new MethodReturnType(name);
}
private final String name;
private MethodReturnType(String name) {
this.name = name;
}
/**
* Creates a new method invoker.
* @param target the object containing the method to invoke.
* @return the created method invoker.
* @throws NullPointerException if the given target is null
.
*/
public Invoker in(Object target) {
return newInvoker(name, target);
}
/**
* Specifies the parameter types of the method to invoke. This method call is optional if the method to invoke does not take
* arguments.
* @param parameterTypes the parameter types of the method to invoke.
* @return the created parameter types holder.
* @throws NullPointerException if the array of parameter types is null
.
*/
public MethodParameterTypes withParameterTypes(Class... parameterTypes) {
return newParameterTypes(name, parameterTypes);
}
}