★ wanayoo — archive 1999 http://codeguru.earthweb.com/java/articles/385.shtmlNouvelle recherche | Portail wanayoo
EarthWeb
Developer.com Click Here!
Click Here!
Find:
 

EXPERT SEARCH
SEARCH TECHCRAWLER -----
Site
windows 2000
visual c++
java
visual basic
javascripts
recommend it
 
Book
thinking in java
 
Interact
forum
guest book
jobs
jokes
what's new

share code
 
Resource
add resource
modify resource
new resource
 

-----
Java by E-mail:

Get the weekly e-mail highlights on Java!
-----



EarthWeb Sites:
IT Management
Networking & Telecommunications
IT Jobs
Software Development
Hardware & Systems
Commerce
Community

EarthWeb Worldwide

-

Call a method dynamically


Author: Real Gagnon
Author's WebSite: http://tactika.com/realhome/realhome.html

Given the class name and function name, call function using reflection API.


 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 s) throws 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 ###";
       }
    }

Posted On: 4-Jul-1999

Give them a visit Click Here!

Click Here!

Get the Answers You Need!
Incentive-based, question & answer Marketplace. The first 5,000 members receive $12 credit! Try it! Knowledge Market.

For tons of Windows technology resources, check out the Windows Programming directory.

Target your Technical searches with
TechCrawler

Sweepstakes:
Here's your chance to win Windows 2000 Professional. EarthWeb's sweepstakes runs through July 2. Click here to enter.

Give them a visit Click Here!

Click Here!

Click Here!

Click Here!

Home About Us Search Subscribe Advertising Info Contact Us FAQs
Use of this site is subject to certain Terms & Conditions.
Earthweb's statement regarding our users' right to privacy.
Copyright ©1996-2000 EarthWeb Inc.. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited.