Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

How to dynamically edit a method

Thread view: 
jan.rebada@gmail.com - 27 Jun 2007 15:13 GMT
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();
    }
}
Ingo R. Homann - 27 Jun 2007 15:20 GMT
Hi,

> public class dynamicloader {
>  ...
[quoted text clipped - 5 lines]
>             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
Tom Hawtin - 27 Jun 2007 15:26 GMT
> 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
printdude1968@gmail.com - 27 Jun 2007 16:27 GMT
> jan.reb...@gmail.com wrote:
> > I have the following code. The problem is it always display a null
[quoted text clipped - 10 lines]
>
> Tom Hawtin

OMG... is this "self modifying code"?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.