Class MethodName


  • public final class MethodName
    extends java.lang.Object
    Understands the name of a method to invoke using Java Reflection.

    The following is an example of proper usage of this class:

       // Equivalent to call 'person.setName("Luke")'
       method("setName").withParameterTypes(String.class)
                        .in(person)
                        .invoke("Luke");
    
       // Equivalent to call 'person.concentrate()'
       method("concentrate").in(person).invoke();
    
       // Equivalent to call 'person.getName()'
       String name = method("getName").withReturnType(String.class)
                                      .in(person)
                                      .invoke();
    
       // Equivalent to call 'jedi.getPowers()'
       List<String> powers = method("getPowers").withReturnType(new TypeRef<List<String>>() {})
                                                .in(person)
                                                .invoke();
     

    Author:
    Yvonne Wang, Alex Ruiz
    • Method Detail

      • startMethodAccess

        public static MethodName startMethodAccess​(java.lang.String name)
        Creates a new MethodName: the starting point of the fluent interface for accessing methods using Java Reflection.
        Parameters:
        name - the name of the method to invoke using Java Reflection.
        Returns:
        the created MethodName.
        Throws:
        java.lang.NullPointerException - if the given name is null.
        java.lang.IllegalArgumentException - if the given name is empty.
      • withReturnType

        public <T> MethodReturnType<T> withReturnType​(java.lang.Class<T> type)
        Specifies the return type of the method to invoke. This method call is optional if the return type of the method to invoke is void.
        Type Parameters:
        T - the generic type of the method's return type.
        Parameters:
        type - the return type of the method to invoke.
        Returns:
        the created return type holder.
        Throws:
        java.lang.NullPointerException - if the given type is null.
      • withReturnType

        public <T> MethodReturnTypeRef<T> withReturnType​(TypeRef<T> type)
        Specifies the return type reference of the method to invoke. This method call is optional if the return type of the method to invoke is void.
        Type Parameters:
        T - the generic type of the method's return type.
        Parameters:
        type - the return type reference of the method to invoke.
        Returns:
        the created return type holder.
        Throws:
        java.lang.NullPointerException - if the given type reference is null.
        Since:
        1.1
      • withParameterTypes

        public MethodParameterTypes<java.lang.Void> withParameterTypes​(java.lang.Class<?>... parameterTypes)
        Specifies the parameter types of the method to invoke. This method call is optional if the method to invoke does not take arguments.
        Parameters:
        parameterTypes - the parameter types of the method to invoke.
        Returns:
        the created parameter types holder.
        Throws:
        java.lang.NullPointerException - if the array of parameter types is null.
      • in

        public Invoker<java.lang.Void> in​(java.lang.Object target)
        Creates a new invoker for a method that takes no parameters and return value void.
        Parameters:
        target - the object containing the method to invoke.
        Returns:
        the created method invoker.
        Throws:
        java.lang.NullPointerException - if the given target is null.