• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to know a variable is accessed?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have two Java Classes as follows :


and --


Now i want to generate a log file (when the execution of main() method of class B ends) which tells me that "THE VARIABLE NAMED 'a' OF CLASS 'A' IS ACCESSED ONCE" .

PLEASE NOTE THAT I DON'T HAVE PERMISSION TO MODIFY THE SOURCE CODE OF EXISTING CLASS ..

So please tell me how to proceed to get the solution ..

Thanks.
Ganesh
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You *might* be able to use AOP or a byte-code manipulation framework to do something, but as it's a public member and not a method call I'm not so sure.
 
Ganesh Choudhary
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what i am doing in the case of private variable . i am instrumenting the Getters and Setters of the corresponding variable in that case . but i don't know what to do in the case of a public variable.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like David said, AOP (aspect oriented programming) may help you out (but I'm not sure). Check out AspectJ.
 
Ganesh Choudhary
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any idea where to put aspect ??? i mean to say that on which method of which class i will have to put the aspect ??? actually i have tried to debug the above code but on pressing F5 it doesn't go to any internal method call so i am not getting on which method i should use the aspect ...
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would have been better design to give the variable private access. Then the get method could have counted any access from outside the class.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There *is* no method to put the aspect on, which is why I was saying you might need to resort to byte-code augmentation. I'm not overly-familiar with raw AspectJ pointcuts, but off the top of my head I don't know of a way to do it at the variable access level.

One thing to consider would be extending the class, or making a composite, and wrap up access to the variable that way, and make it possible to use less-intrusive methods. If you're not in control of the class creation, that won't work.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the AspectJ pointcut reference - get/set(FieldPattern) seems what you are looking for.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah; good to know (although I never use AspectJ directly really).
 
Ganesh Choudhary
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Michalik wrote:Have a look at the AspectJ pointcut reference - get/set(FieldPattern) seems what you are looking for.



Thank You very much Man !!! it's working fine now . I have used get and set pointcut for my purpose ...
 
reply
    Bookmark Topic Watch Topic
  • New Topic