0

I need to be able identify the Object which is calling a specific method. So if for example I have the following

    JLabel l = new JLabel("Hello");
    JLabel label = new JLabel("HELLO");
    label.setText("BYE");

I need to be able to identify the object in variable label is actually calling setText and not l. I did manage to get the type of the object calling the method, in this case being JLabel, as I am using SOOT to be able to get method information. However what I actually need is the object making use of this method.

Is there a way to be able to identify the actual object calling the method?

rolve
  • 10,083
  • 4
  • 55
  • 75
ict1991
  • 2,060
  • 5
  • 26
  • 34
  • I need to get the GUI components calling certain methods and manipulate them... for example changing the color of the text in the label... but not for all method calls or objects, the methods I get from the SOOT output – ict1991 Mar 17 '12 at 17:05
  • The fundamental problem is that, objects do not call methods. The *method call* may be inside some object (which is probably what you meant), but there's no decent way to get that (except with some *very* ugly call stack dump investigation; could be useful for debugging but definitely not for production code). – Joonas Pulakka Mar 17 '12 at 17:13
  • @JoonasPulakka yes you are right, that is what I meant – ict1991 Mar 17 '12 at 17:20
  • 2
    This still sounds to me a design mistake that you're trying to solve with kludge code. Why not fix the design in the first place so you don't need to rely on fragile code? – Hovercraft Full Of Eels Mar 17 '12 at 17:52

2 Answers2

3

Yes, you could find who is calling via reflection. See this stack overflow post: How do I find the caller of a method using stacktrace or reflection? for more info on how to do that.

However, I would be concerned that you're trying to do something in a much harder way than necessary to achieve whatever end goal you are after. Could you pass "this" in as a parameter to the sub-methods that need to know who's calling?

Community
  • 1
  • 1
Jessica Brown
  • 8,222
  • 7
  • 46
  • 82
  • I am building a tool that needs to know information about another system which is given to be by someone else, so I cannot modify the system in any way – ict1991 Mar 17 '12 at 17:18
  • i'll try reflection and let you know how I managed... thanks for the info – ict1991 Mar 17 '12 at 17:23
  • Are you familiar with reflection in general? – Jessica Brown Mar 17 '12 at 17:25
  • 1
    @JessicaBrown +1 I would use the stack trace as a first option. The reflection method you refer to is in a internal package which might not be on all JVMs. – Peter Lawrey Mar 17 '12 at 18:12
  • @JessicaBrown I've never used refection... I think that the stacktrace will be a bit messy for what I need to do... I'll first try and use refection – ict1991 Mar 18 '12 at 12:59
  • @JessicaBrown I looked at Refection and I dont know whether I've missed something or not, but Refection only gives you the type of the caller right? Not the actual object which is created at runtime? – ict1991 Mar 19 '12 at 19:59
  • Yes, you may have missed something. Take a look at these articles: http://www.sevagas.com/?Modify-any-Java-class-field-using or http://java.sun.com/developer/technicalArticles/ALT/Reflection/ look at the section on changing values of fields or http://stackoverflow.com/questions/4426674/how-to-change-the-attribute-value-using-reflection If those don't give enough detail, it may be time to post a new more specific question here on Stackoverflow with the new problem you are trying to solve – Jessica Brown Mar 20 '12 at 19:57
0

There is no method that is appropriate. You should pass a parameter to your method to tell it what to do, no go sniffing up the stack.

bmargulies
  • 97,814
  • 39
  • 186
  • 310