Thursday 26 October 2017

How to access the methods of a class using Java Reflection | Reflection in java


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=yVR-4jTp2fM&list=UUhwKlOVR041tngjerWxVccw

ReflectionDemo.java
import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * 
 * We can access the methods of a class.
 *
 */
public class ReflectionDemo
{
    public static void main(String[] args)
    {

        Class<ArrayList> classObj = ArrayList.class;

        /*
         * Returns:the array of Method objects representing the public methods
         * of this class.
         */

        Method[] methodArray = classObj.getMethods();

        for (Method method : methodArray)
        {
            System.out.println(method);
        }

    }

}
Output
public boolean java.util.ArrayList.add(java.lang.Object)
public void java.util.ArrayList.add(int,java.lang.Object)
public boolean java.util.ArrayList.remove(java.lang.Object)
public java.lang.Object java.util.ArrayList.remove(int)
public java.lang.Object java.util.ArrayList.get(int)
public java.lang.Object java.util.ArrayList.clone()
public int java.util.ArrayList.indexOf(java.lang.Object)
public void java.util.ArrayList.clear()
public boolean java.util.ArrayList.contains(java.lang.Object)
public boolean java.util.ArrayList.isEmpty()
public java.util.Iterator java.util.ArrayList.iterator()
public int java.util.ArrayList.lastIndexOf(java.lang.Object)
public void java.util.ArrayList.replaceAll(java.util.function.UnaryOperator)
public int java.util.ArrayList.size()
public java.util.List java.util.ArrayList.subList(int,int)
public java.lang.Object[] java.util.ArrayList.toArray(java.lang.Object[])
public java.lang.Object[] java.util.ArrayList.toArray()
public java.util.Spliterator java.util.ArrayList.spliterator()
public boolean java.util.ArrayList.addAll(int,java.util.Collection)
public boolean java.util.ArrayList.addAll(java.util.Collection)
public void java.util.ArrayList.forEach(java.util.function.Consumer)
public java.lang.Object java.util.ArrayList.set(int,java.lang.Object)
public void java.util.ArrayList.ensureCapacity(int)
public void java.util.ArrayList.trimToSize()
public java.util.ListIterator java.util.ArrayList.listIterator()
public java.util.ListIterator java.util.ArrayList.listIterator(int)
public boolean java.util.ArrayList.removeAll(java.util.Collection)
public boolean java.util.ArrayList.removeIf(java.util.function.Predicate)
public boolean java.util.ArrayList.retainAll(java.util.Collection)
public void java.util.ArrayList.sort(java.util.Comparator)
public boolean java.util.AbstractList.equals(java.lang.Object)
public int java.util.AbstractList.hashCode()
public java.lang.String java.util.AbstractCollection.toString()
public boolean java.util.AbstractCollection.containsAll(java.util.Collection)
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
public default java.util.stream.Stream java.util.Collection.stream()
public default java.util.stream.Stream java.util.Collection.parallelStream()

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_access_methods.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_access_methods

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/6c95796df09abfb09587d07613e2e6d5f9ad024e/BasicJava/ReflectionDemo_access_methods/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment