Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Getting Information on a Calling Class

Thread view: 
Hal Vaughan - 05 Jun 2007 06:33 GMT
I have a case where I'd like to return different data depending on which
class is calling the current class.  Is there any way to get the instance
of the object that called a method?

Thanks!

Hal
Stefan Ram - 05 Jun 2007 07:07 GMT
>I have a case where I'd like to return different data depending
>on which class is calling the current class.

 A class can not invoke or be invoked.

>Is there any way to get the instance of the object that called
>a method?

 In the first sentence you asked about classes calling classes
 and now you ask about objects calling methods.

 There does not have to be »a calling object«, as a method
 usually can be invoked from within the static main method.

 You might get the name or class of the caller from
 »java.lang.Thread.currentThread().getStackTrace()[ 2 ]«,
 but I am not aware of a means to get the object.
Lew - 05 Jun 2007 13:01 GMT
Hal Vaughan <hal@thresholddigital.com> writes:
>> I have a case where I'd like to return different data depending
>> on which class is calling the current class.
>> Is there any way to get the instance of the object that called
>> a method?

interface Caller
{
}
class Callee
{
 public void callMe( Caller caller )
 {
   doSomethingWith( caller );
 }
}
class ACaller implements Caller
{
 public void someMethod()
 {
   Callee callee = new Callee();
   callee.callMe( this );
 }
}

Signature

Lew

Hal Vaughan - 05 Jun 2007 17:05 GMT
> Hal Vaughan <hal@thresholddigital.com> writes:
>>> I have a case where I'd like to return different data depending
[quoted text clipped - 20 lines]
>   }
> }

I was thinking about just including itself as an argument and still might,
but was hoping for another way to handle it.  It's a long story, but it has
to do with wanting to stick with a standard already set.

Hal
Tris Orendorff - 06 Jun 2007 22:32 GMT
> I have a case where I'd like to return different data depending on which
> class is calling the current class.  

Why on Earth would you need to do this?  Can you give an example?

Signature

Tris Orendorff
[Q: What kind of modem did Jimi Hendrix use?
A: A purple Hayes.]

Hal Vaughan - 07 Jun 2007 02:04 GMT
>> I have a case where I'd like to return different data depending on
> which
>> class is calling the current class.
>
> Why on Earth would you need to do this?  Can you give an example?

I have a setting editor that edits data in tables depending on what is
selected in components.  This app will have a number of tab panels and each
tab will handle a different set of data.  I know I'll be adding tabs along
the way and I won't have time to recode each one.  I've written a number of
wrapper classes to work with all the Swing components.  I have one wrapper
for JComboBox that will have combined fields.  When a component wrapper
class calls to ask for its value, if it could check the calling component
and compare it to the list of registered components, then it could send
back the answer that registered component would use.

In other words, I might have three registered components and each one would
receive a different value when calling the same method.  Unregistered
components would always get the same value.

Hal
Christian - 07 Jun 2007 11:13 GMT
Tris Orendorff schrieb:

>> I have a case where I'd like to return different data depending on
> which
>> class is calling the current class.  
>
> Why on Earth would you need to do this?  Can you give an example?

I can also give some nice example..

I use this for creating my log4j loggers: (idea came from Hein Kabutz)

private static final Logger logger = LoggerFactory.make();

and behind this make method is:

public static  Logger make(){
   Throwable t = new Throwable();
   StackTraceElement directCaller = t.getStackTrace()[1];
   Logger l = null;
   try {
       l =   Logger.getLogger(Class.forName(directCaller.getClassName()));

   } catch(ClassNotFoundException cnfe ) {
       l =  Logger.getLogger(directCaller.getClassName());
   }
       
   return l;
}
Lew - 07 Jun 2007 13:55 GMT
> I use this for creating my log4j loggers: (idea came from Hein Kabutz)
>
[quoted text clipped - 15 lines]
>     return l;
> }

How does this give a different result from
 Logger logger = Logger.getLogger( getClass() )
?

Signature

Lew

Christian - 07 Jun 2007 20:03 GMT
Lew schrieb:
>> I use this for creating my log4j loggers: (idea came from Hein Kabutz)
>>
[quoted text clipped - 20 lines]
>  Logger logger = Logger.getLogger( getClass() )
> ?

you need an object for your  getClass() ..

my logger is set in a static context.. so getClass() doesn't work..

Christian
Lew - 07 Jun 2007 20:37 GMT
> Lew schrieb:
>>> I use this for creating my log4j loggers: (idea came from Hein Kabutz)
[quoted text clipped - 24 lines]
>
> my logger is set in a static context.. so getClass() doesn't work..

OK, then, Logger logger = Logger.getLogger( Foo.class );

Signature

Lew

Christian - 07 Jun 2007 20:38 GMT
Lew schrieb:
>> Lew schrieb:
>>>> I use this for creating my log4j loggers: (idea came from Hein Kabutz)
[quoted text clipped - 25 lines]
>
> OK, then, Logger logger = Logger.getLogger( Foo.class );

Would be possible... but my solution can be copy pasted..
.. is the same for every class..

thats the simple and onlyth advantage..
Lew - 08 Jun 2007 01:29 GMT
Lew schrieb:
>> OK, then, Logger logger = Logger.getLogger( Foo.class );

> Would be possible... but my solution can be copy pasted..
> .. is the same for every class..
>
> thats the simple and onlyth advantage..

I've never understand why we can't use "class" by itself in a static context.

Signature

Lew

Owen Jacobson - 08 Jun 2007 06:32 GMT
> Lew schrieb:
>
[quoted text clipped - 5 lines]
>
> I've never understand why we can't use "class" by itself in a static context.

That's one of the many little warts Java has.  While Foo.class looks
like a static member expression, it's actually its own type of
expression -- a class literal, though the term is not used in the
JLS.  The resemblance to static member access is convenient, but not
carried through completely.
Tor Iver Wilhelmsen - 18 Jun 2007 21:45 GMT
På Fri, 08 Jun 2007 07:32:53 +0200, skrev Owen Jacobson  
<angrybaldguy@gmail.com>:

> That's one of the many little warts Java has.  While Foo.class looks
> like a static member expression, it's actually its own type of
> expression -- a class literal, though the term is not used in the
> JLS.  The resemblance to static member access is convenient, but not
> carried through completely.

I seem to recall it's synthesized by the compiler into the method

Class class$() { return Class.forName("package.Foo"); }

which makes sense in a way.
Joshua Cranmer - 19 Jun 2007 01:35 GMT
> På Fri, 08 Jun 2007 07:32:53 +0200, skrev Owen Jacobson
> <angrybaldguy@gmail.com>:
[quoted text clipped - 10 lines]
>
> which makes sense in a way.

Actually, starting from Java 1.5, the VM code says that the
CONSTANT_Class_Info can be used as a valid argument for a ldc, so I would
assume that Foo.class would not be translated into an instance variable.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.