This discussion is archived
1 2 Previous Next 17 Replies Latest reply: Mar 18, 2010 10:03 AM by jtahlborn RSS

get object instance that another object instance is a member of

843798 Newbie
Currently Being Moderated
I realize that I can find all the fields of a declared class using reflection, but I'd like to do it reversely on an object.
Say I have class A and B.
A contains an instance of B:

public class A {
private B b;
}

public class B {
...
}

I would like to know what Object or class B is a member of but I cannot find any way to do this.
For instance, inside the B class, Id like to have some logic that does something if B is a member of the A class.
Like I said, using A I can find out if B is a member, but not vice versa.

Any ideas?
  • 1. Re: get object instance that another object instance is a member of
    DrClap Expert
    Currently Being Moderated
    No, there's no way to find all the objects which contain a reference to B.

    So whatever problem that idea was supposed to solve, you'll have to find a different solution for it.
  • 2. Re: get object instance that another object instance is a member of
    843798 Newbie
    Currently Being Moderated
    thanx - thats what I figured
  • 3. Re: get object instance that another object instance is a member of
    791266 Explorer
    Currently Being Moderated
    blawrence wrote:
    thanx - thats what I figured
    You might also be able to use aspect oriented programming to solve the problem. You are however most likely having an odd and bad design if you must know if a certain instance is aggregated in another instance.
  • 4. Re: get object instance that another object instance is a member of
    843798 Newbie
    Currently Being Moderated
    Well - your opinion of "odd or bad" is subjective without knowing anything about what I'm trying to do, so let me explain.
    I have class A that gets loaded in a web application's classloader.
    A contains an instance of B, but B is loaded in a separate classloader.
    I want to get access to the classloader of A (the web application classloader), from B, without knowing anything about A or any other classes in the web application.
    Is this still "odd or bad"?
    If so, can you provide some input to help?
  • 5. Re: get object instance that another object instance is a member of
    791266 Explorer
    Currently Being Moderated
    blawrence wrote:
    Well - your opinion of "odd or bad" is subjective without knowing anything about what I'm trying to do, so let me explain.
    I have class A that gets loaded in a web application's classloader.
    A contains an instance of B, but B is loaded in a separate classloader.
    I want to get access to the classloader of A (the web application classloader), from B, without knowing anything about A or any other classes in the web application.
    Why didn't you say that you wanted to get hold of the classloader in the original question? That's completely different from getting hold of an instance "that you are aggregated in".
    Is this still "odd or bad"?
    It still sounds odd, and possibly wrong, but it depends on why you need to get hold of that classloader.
    If so, can you provide some input to help?
    Yes, you should read a tutorial on classloaders and how they work. Reflection isn't what you need to use.

    Kaj
  • 6. Re: get object instance that another object instance is a member of
    843798 Newbie
    Currently Being Moderated
    Why didn't you say that you wanted to get hold of the classloader in the original question? That's completely different from getting hold of an instance "that you are aggregated in".
    Not really. If I can get a reference to the object that loaded another object, I can get its classloader.
    It still sounds odd, and possibly wrong, but it depends on why you need to get hold of that classloader.
    I need to get a hold of that other classloader to load up resources - specifically a properties file.
    Yes, you should read a tutorial on classloaders and how they work. Reflection isn't what you need to use.
    I'm not using relection and I'm aware of how classloaders work.
  • 7. Re: get object instance that another object instance is a member of
    791266 Explorer
    Currently Being Moderated
    blawrence wrote:
    Why didn't you say that you wanted to get hold of the classloader in the original question? That's completely different from getting hold of an instance "that you are aggregated in".
    Not really. If I can get a reference to the object that loaded another object, I can get its classloader.
    Some information is still missing. What class loader is loading A, and what class loader is loading class B. Why are they loaded by different class loaders?

    Why does class B need to load something using the class loader of A without knowing about A?

    >
    It still sounds odd, and possibly wrong, but it depends on why you need to get hold of that classloader.
    I need to get a hold of that other classloader to load up resources - specifically a properties file.
    This still sounds odd especially given that you said this in the original question
    For instance, inside the B class, Id like to have some logic that does something if B is a member of the A class.
    >
    Yes, you should read a tutorial on classloaders and how they work. Reflection isn't what you need to use.
    I'm not using relection
    But you posted your question in the reflections forum.
  • 8. Re: get object instance that another object instance is a member of
    DrClap Expert
    Currently Being Moderated
    blawrence wrote:
    I need to get a hold of that other classloader to load up resources - specifically a properties file.
    Then the most straightforward way would be to have A get that properties file and pass it to B, as a constructor parameter or something. An object doesn't have any way of accessing the object which created it.

    Unless it happens that A's classloader is actually the parent of B's classloader. In which case it's simple to get a reference to A's classloader from B.
  • 9. Re: get object instance that another object instance is a member of
    843798 Newbie
    Currently Being Moderated
    Some information is still missing. What class loader is loading A, and what class loader is loading class B. Why are they loaded by different class loaders?

    Why does class B need to load something using the class loader of A without knowing about A?
    The classloader that loads A is a web application classloader and the classloader for B is a separate classloader used by the application server to load classes related to the app server domain - so its not the system classloader. BTW - this is Weblogic I'm using. The system classloader loads up a bunch of Weblogic specific classes and then another classloader loads up all the classes defined in JAR files in the domain's lib directory - these are classes shared by all applications running in that domain, but again - its not the system classloader. My team drops JARs into the domain lib folder that need to be shared by multiple applications running within that domain.
    This still sounds odd especially given that you said this in the original question
    For instance, inside the B class, Id like to have some logic that does something if B is a member of the A class.
    This is probably my fault for not getting to the point of my issue but rather asking another question that was in a round-about effort to solve my original question.
    But you posted your question in the reflections forum.
    Well - the "Relections and Reference Objects" forum. I figured a classloader was a 'reference object'. Plus none of the other Core APIs forums sounded like my question fit.
  • 10. Re: get object instance that another object instance is a member of
    843798 Newbie
    Currently Being Moderated
    Then the most straightforward way would be to have A get that properties file and pass it to B, as a constructor parameter or something. An object doesn't have any way of accessing the object which created it.
    Thanx for your input, however I'd like to not have to code up all the code to pass the properties file to B in each specific class that includes a reference to B.
    Unless it happens that A's classloader is actually the parent of B's classloader. In which case it's simple to get a reference to A's classloader from B.
    Unfortunately it isn't - I think its the other way around. B's classloader is the parent of A's classloader (I think) so from what I can tell, B's classloader cannot find classes defined by any of its 'child' classloaders.

    Edited by: blawrence on Mar 17, 2010 2:45 PM
  • 11. Re: get object instance that another object instance is a member of
    843798 Newbie
    Currently Being Moderated
    Just to add more info to this...

    I've partially gotten around this by passing a Class reference to B so that I can get the classloader via that class reference, but sometimes A doesn't pass a class reference to B and therefore I cannot get its classloader.
  • 12. Re: get object instance that another object instance is a member of
    Kayaman Guru
    Currently Being Moderated
    blawrence wrote:
    Well - the "Relections and Reference Objects" forum. I figured a classloader was a 'reference object'.
    Just a quick, non-helpful comment on this. Reference objects are WeakReference, SoftReference and PhantomReference, so classloaders don't fit into that.
  • 13. Re: get object instance that another object instance is a member of
    791266 Explorer
    Currently Being Moderated
    blawrence wrote:
    Just to add more info to this...

    I've partially gotten around this by passing a Class reference to B so that I can get the classloader via that class reference, but sometimes A doesn't pass a class reference to B and therefore I cannot get its classloader.
    What will you do if more than one class loader is loading A?
  • 14. Re: get object instance that another object instance is a member of
    843798 Newbie
    Currently Being Moderated
    Just a quick, non-helpful comment on this. Reference objects are WeakReference, SoftReference and PhantomReference, so classloaders don't fit into that.
    Well - what forum should I have posted in then?
1 2 Previous Next