reflection api in java

reflection API in java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.

Reflection API

Reflection means an ability of a software to analyze itself. In Java, Reflection API provides a facility to analyze and change the runtime behavior of a Class, at runtime.
reflection API in java









For example, using reflection at the runtime you can determine what method, field, constructor or modifiers a class supports.

What is reflect package?

java.lang.reflect package encapsulates several important interfaces and classes. These classes and interface define methods which are used for reflection.

Some Important Classes of java.lang.reflect package

Class,
Class, does ?
Arrayallow you to dynamically create and manipulate arrays
Constructorgives information about constructor of a class
Fieldprovide information about field
Ma methodprovides information about method
the Modifierprovide information about class the  end member access modifier
Proxysupports dynamic proxy classes
Apart from these classes java.lang.Class is another very important class used in Reflection API.

Uses of Reflection

  • Developing IDE
  • Debugging and Test tools
  • Loading drivers and providing dynamic information
  • Disadvantages of Reflection

    • Low performance
    • Security risk
    • Violation of Oops concept
    • java.lang.Class class

      Class is a final class in java.lang package which extends Object class. An instance of this class represents classes and interfaces in a running Java application. It is used to analyze and change the dynamic behavior of a class at runtime.

      Some Important Methods of java.lang.Class class

      This class defines several methods using which we can get information about methodsconstructorsmodifiers and members of a class at runtime.

      forName()

      This method takes the fully qualified name of classes or interface as its argument and returns an instance of the class associated with it. Syntax
      static Class< ?> forName(String className)
      

      Example using forName() method

    • class Student{}
      class Test
      {
       public static void main(String args[])
       {
        Class c = Class.forName("Student");
        System.out.println(c.getName());
       }
      }
      
      Output :
      Student
      

      getConstructors() and getDeclaredConstructors()

      getConstructors() the method returns an array of Constructors object that represents all the public constructors of the invoking object. Remember, this method only returns public constructors. If you want to see all the declared constructors of a class then use.getDeclaredConstructors() Following is the general syntax of both,
    •  
      forName()










Constructor< ?>[] getConstructors();
Constructor< ?>[] getDeclaredConstructors();

Example using getConstructors() and getDeclaredConstructors() method

import java.lang.reflect.*;
class Student
{
 public Student(){}
 public Student(String name){}
}

class Test
{
 public static void main(String args[])
 {
  try
  {
  Class c = Class.forName("Student");
  Constructor< Student>[] ct = c.getConstructors();
  for(int i=0; i< ct.length; i++)
    { System.out.println(ct[i]); }
  Constructor< Student>[] cdt = c.getDeclaredConstructors();
  for(int i=0;i< cdt.length;i++)
    { System.out.println(cdt[i]);}

  }
  catch(Exception e)
  { e.printStackTrace();}
 }
}
Output :
  public Student()
  public Student()
  Student(java.lang.String)

getMethods() and getDeclaredMethods()

getMethods() the method returns an array of Method object that reflects all the public method of invoking the object. getDeclaredMethods() returns only the declared methods of the invoking class object. The syntax for both is following,
Method< ?>[] getMethods();
Method< ?>[] getDeclaredMethods();

Example using getDeclaredMethods() method

import java.lang.reflect.*;
class Student
{
 public void show(){}
 void display(){}
}

class Test
{
 public static void main(String args[])
 {
  try
  {
   Class c = Class.forName("Student");
   Method md[] = c.getDeclaredMethods();
   for(int i=0; i< md.length; i++ )
    { System.out.println(md[i]); }
  }
  catch(Exception e)
  { e.printStackTrace();}
 }
}
Output :
public void Student.show()
  void Student.display()

getFields() and getDeclaredFields()

getFields() returns an array containing Field objects reflecting all the accessible public members of the class or interface represented by this Class object. getDeclaredFields() returns array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.
Field< ?>[] getFields();
Field< ?>[] getDeclaredFields();

Example using getFields() and getDeclaredFields() method

import java.lang.reflect.*;
class Student
{
 public  String name;
 int roll;
}

class Test
{
 public static void main(String args[])
 {
  try
  {
   Class c = Class.forName("Student");
   Field ff[] = c.getFields();
   for(int i=0; i< ff.length; i++)
    { System.out.println(ff[i]); }
   Field f[] = c.getDeclaredFields();
   for(int i=0;i< f.length; i++)
    { System.out.println(f[i]);  }
  }
  catch(Exception e)
  { e.printStackTrace();}
 }
}
Output :
public java.lang.String Student.name
  public java.lang.String Student.name
  int Student.roll






















































































Comments