Java - How to find the class an Object is part of
Is there any way in Java to find the class an object is part of?
class WrapperClass
{
SomeObject o;
...
someMethodOfWrapperClass()
{ ... }
}
[...]
SomeObject foo = new SomeObject();
WrapperClass wrap = new WrapperClass();
wrap.o = foo;
Is there any way to find the corresponding WrapperClass object wrap
from within the SomeObject obje开发者_如何学Pythonct or one of it's methods?
foo.getWrapperClassObject().someMethodOfWrapperClass();
Thanks you very much in advance!!
Thomas
The short answer is "no".
You could do as user robev has suggested and pass a reference to the wrapper class into SomeObject
, but that creates a circular relationship between the two classes, WrapperClass
is no longer really a wrapper class.
It begs the question of why you are trying to do this.
In most cases when we create a wrapper class, we are wrapping code which we have no control over, so you wouldn't normally even be able to modify SomeObject's constructor.
Have you thought of trying inheritance instead? Maybe your WrapperClass
should extend SomeObject
, adding the someMethodOfWrapperClass()
method as additional behavior.
Maybe something like this:
public class ExtendingClass extend SomeObject
{
public void someMethodOfExtendingClass()
{ ... }
}
// so you can define an instance 'foo' which is ALSO an instance of SomeObject
ExtendingClass foo = new ExtendingClass();
// now you can call...
foo.someMethodOfExtendingClass();
You could have a parent child relationship where SomeObject is told who its parent is, so when you do getWrapperClassObject() you'll just return the stored parent pointer.
Example constructor:
SomeObject(WrapperClass parent) { this.parent = parent; }
In SomeObject: WrapperClass.this.someMethodOfWrapperClass();
I cannot really think of a case where it makes sense to do what you are trying to do... but you might find an inner class useful in doing it...
class Outer
{
Inner inner = new Inner();
class Inner
{
void foo()
{
bar();
}
}
void bar()
{
System.out.println("bar");
}
public static void main(String[] argv)
{
Outer o;
o = new Outer();
o.inner.foo();
}
}
The answer by robev is essentially the same, but robev has the more general purpose answer (Inner has a reference to Outer in the code I posted, it is just hidden and created by the compiler).
As others said, this is pretty suspicious code.
There are several ways to achieve it.
- One would be to add a reference to the wrapped
SomeObject
but that leads to circular dependency like Justin pointed out. - Second, would be to simply make
SomeObjectWrapper extends SomeObject
but do you even have control over that part of code? Last would be is to set up a convention and use reflection to get the code (the one I used is WrappedObject.name+"Wrapper"):
package test; public class ObjectWrapper { Object o; public ObjectWrapper(Object o){ this.o = o; } } Object o = new Object(); String pack = "test"; String name = package+o.getClass().getSimpleName()+"Wrapper"; Class objWrapper = Class.forName(name); objWrapper.getConstructor(Object.class).newInstance(o);
But this is far too unwieldy to be of any use. For example if you make Object o = new Integer
the whole thing will burst from exceptions.
The question doesn't make sense. Objects aren't 'part of' classes. References are part of classes, but what they point to isn't. Multiple references can point to the same object. So you can't even get a unique reference for any particular object, let alone what class the reference is in.
精彩评论