Recent Tutorials and Articles
    Listing class methods using Java Reflections
    Published on: 10th August 2013

    This tutorial provides the code to list down all the methods (private, protected, default and public) of a class using Reflections API in Java.

    Reflections API is a powerful tool provided by Java to developers as it lets you access all (with any access modifier - public, private, protected and default) the members (instance and static fields & methods), provided you are not running in restricted context e.g. Applets where Security manager will not let you do most of reflections operations.

    Below is the program to print all the methods of a class using Reflections API in Java.

    MethodsPrinter.java
    package com.sts.allprogrammingtutorials.generics;
    
    import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;
    
    /**
     * @author Sain Technology Solutions
     *
     */
    public class MethodsPrinter {
    	
    	public void print(Class<?> clazz) {
    		
    		for (Method method : clazz.getDeclaredMethods()) { // get all the methods and iterate over those
    			//nested class to build the method components
    			final MethodComponentsBuilder methodComponentsBuilder = new MethodComponentsBuilder(method);
    			
    			System.out.println(methodComponentsBuilder.getModifier() + " "
    					+ methodComponentsBuilder.getReturnType() + " "
    					+ method.getName() +"(" + methodComponentsBuilder.getParameters() + ")" 
    					+ methodComponentsBuilder.getExceptions());
    		}
    		
    	}
    	
    	private static class MethodComponentsBuilder {
    		private final Method method;
    		
    		public MethodComponentsBuilder(final Method method) {
    			this.method = method;
    		}
    		
    		
    		/**
    		 * @return modifiers such as public, private, protected, static etc.
    		 */
    		public String getModifier() {
    			return Modifier.toString(method.getModifiers());
    		}
    		
    		/**
    		 * @return the return type of a method
    		 */
    		public String getReturnType() {
    			return method.getReturnType().getCanonicalName();
    		}
    		
    		/**
    		 * @return the input parameters separated by a comma
    		 */
    		public String getParameters() {
    			StringBuilder parameters = new StringBuilder();
    			
    			for(Class<?> clazz : method.getParameterTypes()) {
    				parameters.append(clazz.getCanonicalName());
    				parameters.append(",");
    			}
    			
    			if(parameters.length() > 0 ) {
    				parameters = parameters.deleteCharAt(parameters.length()-1);
    			}
    			return parameters.toString();
    		}
    		
    		/**
    		 * @return the exceptions that the method declares in its throws clause
    		 */
    		public String getExceptions() {
    			
    			StringBuilder exceptions = new StringBuilder();
    			
    			for(Class<?> clazz : method.getExceptionTypes()) {
    				exceptions.append(clazz.getCanonicalName());
    				exceptions.append(",");
    			}
    			
    			if(exceptions.length() > 0 ) {
    				exceptions = exceptions.insert(0, " throws ");
    				exceptions = exceptions.deleteCharAt(exceptions.length()-1);
    			}
    			return exceptions.toString();
    		}
    	}
    }
    
    

    Following is the demo class that calls the above program by passing in the class which we want to print all the methods of -

    MethodsPrinterDemo.java
    package com.sts.allprogrammingtutorials.generics;
    
    
    /**
     * @author Sain Technology Solutions
     * 
     */
    public class MethodsPrinterDemo {
    
    	/**
    	 * @param args
    	 * @throws ClassNotFoundException 
    	 */
    	public static void main(String[] args) throws ClassNotFoundException {
    		
    		final Class<?> clazz = Class.forName("java.lang.Object");
    
    		MethodsPrinter methodsPrinter = new MethodsPrinter();
    		methodsPrinter.print(clazz);
    
    	}
    
    }
    
    

    And here is what you get as output after running the above code -

    Output:
    protected void finalize() throws java.lang.Throwable
    public final native void wait(long) throws java.lang.InterruptedException
    public final void wait() throws java.lang.InterruptedException
    public final void wait(long,int) throws java.lang.InterruptedException
    public boolean equals(java.lang.Object)
    public java.lang.String toString()
    public native int hashCode()
    public final native java.lang.Class getClass()
    protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException
    private static native void registerNatives()
    public final native void notify()
    public final native void notifyAll()
    
    

    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Published on: 10th August 2013

    Comment Form is loading comments...