There is nothing in the standard API that allows you to get this information. It's a bit unclear what you mean with 'declaring class' or 'owner class' but for this sake of this answer I'll assume that it is the class whose code created the instance of the object (that you want the owner class from).
This information is not stored by the JVM by default.
But, using the heap profiler that is packaged along with the JDK distribution, you can record the stacktrace of the point at which objects are allocated, and this information can be written to a file on various points in time.
That still doesn't give you an API call to retrieve the information, but it shows that it is technically possible to record this type of information.
I search a bit on Google and found that someone did create an API that uses the same basic technique as the heap profiler (the java.lang.instrumentation
package/JVMTI interface)
- Open source project: java-allocation-instrumenter
With a bit of work you should be able to build something with it.
The site has a nice example:
AllocationRecorder.addSampler(new Sampler() {
public void sampleAllocation(int count, String desc, Object newObj, long size) {
System.out.println("I just allocated the object " + newObj +
" of type " + desc + " whose size is " + size);
if (count != -1) { System.out.println("It's an array of size " + count); }
}
});
Instead of printing, you should get the stacktrace using new Exception().getStackTrace()
, remove the first few StackTraceElement
objects that refer to the sampler and the API classes, and then call StackTraceElement.getClassName()
to get the name of the class that created the object instance, in other words, your OwnerClass.