Results 1 to 9 of 9
- 04-19-2011, 06:44 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
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,
JohnLast edited by mayngontay; 04-19-2011 at 07:02 AM. Reason: mispelled
- 04-19-2011, 12:11 PM #2
You can't obtain a reference unless you pass a reference. So change setMessage() to setMessage(Engine object)
db
- 04-19-2011, 12:38 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
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.
- 04-19-2011, 01:53 PM #4
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-20-2011, 02:11 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
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); } }
- 04-20-2011, 04:00 AM #6
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
- 04-20-2011, 04:28 AM #7
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
There is no 'give me a reference to the object that called me' mechanism in Java. Change your design, or use a different language.
- 04-20-2011, 04:32 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
- 04-20-2011, 05:02 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
You are right that we can change the signature of
toJava Code:Foo.setMessage()
and call it byJava Code:setMessage(Engine callerEngine, String msg)
. However, the script textJava Code:firstEngine.eval("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).Java Code:"Packages.Foo.setMessage(firstEngine, 'Hello!');"
Anyway, I have changed my application design. Thank you very much.
Similar Threads
-
object and reference
By aizen92 in forum New To JavaReplies: 11Last Post: 04-01-2011, 08:39 PM -
Knowing what object called the actionListener?
By enerj in forum New To JavaReplies: 4Last Post: 11-02-2010, 09:30 AM -
Method, returning reference to an object
By Saletra in forum New To JavaReplies: 3Last Post: 08-23-2010, 08:22 PM -
keyListener how to modify object which called it?
By alacn in forum New To JavaReplies: 2Last Post: 08-12-2010, 07:30 AM -
Object and reference
By katie in forum New To JavaReplies: 2Last Post: 10-19-2009, 03:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks