• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Reference to java.lang.reflect.Method

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!
I have some sort of AOP adviser class (a cross-cutting concern). So it is thought to transparently get calls from many different objects.
Now, I don't know weather it is possible or not, but once this object get's the call, I would like to create or to have an instance of the calling class (the object), and a reference of the calling method (java.lang.reflect.Method).

I dream of something like:



Is that humanely possible?
Best regards and thanks in advance
XM
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Possible, but not 100% reliable. Thread.currentThread().getStackTrace[0] is a java.lang.StackTraceElement object which can tell you the class name, method name, source file, and source line for the currently executing method. For non-overloaded methods, that's enough. For overloaded methods, you could use the source file/line number and something like BCEL to examine the line number tables in the class file to figure out which overload is being invoked.

Of course, sometimes source file/line number isn't available. And some methods may be "optimized away" so that they don't show a stack frame at all!

So, the bottom line is you can do it, it's not terribly hard, but it won't always work.
 
Marx Villegas
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! what I need is just a referential thing, so probably I don't need to care about the overloads.
Thanks a lot Ernest
XM
 
She'll be back. I'm just gonna wait here. With this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic