0

How can we check value of variable defined and used in some method. One way is to print that value, but I want to do it programmatically without modifying original code(which actually is third party java library so I shouldn't)

Why not IDE debugger, Why I want it : I am trying to write a test program which can check and compare certain variables at runtime for 2 versions of API. Whenever new version of particular java API(in some cases I am writing that API) is released I would like to run this program to test recent API changes are not impacting more than desired variables. I can write junit test cases to test all input output combinations, but after checking variable values this test program will be able to tell where exactly we have problem in code.

Can java agent give me that information, if yes a sample code would be great. I tried to search java agent related articles but couldn't find specific for my use case. Most of the articles talk about how to write it at basic level.

Vipin
  • 4,851
  • 3
  • 35
  • 65
  • Why can't you try debugging? – vineeth Mar 14 '17 at 04:15
  • Java IDE's typically have an interactive debugging facility that allows you to single step line by line through your code as it is executing. The values of variables present in the current scope are displayed during this process, enabling you to very readily check their values. – scottb Mar 14 '17 at 04:18
  • I added one new section **why I want it**, that should answer your question. I realised it now I should have added that in question, will add now. – Vipin Mar 14 '17 at 04:19
  • 1
    *" I am trying to write a test program which can check and compare certain variables at runtime for 2 versions of API."* ... so ... unit tests? The Java Development Environment includes libraries for writing unit tests to validate your APIs. – scottb Mar 14 '17 at 04:22
  • 1
    Normally when you write unit tests you are concerned only with the final result, not with the intermediate calculations. I'm not aware of an way in Java to check local variables externally. This might be possible with a debugger (literally driving the debugger interface with another program) but that hardly seems practical. – markspace Mar 14 '17 at 04:24
  • Quite right. If an API is developed to a contract (as it should be), then validating the implementation of the API involves unit tests. If the implementation is invalid (because the unit tests fail), then use a debugger. The reason for the OP's need for some other nuance escapes me. – scottb Mar 14 '17 at 04:29
  • @scottb markspace answerd your question why not unit tests. – Vipin Mar 14 '17 at 04:30
  • @Vipin not quite. – scottb Mar 14 '17 at 04:30
  • @scottb you may want to read byteman, that also provides some functionality to test non testable code. In this specific case I have thousands of scenarios and i can't afford to check/test all using debugger. I am trying to automate all my manual testing efforts. – Vipin Mar 14 '17 at 04:34
  • @Vipin well, I'm not going to pretend that I understand fully your use case or scenario. However, I've found in the past that whenever I'm unable to articulate a concise contract for an API that lends itself well to unit testing, more often than not it is because the API is flawed ... and the testing not so much. – scottb Mar 14 '17 at 04:38
  • @scottb I can write junit test cases to test all input output combinations, but after checking variable values this test program will be able to tell where exactly we have problem in code. Updated question as well with this explanation. – Vipin Mar 14 '17 at 04:49
  • At a very low level, you could get to the VM and look at the byte code. A debugger is going to be easier, and it might be possible to script one (e.g., http://stackoverflow.com/questions/630044/how-to-script-the-java-debugger-command-line-tool-jdb). Similar to @scottb, I think the issue boils down to either the API provides the expected result or it doesn't. If it is a 3rd party lib and it fails, and you cannot make a change to it, then one is left with not using it, working around it, or waiting on a fix. If it isn't your code, it is a black box, and should be tested as such, IMO. – KevinO Mar 14 '17 at 07:02
  • @KevinO I am developing that API most of the time, I want to write program which can debug two versions and tell differences of variable values for thousands of scenarios. I know I can debug and see what I want but I want to write program for that. Not sure how can I emphasise this, I don't want to debug myself. – Vipin Mar 14 '17 at 07:28
  • @Vipin, I think we understand your emphasis, though you have now stated it is a both a third party lib and that you develop it. To look at variable change inside a method, a couple of people proposed scripting a debugger (which is writing a program). It was suggested you might get at the bytecode (see http://asm.ow2.org/; see also http://stackoverflow.com/questions/6816951/can-i-get-information-about-the-local-variables-using-java-reflection). It appears logging is out (ignoring Kernighan & Ritchie's admonishment). Still think you have an XY Problem, & that others are suggesting the same. – KevinO Mar 14 '17 at 08:08
  • @KevinO i am writing API is available in question, I think it is not xy problem. I am open for any way which is elegant and works well. So far only you have come up with one solution which I think may work. Do you have any example how can I check variable value using debugger API ? – Vipin Mar 14 '17 at 08:55

0 Answers0