001    /*
002     * Created on Jan 25, 2009
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * 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 is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     *
013     * Copyright @2009 the original author or authors.
014     */
015    package org.fest.reflect.method;
016    
017    import static org.fest.reflect.method.Invoker.newInvoker;
018    import static org.fest.reflect.method.StaticMethodParameterTypes.newParameterTypes;
019    
020    import org.fest.reflect.reference.TypeRef;
021    
022    /**
023     * Understands the return type 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.getCommonPowers()'
028     *   List&lt;String&gt; powers = {@link org.fest.reflect.core.Reflection#staticMethod(String) staticMethod}("getCommonPowers").{@link StaticMethodName#withReturnType(TypeRef) withReturnType}(new {@link TypeRef TypeRef}&lt;List&lt;String&gt;&gt;() {})
029     *                                            .{@link StaticMethodReturnTypeRef#in(Class) in}(Jedi.class)
030     *                                            .{@link Invoker#invoke(Object...) invoke}();
031     * </pre>
032     * </p>
033     *
034     * @param <T> the generic type of the static method's return type.
035     *
036     * @author Alex Ruiz
037     *
038     * @since 1.1
039     */
040    public class StaticMethodReturnTypeRef<T> {
041    
042      static <T> StaticMethodReturnTypeRef<T> newReturnTypeRef(String name, TypeRef<T> type) {
043        if (type == null)
044          throw new NullPointerException("The return type reference of the static method to access should not be null");
045        return new StaticMethodReturnTypeRef<T>(name);
046      }
047    
048      private final String name;
049    
050      private StaticMethodReturnTypeRef(String name) {
051        this.name = name;
052      }
053    
054      /**
055       * Creates a new method invoker.
056       * @param target the object containing the method to invoke.
057       * @return the created method invoker.
058       * @throws NullPointerException if the given target is <code>null</code>.
059       */
060      public Invoker<T> in(Class<?> target) {
061        return newInvoker(name, target);
062      }
063    
064      /**
065       * Specifies the parameter types of the static method to invoke. This method call is optional if the method to invoke
066       * does not take arguments.
067       * @param parameterTypes the parameter types of the method to invoke.
068       * @return the created parameter types holder.
069       * @throws NullPointerException if the array of parameter types is <code>null</code>.
070       */
071      public StaticMethodParameterTypes<T> withParameterTypes(Class<?>... parameterTypes) {
072        return newParameterTypes(name, parameterTypes);
073      }
074    }