title: How do I determine the context in which a ColdFusion object resides?
body: <p>So, given that I have an instance of this component:</p>
  
  <p>foo.cfc</p>
  
  <pre><code>&lt;cfcomponent&gt;
    &lt;cffunction name="locateMe"&gt;
      &lt;cfreturn "I don't know where I live!"&gt;
    &lt;/cffunction&gt;
  &lt;/cfcomponent&gt;
  </code></pre>
  
  <p>And, this other component, fooParent.cfc:</p>
  
  <pre><code>&lt;cfcomponent&gt;
    &lt;cfset this.foo = createObject("component", "foo")&gt;
  &lt;/cfcomponent&gt;
  </code></pre>
  
  <p>Let's say I create instances of "foo" a few different ways:</p>
  
  <pre><code>&lt;cfset myStruct = {}&gt;
  &lt;cfset myStruct.foo = createObject("component", "foo")&gt;
  
  &lt;cfset myFoo = createObject("component", "foo")&gt;
  
  &lt;cfset myFooParent = createObject("component", "fooParent")&gt;
  
  &lt;cfoutput&gt;
  #myStruct.foo.locateMe()#&lt;br&gt;
  #myFoo.locateMe()#&lt;br&gt;
  #myFooParent.foo.locateMe()#&lt;br&gt;
  &lt;/cfoutput&gt;
  </code></pre>
  
  <p>As expected, this outputs:</p>
  
  <pre><code>I don't know where I live!
  I don't know where I live!
  I don't know where I live!
  </code></pre>
  
  <p>What I want to know is, what can I possibly do within foo.cfc that will tell me something (anything!) about the context in which it is being invoked? Since everything ultimately lives in (at least) a scope of some sort, and all scopes are a kind of object, what I'm saying is that <strong>I would really like some way to determine the containing object, from within a given instantiated object</strong>.  Ultimately, some way of building foo.cfc so that something like this could be my output, from my sample snippet above:</p>
  
  <pre><code>I live within a "class coldfusion.runtime.Struct" instance!
  I live within a "class coldfusion.runtime.VariableScope" instance!
  I live within a "component cfjunk.fooParent" instance!
  </code></pre>
  
  <p>Where each of those values might be determined by inspecting the result from passing <code>getMetaData</code> the actual containing object reference.</p>
  
  <p><em>Update</em> As suggested by Micah in the comments, I've added the "Java" tag to this, since I suspect he could be correct in that the solution may lie in using Java for introspection.</p>
  
  <p><em>Update</em></p>
  
  <p>Rather than leave this as what appears to be a purely-academic discussion, let me explain why I need this.</p>
  
  <p>I'm using CFWheels ORM with includes to get back references to my data like so:</p>
  
  <pre><code>var user = model("User").findOne(where="id=123", include="AuthSource", returnAs="object");
  </code></pre>
  
  <p>This will return to me an object that I can reference like so:</p>
  
  <pre><code>user.id // property of the "User" model
  user.reset() // method on the "User" model
  user.AuthSource.id // property of the "AuthSource" model
  user.AuthSource.authenticate(password) // method on the "AuthSource" model
  </code></pre>
  
  <p>Now, within my "AuthSource.authenticate" method, I would like to know about the "User" object that I'm contained within.  Otherwise, I will end up having to call the function like this, instead:</p>
  
  <pre><code>user.AuthSource.authenticate(user, password) // note the redundancy in the use of "user"
  </code></pre>
  
  <p>I should be able to rely on the fact that I'm calling the method on the AuthSource model through the User object and actually read from that object from within that method.</p>