Reflection in java - Methods used and program


Contents of page :
  • Reflection in java
  • Let’s understand step-by-step how Reflection program will work in java >
  • Let’s create simple program to demonstrate concept of Reflection in java >
    • Exceptions thrown by methods used in reflection >
    • Alternatively, for loading class we can use ClassLoader.getSystemClassLoader()>
  • Let’s create program to understand how we can pass String and Integer as argument in method in java >



Reflection in java>
Reflection is used to load java classes at runtime. Frameworks like struts, spring and hibernate uses reflection for loading classes at runtime.


Let’s understand step-by-step how Reflection program will work in java >

Step 1 > load 'ReflectionClass' at runtime, please ensure you write package name before class name.
Step 2 > create object of ReflectionClass by calling constructor of ReflectionClass
Step 3 > calling methodNoPara() method of ReflectionClass.

Let’s create simple program to demonstrate concept of Reflection in java >
package reflection1;
import java.lang.reflect.Method;
class ReflectionClass {
   /**
   * constructor
   */
   public ReflectionClass() {
          System.out.println("in constructor of ReflectionClass");
   }
   /**
   * Method with no parameter
   */
   public void methodNoPara() {
          System.out.println("in methodNoPara() of ReflectionClass ");
   }
}
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class
*/
public class ReflectionTest {
   public static void main(String... a) {
          try {
                 // step 1, load 'ReflectionClass' at runtime
                 Class cls = Class.forName("reflection1.ReflectionClass");
                 // step 2 //will call constructor of ReflectionClass
                 Object object = cls.newInstance();
                
                 // step 3, calling methodNoPara()
                 Method method = cls.getMethod("methodNoPara", null);
                 method.invoke(object, null);
          } catch (Exception e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
in constructor of ReflectionClass
in methodNoPara() of ReflectionClass
*/

Exceptions thrown by methods used in reflection in java >

Method
Class.forName("reflection1.ReflectionClass")
ClassNotFoundException
cls.newInstance()
InstantiationException
IllegalAccessException
cls.getMethod("methodNoPara", null)
SecurityException
NoSuchMethodException


Alternatively, for loading class we can use ClassLoader.getSystemClassLoader()>
Replace
Class.forName("reflection1.ReflectionClass");

with below code >
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Class cls = classLoader.loadClass("ReflectionClass");




Let’s create program to understand how we can pass String and Integer as argument in method in java>
package reflection2;
import java.lang.reflect.Method;
class ReflectionClass {
   /**
   * constructor
   */
   public ReflectionClass() {
          System.out.println("in constructor of ReflectionClass");
   }
   /**
   * Method with no parameter
   */
   public void methodNoPara() {
          System.out.println("in methodNoPara() of ReflectionClass");
   }
   /**
   * Method with String parameter
   */
   public void methodString(String str) {
          System.out.println("in methodString()   > " + str);
   }
   /**
   * Method with Integer parameter
   */
   public void methodInteger(Integer integer) {
          System.out.println("in methodInteger()  > " + integer);
   }
}
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class
*/
public class ReflectionTest {
   public static void main(String... a) {
          try {
                 //String parameter
                 Class stringPara[] = new Class[1];
                 stringPara[0] = String.class;
                 Class integerPara[] = new Class[1];  
                 integerPara[0] = Integer.class;
                
                 // step 1, load 'ReflectionClass' at runtime
                 Class cls = Class.forName("reflection2.ReflectionClass");
                
                 // step 2 //will call constructor of ReflectionClass
                 Object object = cls.newInstance();
                
                 // step 3a, calling methodNoPara()
                 Method method = cls.getMethod("methodNoPara", null);
                 method.invoke(object, null);
                 // step 3b, calling methodString()
                 method = cls.getMethod("methodString", stringPara);
                 method.invoke(object, "ankit");
                 // step 3c, calling methodInteger()
                 method = cls.getMethod("methodInteger", integerPara);
                 method.invoke(object, 911);
            
          } catch (Exception e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
in constructor of ReflectionClass
in methodNoPara() of ReflectionClass
in methodString()   > ankit
in methodInteger()  > 911
*/

No comments:

Post a Comment