java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow java.lang.reflect arrow Calling a method dynamically using reflection
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4094
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Calling a method dynamically using reflection E-mail
User Rating: / 39
PoorBest 

The following examples shows how to call methods dynamically using reflection.

import java.lang.reflect.*;
import java.io.*;

public class testReflect {
   public static void main(String s[]) {
      testReflect t = new testReflect();
      try {
         t.doit();
         }
      catch (Exception e) {
         e.printStackTrace();
         }
      }
   public void doit() throws Exception {
      String aClass;
      String aMethod;   
      // we assume that called methods have no argument
      Class params[] {};
      Object paramsObj[] {};
      
      while (true) {
         /* examples 
            Class: class1         Method: class1Method2
            Class: java.util.Date Method: toString
            Class: java.util.Date Method: getTime
         */
         aClass  = Input.Line("\nClass : ");
         aMethod = Input.Line("Method: ");
         // get the Class
         Class thisClass = Class.forName(aClass);
         // get an instance 
         Object iClass = thisClass.newInstance();
         // get the method
         Method thisMethod = thisClass.getDeclaredMethod(aMethod, params);
         // call the method
         System.out.println(thisMethod.invoke(iClass, paramsObj).toString());
         }
      }
   
   static class Input {
     public static String Line(String sthrows IOException {
         BufferedReader input = 
             new BufferedReader(new InputStreamReader(System.in));
         System.out.print(s);
         return input.readLine();     
        }
     }
}

class class1 {
   public String class1method1() {
      return "*** Class 1, Method1 ***";
      }
   public String class1method2() {
      return "### Class 1, Method2 ###";
      }
}

The next example calls a class method with 2 arguments:

import java.lang.reflect.*;

public class TestReflect {
  static void invoke(String aClass, String aMethod, Class[] params, Object[] args) {
    try {
      Class c = Class.forName(aClass);
      Method m = c.getDeclaredMethod(aMethod, params);      
      Object i = c.newInstance();
      Object r = m.invoke(i, args);
      
    catch (Exception e) {
      e.printStackTrace();
      
    }

  public static void main(String[] args) {
    invoke("Class1""say"new Class[] {String.class, String.class}
           new Object[] 
             {new String("Hello")new String("World")});
    }
}

class Class1 {
  public void sayString s1, String s2) {
    System.out.println(s1 + " " + s2);
    }
}   

 Related Tips

 
Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.