Results 1 to 9 of 9
  1. #1
    mayngontay is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default How to obtain a caller object reference from within a called method?

    I have to execute a given String as JavaScript code, e.g. eval('Foo.setMessage("Hello!")'), from within a class called Engine. Here, setMessage() is a public static method of the Foo class. Because I want to access some properties of the Engine object from within the setMessage() method, how can I obtain a reference to the MyApp object?

    I do know how to get the caller class name using Reflection or StackTrace or Throwable, but these do not return a caller object reference.

    Please help, thanks,
    John
    Last edited by mayngontay; 04-19-2011 at 07:02 AM. Reason: mispelled

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default

    You can't obtain a reference unless you pass a reference. So change setMessage() to setMessage(Engine object)

    db

  3. #3
    mayngontay is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    You can't obtain a reference unless you pass a reference. So change setMessage() to setMessage(Engine object)

    db
    Ok, assuming that I change setMessage(String msg) to setMessage(Engine object, String msg). How can I change the call to this method. The original call is eval('Foo.setMessage("Hello!")'). Note that, the call is made via the evaluation of a JavaScript string, it is not a direct call. Do you think it should be eval('Foo.setMessage(this, "Hello!")')? This will not work. Please carefully read my question. Thanks.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    Quote Originally Posted by mayngontay View Post
    Please carefully read my question. Thanks.
    Please ask your question more clearly. Thanks.

    Saying "this will not work" is as useful to us as us saying "then fix it" is useful to you.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    mayngontay is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Ok, thanks. Here is the code to make my question clearer.
    Java Code:
    import javax.script.*;
    
    public class Engine {
        public static ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("ECMAScript");
        public String engineName;
        public String message;
    
        public Engine(String name) {
            this.engineName = name;
        }
    
        public void eval(String script) {
            try {
                Engine.scriptEngine.eval(script);
                //to do something more
            } catch (ScriptException e) {
                System.out.println(e.toString());
            }
        }
    
        public static void main(String[] args) {
            Engine firstEngine = new Engine("First engine");
            firstEngine.eval("Packages.Foo.setMessage('Hello!');");
    
            Engine secondEngine = new Engine("Second engine");
            secondEngine.eval("Packages.Foo.setMessage('Hello!');");
        }
    }
    
    public class Foo {
        public static void setMessage(String msg){
            //here: how to get which engine is calling this method: the first engine or the second engine?
            //callerEngine.message = msg;
            //System.out.println(callerEngine.engineName);
        }
    }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default

    In the main method, you already have variable references for firstEngine and secondEngine. What prevents you from changing the signature of Foo.setMessage (or creating an overload of the method) and passing those references as parameters?

    db

  7. #7
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    There is no 'give me a reference to the object that called me' mechanism in Java. Change your design, or use a different language.

  8. #8
    mayngontay is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by toadaly View Post
    There is no 'give me a reference to the object that called me' mechanism in Java.
    If the above is true, then I would like to say thank you very much for poiting this out! I will finish looking for such the mechanism in Java.

  9. #9
    mayngontay is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    In the main method, you already have variable references for firstEngine and secondEngine. What prevents you from changing the signature of Foo.setMessage (or creating an overload of the method) and passing those references as parameters?
    db
    You are right that we can change the signature of
    Java Code:
    Foo.setMessage()
    to
    Java Code:
    setMessage(Engine callerEngine, String msg)
    and call it by
    Java Code:
    firstEngine.eval("Packages.Foo.setMessage(firstEngine, 'Hello!');");
    . However, the script text
    Java Code:
    "Packages.Foo.setMessage(firstEngine, 'Hello!');"
    is actually from a file. The one who created this file may not know the engine object reference which will execute this script (e.g., firstEngine or myEngine or whatever).

    Anyway, I have changed my application design. Thank you very much.

Similar Threads

  1. object and reference
    By aizen92 in forum New To Java
    Replies: 11
    Last Post: 04-01-2011, 08:39 PM
  2. Knowing what object called the actionListener?
    By enerj in forum New To Java
    Replies: 4
    Last Post: 11-02-2010, 09:30 AM
  3. Method, returning reference to an object
    By Saletra in forum New To Java
    Replies: 3
    Last Post: 08-23-2010, 08:22 PM
  4. Replies: 2
    Last Post: 08-12-2010, 07:30 AM
  5. Object and reference
    By katie in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 03:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •