Saturday 16 August 2014

Get Method Parameters and Return Type

Method 'getParameterTypes' of class 'Method' is used to get all the parameter types of this function.

Method 'getReturnType' of class 'Method' is used to get the return type of the function.

public class Employee {
    String firstName, lastName;
    int id, age;
    
    private String getFirstName(){
        return firstName;
    }
    
    String getLastName(){
        return lastName;
    }
    
    int getId(){
        return id;
    }
    
    int getAge(){
        return age;
    }
    
    void setFirstName(String firstName){
        this.firstName = firstName;
    }
    
    void setLastName(String lastName){
        this.lastName = lastName;
    }
    
    void setAge(int age){
        this.age = age;
    }
    
    void setId(int id){
        this.id = id;
    }
    
    void setNames(String firstName, String lastName){
        setFirstName(firstName);
        setLastName(lastName);
    }
}

import java.lang.reflect.*;

public class GetDecllaredMethods {
    public static void main(String args[]) throws ClassNotFoundException{
        Class myClass = Class.forName("Employee");
        Method[] meth = myClass.getDeclaredMethods();
        
        System.out.println("Methods in Class Employee are");
        for(Method m : meth){
            System.out.println(m +" ");
            Class[] parameterTypes = m.getParameterTypes();
            System.out.println("Parameter types are");
            for(Class c : parameterTypes){
                System.out.println(c.getName());
            }
            System.out.print("Return type is : ");
            System.out.println(m.getReturnType());
            System.out.println("---------------------------");
        }
    }
}

Output
Methods in Class Employee are
int Employee.getId() 
Parameter types are
Return type is : int
---------------------------
int Employee.getAge() 
Parameter types are
Return type is : int
---------------------------
void Employee.setAge(int) 
Parameter types are
int
Return type is : void
---------------------------
void Employee.setId(int) 
Parameter types are
int
Return type is : void
---------------------------
void Employee.setNames(java.lang.String,java.lang.String) 
Parameter types are
java.lang.String
java.lang.String
Return type is : void
---------------------------
private java.lang.String Employee.getFirstName() 
Parameter types are
Return type is : class java.lang.String
---------------------------
java.lang.String Employee.getLastName() 
Parameter types are
Return type is : class java.lang.String
---------------------------
void Employee.setFirstName(java.lang.String) 
Parameter types are
java.lang.String
Return type is : void
---------------------------
void Employee.setLastName(java.lang.String) 
Parameter types are
java.lang.String
Return type is : void
---------------------------


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment