This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Is there are any way to identify the place from where the particulate method is calling in runtime?  RSS feed

 
Ranch Hand
Posts: 48
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Is there are any way to identify the place from where the particulate method is calling in runtime?

 
Marshal
Posts: 67163
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why? If the method needs information to make decisions, such info should be passed explicitly as a parameter; not implicitly based upon where it was called from.
 
Varun Selva
Ranch Hand
Posts: 48
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Why? If the method needs information to make decisions, such info should be passed explicitly as a parameter; not implicitly based upon where it was called from.




I am asking this for inquiry purpose.
because I have to find from existing flow in a complex Project.

I can make sure that,that method is called by somewhere. But couldn't find the place.
 
Rancher
Posts: 42974
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is possible, and I'll disagree with Bear that it's always feasible to pass the necessary information to the method.

At the spot where you need this information, start with StackTraceElement[] stackTrace = (new Exception()).getStackTrace() and examine what's in that array.
 
Sheriff
Posts: 12747
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple of other options for this kind of investigation. You could use an IDE like Eclipse to find all the references to that method. This method assumes that you have the source code and you are only interested in calls made to the method from within the immediate project or workspace codebase. If there is calling code for which you don't have the source, you could set a breakpoint inside that method and run the program in a debugger, then examine the call stack every time you hit the breakpoint.
 
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:At the spot where you need this information, start with StackTraceElement[] stackTrace = (new Exception()).getStackTrace() and examine what's in that array.


Or, to avoid having to create an Exception object
Thread.currentThread().getStackTrace()
 
Sheriff
Posts: 21465
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It still might. From the source of Java 6 through 8:
dumpThreads is native, but if getStackTrace() is called on the current thread it just creates a new exception.

However, I must agree that Thread.currentThread().getStackTrace() looks cleaner.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!