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    
020    /**
021     * Understands the parameter types of the method to invoke.
022     * <p>
023     * The following is an example of proper usage of this class:
024     * <pre>
025     *   // Equivalent to call 'person.setName("Luke")'
026     *   {@link org.fest.reflect.core.Reflection#method(String) method}("setName").{@link MethodName#withParameterTypes(Class...) withParameterTypes}(String.class)
027     *                    .{@link MethodParameterTypes#in(Object) in}(person)
028     *                    .{@link Invoker#invoke(Object...) invoke}("Luke");
029     *
030     *   // Equivalent to call 'person.concentrate()'
031     *   {@link org.fest.reflect.core.Reflection#method(String) method}("concentrate").{@link MethodName#in(Object) in}(person).{@link Invoker#invoke(Object...) invoke}();
032     *
033     *   // Equivalent to call 'person.getName()'
034     *   String name = {@link org.fest.reflect.core.Reflection#method(String) method}("getName").{@link MethodName#withReturnType(Class) withReturnType}(String.class)
035     *                                  .{@link MethodReturnType#in(Object) in}(person)
036     *                                  .{@link Invoker#invoke(Object...) invoke}();
037     * </pre>
038     * </p>
039     *
040     * @param <T> the generic type of the method's return type.
041     *
042     * @author Yvonne Wang
043     * @author Alex Ruiz
044     */
045    public final class MethodParameterTypes<T> {
046    
047      private final String name;
048      private final Class<?>[] parameterTypes;
049    
050      static <T> MethodParameterTypes<T> newParameterTypes(String name, Class<?>[] parameterTypes) {
051        if (parameterTypes == null)
052          throw new NullPointerException("The array of parameter types for the method to access should not be null");
053        return new MethodParameterTypes<T>(parameterTypes, name);
054      }
055    
056      private MethodParameterTypes(Class<?>[] parameterTypes, String name) {
057        this.name = name;
058        this.parameterTypes = parameterTypes;
059      }
060    
061      /**
062       * Creates a new method invoker.
063       * @param target the object containing the method to invoke.
064       * @return the created method invoker.
065       * @throws NullPointerException if the given target is <code>null</code>.
066       */
067      public Invoker<T> in(Object target) {
068        return newInvoker(name, target, parameterTypes);
069      }
070    }