How to dynamically edit a method

Discussion in 'Java' started by jan.rebada@gmail.com, Jun 27, 2007.

  1. Guest

    I have the following code. The problem is it always display a null
    value. It will not successfully set the Name. THanks.

    import java.lang.reflect.Method;


    public class dynamicloader {

    private String name = "";


    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public void load() {
    Class c = this.getClass();
    Object newobj = null;
    try {
    newobj = c.newInstance();
    } catch (Exception e) {
    System.out.println(e);
    }

    try {
    Method m = c.getMethod("setName", new Class[]{String.class});
    String result = (String) m.invoke(newobj,new Object[]{"My name"});
    System.out.println("Result: " + result);
    } catch (Exception e) {
    System.out.println(e);
    }
    }

    public static void main(String[] args) {
    dynamicloader dl = new dynamicloader();
    dl.load();
    }
    }
     
    , Jun 27, 2007
    #1
    1. Advertising

  2. Hi,

    wrote:
    > public class dynamicloader {
    > ...
    > public void setName(String name) {
    > this.name = name;
    > }
    > ...
    > Method m = c.getMethod("setName", new Class[]{String.class});
    > String result = (String) m.invoke(newobj,new Object[]{"My name"});
    > System.out.println("Result: " + result);


    Take a look at your code again (I snipped everything except the
    important lines) and think about it.

    What do you expect?

    Hint: What is the return type of the method you are invoking?

    Ciao,
    Ingo
     
    Ingo R. Homann, Jun 27, 2007
    #2
    1. Advertising

  3. Tom Hawtin Guest

    wrote:
    > I have the following code. The problem is it always display a null
    > value. It will not successfully set the Name. THanks.


    > public void setName(String name) {


    > Method m = c.getMethod("setName", new Class[]{String.class});
    > String result = (String) m.invoke(newobj,new Object[]{"My name"});
    > System.out.println("Result: " + result);


    You have displayed the return value from setName. setName does not
    return a value.

    Reflection is confusing and error-prone. It's best avoided.

    Tom Hawtin
     
    Tom Hawtin, Jun 27, 2007
    #3
  4. Guest

    On Jun 27, 11:26 am, Tom Hawtin <> wrote:
    > wrote:
    > > I have the following code. The problem is it always display a null
    > > value. It will not successfully set the Name. THanks.
    > > public void setName(String name) {
    > > Method m = c.getMethod("setName", new Class[]{String.class});
    > > String result = (String) m.invoke(newobj,new Object[]{"My name"});
    > > System.out.println("Result: " + result);

    >
    > You have displayed the return value from setName. setName does not
    > return a value.
    >
    > Reflection is confusing and error-prone. It's best avoided.
    >
    > Tom Hawtin


    OMG... is this "self modifying code"?
     
    , Jun 27, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. San Diego Guy
    Replies:
    0
    Views:
    551
    San Diego Guy
    Aug 7, 2003
  2. Schultz
    Replies:
    3
    Views:
    568
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Feb 14, 2005
  3. =?Utf-8?B?a2Vu?=
    Replies:
    1
    Views:
    10,342
    Wiktor Zychla [C# MVP]
    Jan 23, 2006
  4. Sharon McCarty
    Replies:
    0
    Views:
    253
    Sharon McCarty
    Nov 24, 2004
  5. Frank Kurka
    Replies:
    8
    Views:
    312
Loading...

Share This Page