Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to dynamically edit a method

Reply
Thread Tools

How to dynamically edit a method

 
 
jan.rebada@gmail.com
Guest
Posts: n/a
 
      06-27-2007
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();
}
}

 
Reply With Quote
 
 
 
 
Ingo R. Homann
Guest
Posts: n/a
 
      06-27-2007
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

 
Reply With Quote
 
 
 
 
Tom Hawtin
Guest
Posts: n/a
 
      06-27-2007
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
 
Reply With Quote
 
printdude1968@gmail.com
Guest
Posts: n/a
 
      06-27-2007
On Jun 27, 11:26 am, Tom Hawtin <use...@tackline.plus.com> wrote:
> jan.reb...@gmail.com 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"?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Snapshot restraint - edit, edit, edit Alan Browne Digital Photography 24 05-10-2005 10:15 PM
Snapshot restraint - edit, edit, edit Patrick Digital Photography 0 05-06-2005 10:53 PM
Edit All Function for DataGrid, and Moving the Edit Function in a DataGrid Schultz ASP .Net 3 02-14-2005 04:47 AM
copying value of DDL in a Datagrid "pre-edit command" to value in "post edit command" San Diego Guy ASP .Net 0 08-07-2003 08:59 PM



Advertisments