aspose file tools*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""I Watch ""I New topic
Author

"I'm super(), you're (blank)?"

David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
3 classes- say ClassOne, ClassTwo, & ClassThree, extend SuperClass() and inherit the following method:
/* Returns the fully qualified name of the current class that called this method */
public String myNameIs()
{ ... }
So say I'm in ClassThree's main() and I say:
void main(...)
{
System.out.println(myNameIs());
}
I want it to say "ClassThree" and if I'm in ClassOne I want it to say "ClassOne". I do not want it to say "SuperClass" for any case. The purpose for this is not demonstrated in the example, because cleary I wouldn't have to do something stupid like that to get the name of the class I'm currently in.
From within a super class, can I determine the name of the child class that invoked one of the super class' methods?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Yup. Hopefully this is robust enough. I haven't fully tested it out, but it seems to work.If you really wanted "just" the classname (and not the fully qualified classname) you can use a few String methods to snip off everything in front of the last period character, and return just the last bit.
David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
Hot dang! Thanks Mike!

outputs:
Naming.OneChild
Naming.SuperClass
MyNameIs() returns 2 different things depending on if SuperClass is extended or not, which seems ambiguous to me. Is there a way that I can explicitly say
-"Gimme the name of the class that extends me, null if there is none."
-"Gimme the name of my class, regardless if I'm extended or not"
?
[ March 22, 2002: Message edited by: David Duran ]
Adam Blinkinsop
Greenhorn

Joined: Mar 22, 2002
Posts: 7
Unfortunately, there is no way to find subclasses of a class with just reflection (using the getClass() methods and stuff).
One way that I have found to do this (if it's only your own classes that you want to find subclasses of) is to statically store the names of subclasses in their superclass. (complicated it sounds, no?)
Ok - In the super class, make a static variable that is a Vector or array of class names, or java.lang.reflect.Class objects. In each subclass, call that method in a static initializer, adding it as a subclass.
Ex:
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

David,
You said:

MyNameIs() returns 2 different things depending on if SuperClass is extended or not, which seems ambiguous to me.


It's not ambigous at all, since we're not querying an instance of SuperClass when we say one.MyNameIs(); We are asking 'one' who it is, and it properly responds "i am one".

It doesn't return two different things depending on whether or not we've extended the SuperClass. It's because we've asked two different instances who they are.

If your real requirement is actually what you ask here:
Is there a way that I can explicitly say
-"Gimme the name of the class that extends me, null if there is none."
Then I think using a mechanism like Adam's is the only way. "me" in your case is always the SuperClass, but the class has no idea WHO has extended it, unless we tell it to keep track of that. A child knows who its parent class is because of the keyword extends, but a parent class does not track its children. If this were built into the language, it would probably have to be a new keyword, something like sires or perhaps isParentOf
p.s.- Your second gimme:
"Gimme the name of my class, regardless if I'm extended or not"
is what the forName() method gives you. It gives the name of 'my class' ie: me, myself and I, what is my name, I don't care if I'm a bastard child or not.

p.p.s- Your code posted won't run. sc isn't declared before you try to 'out' it.
produces:Maybe *this* is what you meant? Here we see that a base class reference, that is holding a child class, can still properly resolve the eternal question: WhoAmI?
David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
Ah, thank you guys. Your feedback was most helpful. By ambiguous I meant that if I look at the code in SuperClass() I would naturally think that it would return the name of itself and not the name of another class if it was extended. Maybe that's just a bad assumption.
FYI, this is how I stripped the fully qualified class name to just the class name:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "I'm super(), you're (blank)?"