Monday, November 3, 2014

Listing all the Constructors and Methods of a Class/Interface using Reflection API (Java)

The reflection API can be used to list all the available public methods and constructors of a class/interface.

We will use java.util.regex package to display

public int indexOf(String,int)

instead of 

public int java.lang.String.indexOf(java.lang.String,int)

1. Declare a Class variable (say c).
2. Assign to it the result of Class.forName("Fully.Qualified.ClassName")

3. Call c.getDeclaredConstructors() to get an array of Constructor objects.
4. Apply regex filter to shorten class names.
5. Display results to the user.

6. Call c.getDeclaredMethods() to get an array of Method objects.
7. Apply regex filter to shorten class names.
8. Display results to the user. 

c.getConstructors() only provides you the public constructors whereas c.getDeclaredConstructors() provides you all (public, private, protected, package(default access)) constructors.

Same goes with c.getMethods() and c.getDeclaredMethods()


import java.lang.reflect.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReflectionMethods {
    public static void main(String[] args)throws Exception{
        Class c = Class.forName("java.lang.String");
        //Printing Constructors
        System.out.println("Listing contructors: ");
        Constructor[] constructors = c.getDeclaredConstructors(); 
        //Clipping off "java.lang." etc.  (Full class name prefixes)
        Pattern p1 = Pattern.compile(" ([a-zA-Z]+\\.)+");  
        //Clipping off "(java.lang." etc. (Full class name prefixes in arguments)
        Pattern p2 = Pattern.compile("\\(([a-zA-Z]+\\.)+");  
        //Clipping off ",java.lang." etc. (Commas in multiple arguments)
        Pattern p3 = Pattern.compile(",([a-zA-Z]+\\.)+");    
        Matcher m;
        for(Constructor constructor:constructors){
            String constructorString = constructor.toString();
            m = p1.matcher(constructorString);  
            constructorString = m.replaceAll(" ");
            m = p2.matcher(constructorString);
            constructorString = m.replaceAll("(");
            m = p3.matcher(constructorString);
            constructorString = m.replaceAll(", ");
        Method[] methods = c.getDeclaredMethods(); 
        p1 = Pattern.compile(" ([a-zA-Z]+\\.)+");
        p2 = Pattern.compile("\\(([a-zA-Z]+\\.)+"); 
        p3 = Pattern.compile(",([a-zA-Z]+\\.)+");
        //Printing Methods
        System.out.println("\n\nListing Class Methods: ");
        for(Method method:methods){
            String methodString = method.toString();
            m = p1.matcher(methodString);  
            methodString = m.replaceAll(" ");
            m = p2.matcher(methodString);
            methodString = m.replaceAll("(");
            m = p3.matcher(methodString);
            methodString = m.replaceAll(", ");


Listing contructors: 
public String(byte[],int,int)
public String(byte[], Charset)
public String(byte[], String) throws UnsupportedEncodingException
public String(byte[],int,int, Charset)
public String(byte[],int,int, String) throws UnsupportedEncodingException
public String(StringBuilder)
public String(StringBuffer)
public String(byte[])
public String(int[],int,int)
public String()
public String(char[])
public String(String)
public String(char[],int,int)
public String(byte[],int)
public String(byte[],int,int,int)

Listing Class Methods: 
public boolean equals(Object)
public String toString()
public int hashCode()
public int compareTo(String)
public int compareTo(Object)
public int indexOf(String,int)
public int indexOf(String)
public int indexOf(int,int)
public int indexOf(int)
static int indexOf(char[],int,int,char[],int,int,int)
static int indexOf(char[],int,int, String,int)
public static String valueOf(int)
public static String valueOf(long)
public static String valueOf(float)
public static String valueOf(boolean)
public static String valueOf(char[])
public static String valueOf(char[],int,int)
public static String valueOf(Object)
public static String valueOf(char)
public static String valueOf(double)
public char charAt(int)
private static void checkBounds(byte[],int,int)
public int codePointAt(int)
public int codePointBefore(int)
public int codePointCount(int,int)
public int compareToIgnoreCase(String)
public String concat(String)
public boolean contains(CharSequence)
public boolean contentEquals(CharSequence)
public boolean contentEquals(StringBuffer)
public static String copyValueOf(char[])
public static String copyValueOf(char[],int,int)
public boolean endsWith(String)
public boolean equalsIgnoreCase(String)
public static String format(Locale, String, Object[])
public static String format(String, Object[])
public void getBytes(int,int,byte[],int)
public byte[] getBytes(Charset)
public byte[] getBytes(String) throws UnsupportedEncodingException
public byte[] getBytes()
public void getChars(int,int,char[],int)
void getChars(char[],int)
private int indexOfSupplementary(int,int)
public native String intern()
public boolean isEmpty()
public static String join(CharSequence, CharSequence[])
public static String join(CharSequence, Iterable)
public int lastIndexOf(int)
public int lastIndexOf(String)
static int lastIndexOf(char[],int,int, String,int)
public int lastIndexOf(String,int)
public int lastIndexOf(int,int)
static int lastIndexOf(char[],int,int,char[],int,int,int)
private int lastIndexOfSupplementary(int,int)
public int length()
public boolean matches(String)
private boolean nonSyncContentEquals(AbstractStringBuilder)
public int offsetByCodePoints(int,int)
public boolean regionMatches(int, String,int,int)
public boolean regionMatches(boolean,int, String,int,int)
public String replace(char,char)
public String replace(CharSequence, CharSequence)
public String replaceAll(String, String)
public String replaceFirst(String, String)
public String[] split(String)
public String[] split(String,int)
public boolean startsWith(String,int)
public boolean startsWith(String)
public CharSequence subSequence(int,int)
public String substring(int)
public String substring(int,int)
public char[] toCharArray()
public String toLowerCase(Locale)
public String toLowerCase()
public String toUpperCase()
public String toUpperCase(Locale)
public String trim()

No comments:

Post a Comment