Using strings to run methods.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tripharn

    Using strings to run methods.

    Hi guys,

    If I want to load an object, or a method from an object... but I don't know
    what it is can I use the value in the string as the method to run???

    For example, I get a string from a user that is 'triangle' stored in temp.
    Now I know that there is a class called triangle can I do something like

    temp variable = new temp();

    Where temp is obviously the string value and not actually the object temp
    itself.

    Ignore all the security problems here, because it is not going to be used in
    any prog... but I need to be able to do it this way for a testing driver.

    Thanks heaps,
    Tripharn


  • N.K.

    #2
    Re: Using strings to run methods.

    Yes, you can do it using reflect package.

    import java.lang.refle ct.*;

    Assuming class Triangle is defined elsewhere in a package
    com.youpackagen ame.Triangle you can do the following:

    I would defined a hashtable H where you have keys like this:
    ("Triangle","co m.yourpackagena me.Triangle")
    ("Square","com. yourpackagename .Square")
    ("Temp","com.yo urpackagename.T emp")

    etc.

    This will load com.yourpackage name.Temp class:

    Class myClass = Class.forName(H .get("Temp")); ( instead of "Temp"
    send in your variable)

    Here is how to get an object of that class:

    Object myObject = myClass.newInst ance();

    Have fun.

    Nadia



    "Tripharn" <telwyn@bigpond .net.au> wrote in message news:<qhv9b.104 827$bo1.6670@ne ws-server.bigpond. net.au>...[color=blue]
    > Hi guys,
    >
    > If I want to load an object, or a method from an object... but I don't know
    > what it is can I use the value in the string as the method to run???
    >
    > For example, I get a string from a user that is 'triangle' stored in temp.
    > Now I know that there is a class called triangle can I do something like
    >
    > temp variable = new temp();
    >
    > Where temp is obviously the string value and not actually the object temp
    > itself.
    >
    > Ignore all the security problems here, because it is not going to be used in
    > any prog... but I need to be able to do it this way for a testing driver.
    >
    > Thanks heaps,
    > Tripharn[/color]

    Comment

    • N.K.

      #3
      Re: Using strings to run methods.

      I didn't show you how to invoke methods where method name is a
      variable. Here it is:

      The following means that a method has two parameters that are strings.
      Method myMethod;

      Class methodSignature[] = {Class.forName( "java.lang.Stri ng"),
      Class.forName(" java.lang.Strin g")};

      myMethod = myClass.getMeth od(variableMeth odName, methodSignature );
      The following specifies the parameters to the method
      Object methodParameter s[] = {value1, value2};
      myMethod.invoke (myObject, methodParameter s);
      (myObject comes from my previous posting)

      Hope it works for you.

      nadiak@parkergl obal.com (N.K.) wrote in message news:<2d4502eb. 0309160525.662e 8be@posting.goo gle.com>...[color=blue]
      > Yes, you can do it using reflect package.
      >
      > import java.lang.refle ct.*;
      >
      > Assuming class Triangle is defined elsewhere in a package
      > com.youpackagen ame.Triangle you can do the following:
      >
      > I would defined a hashtable H where you have keys like this:
      > ("Triangle","co m.yourpackagena me.Triangle")
      > ("Square","com. yourpackagename .Square")
      > ("Temp","com.yo urpackagename.T emp")
      >
      > etc.
      >
      > This will load com.yourpackage name.Temp class:
      >
      > Class myClass = Class.forName(H .get("Temp")); ( instead of "Temp"
      > send in your variable)
      >
      > Here is how to get an object of that class:
      >
      > Object myObject = myClass.newInst ance();
      >
      > Have fun.
      >
      > Nadia
      >
      >
      >
      > "Tripharn" <telwyn@bigpond .net.au> wrote in message news:<qhv9b.104 827$bo1.6670@ne ws-server.bigpond. net.au>...[color=green]
      > > Hi guys,
      > >
      > > If I want to load an object, or a method from an object... but I don't know
      > > what it is can I use the value in the string as the method to run???
      > >
      > > For example, I get a string from a user that is 'triangle' stored in temp.
      > > Now I know that there is a class called triangle can I do something like
      > >
      > > temp variable = new temp();
      > >
      > > Where temp is obviously the string value and not actually the object temp
      > > itself.
      > >
      > > Ignore all the security problems here, because it is not going to be used in
      > > any prog... but I need to be able to do it this way for a testing driver.
      > >
      > > Thanks heaps,
      > > Tripharn[/color][/color]

      Comment

      • Anthony Martin

        #4
        Re: Using strings to run methods.

        You're probably looking for information about Java Reflection. There's a
        great book called "Java Pitfalls" (
        http://blat.info/articles/developers...8-001.html#001 ) that
        explains when and when not to use this as a solution to a problem.

        Here is a quick code example:

        Import java.lang.refle ct.* ;

        ..
        ..
        ..

        try {
        String className = "x.y.z.MyCl ass" ;
        String methodName = "show" ;
        Integer i = new Integer("10") ; // method argument
        Class c = Class.forName(c lassName) ;
        Object o = c.newInstance() ;
        Method m = c.getMethod(met hodName,
        new Class[] {i.TYPE, String.class}) ;
        // The above TYPE represents the primitave int, not the object wrapper.
        String output = m.invoke(o, new Object[] {i}) ;
        // The above line executes the method by the name stored in methodName.
        } catch ( Exception e ) {
        e.printStackTra ce() ;
        }

        ..
        ..
        ..


        Anthony


        On 9/15/03 8:17 PM, in article
        qhv9b.104827$bo 1.6670@news-server.bigpond. net.au, "Tripharn"
        <telwyn@bigpond .net.au> wrote:
        [color=blue]
        > Hi guys,
        >
        > If I want to load an object, or a method from an object... but I don't know
        > what it is can I use the value in the string as the method to run???
        >
        > For example, I get a string from a user that is 'triangle' stored in temp.
        > Now I know that there is a class called triangle can I do something like
        >
        > temp variable = new temp();
        >
        > Where temp is obviously the string value and not actually the object temp
        > itself.
        >
        > Ignore all the security problems here, because it is not going to be used in
        > any prog... but I need to be able to do it this way for a testing driver.
        >
        > Thanks heaps,
        > Tripharn
        >
        >[/color]

        Comment

        Working...