• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to know a variable is accessed?  RSS feed

 
Ganesh Choudhary
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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 ...
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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 ...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!