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