get parent class name

B

bcr666

I need to be able to determine the class name of the application that
instantiated my class. I've looked at the classloader, and the
AppClassLoader has the classes that it has loaded, but they are
private, and I can't get to them.
 
L

Lew

bcr666 said:
I need to be able to determine the class name of the application that
instantiated my class. I've looked at the classloader, and the
AppClassLoader has the classes that it has loaded, but they are
private, and I can't get to them.

I'm slightly confused by your use of "parent class" in the subject
line, since the question you ask is not related to parent classes.
I'll do my best to answer the question you actually are asking, but if
I guessed wrong about what that is please forgive me.

If the class you want to examine directly instantiated the object of
interest, I think you can use

new Throwable().getStackTrace() [1].getClassName()

(omitting tests for null pointers and the like).

I haven't tested this so double-check things like whether 1 is the
correct index.
 
A

Arne Vajhøj

I need to be able to determine the class name of the application that
instantiated my class. I've looked at the classloader, and the
AppClassLoader has the classes that it has loaded, but they are
private, and I can't get to them.

A parent class is the class that the class you have is
extending.

That is easy to find.

What you are looking for is the calling class.

That is a bit more tricky.

One way is:

StackTraceElement ste = Thread.currentThread().getStackTrace()[levels];
String clznam = ste.getClassName();
String mthnam = ste.getMethodName();

Arne
 
R

Roedy Green

I need to be able to determine the class name of the application that
instantiated my class. I've looked at the classloader, and the
AppClassLoader has the classes that it has loaded, but they are
private, and I can't get to them.

Parent usually means the class from which a class was derived, not the
class of the method that called new.

The most straight forward way would be to add a parameter to your
constructor. You could also add some StackTrace code to your
constructor. see http://mindprod.com/jgloss/trace.html for sample
code.

You might also look into profilers to see if one can do that for you.
http://mindprod.com/jgloss/profiler.html

--
Roedy Green Canadian Mind Products
http://mindprod.com

Microsoft has a new version out, Windows XP, which according to everybody is the "most reliable Windows ever." To me, this is like saying that asparagus is "the most articulate vegetable ever."
~ Dave Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top