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