473,394 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Get the "source" class

I want to get a hold of the "calling" class of a method / constructor.
An example would be as follows:

public class A() {
public A() {
new B();
}
}

public class B() {
public B() {
[stuff here]
}
}

If i replace [stuff here] with System.out.print(getClass().getName()),
i would get "B" printed. But i'm interested in getting a hold of A -
the instance isn't necessary... the name of the class will do just
fine. I suspect it can be done, since the stacktrace of exceptions
contain pretty much the information i want.

Anyone have a solution to this problem?

Regards,
Carsten H. Pedersen
Feb 26 '06 #1
1 4073
Carsten H. Pedersen wrote:
I want to get a hold of the "calling" class of a method / constructor.
An example would be as follows:

public class A() {
public A() {
new B();
}
}

public class B() {
public B() {
[stuff here]
}
}

If i replace [stuff here] with System.out.print(getClass().getName()), i
would get "B" printed. But i'm interested in getting a hold of A - the
instance isn't necessary... the name of the class will do just fine. I
suspect it can be done, since the stacktrace of exceptions contain
pretty much the information i want.

Anyone have a solution to this problem?


Alas, i was too quick in giving up. I found some pointers, and i've
found a solution to the problem. If anyone is interested, this is what
i put in to replace [stuff here]:

StackTraceElement[] ste = new Throwable().getStackTrace();
if (ste.length > 1) {
System.out.println("Class: "+ste[1].getClassName());
}
Feb 26 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Phil Powell | last post by:
I'm sorry but I can't figure out how to explain this any better than this. In PHP we have a command "require()" that obtains a file and logically places it into another file. I cannot figure...
4
by: hetnet | last post by:
Hello, How can i disabled a menuitem like " View => Source"??? -- Met vriendelijke groet, Rob van Westrop
2
by: Doc | last post by:
looking at this site: http://www.garygrantmusic.com/ and looking under source, the majority of what's there seems to be for the crawler at the bottom of the page, it seems to me that most of...
18
by: Lorem Ipsum | last post by:
interesting! I just found a page in which Explorer's View Source does nothing! How did they do that?
2
by: jerry.ranch | last post by:
I've been using row source with the QBE for my list and combo boxes..when would I use control source? jerry
2
by: Werner Sammer | last post by:
I tried to connect to an existing Oracle Database with the following ConnectionString: using System.Data.OracleClient; // + Added Reference "System.Data.OracleClient" string cs = "Data...
3
by: billelev | last post by:
I'm a relative newbie to java and have encountered a "source not found" error when trying to debug the following section of code (for the ParseException.class). public PaymentDates (String...
0
by: wildman | last post by:
Trying to read XML files from SSIS and load into SQL Server. I tested this before and it was working before I placed in a forevery contrainer. also, my simple xml file had to be retyped cause I...
1
by: nagaraj | last post by:
In Dev C++, we tried to compile a application, but it will throw some peculiar error. please find it below "path/main.cpp expected `]' before ';' token" and the source code is "DWORD rtec_Stack...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.