Forum Stats

  • 3,839,816 Users
  • 2,262,538 Discussions
  • 7,901,063 Comments

Discussions

How to get line number in JAVA code has been executed

9dbff406-2843-45e2-9ace-bb31b351f404
edited Dec 21, 2016 4:02PM in Java Programming

I wanted to count code execution routes of a method.So I want to do something at the end of a method to get the line numbers of the codes had been executed in the method of a call

I have a class like this:

2016-11-09_142758.png

if I execute the code "new Test().run(5, 3);"

I can get the result like this:"5,6,7,8".--Because the codes at line 9,10,11 are not be executed

if I execute the code "new Test().run(5, 6);"

I can get the result like this:"5,6,9,10,11"  --Because the codes at line 7,8 are not be executed

How can I do this? Thanks for answering!

Tagged:
morgalrEmyr

Answers

  • rpc1
    rpc1 Member Posts: 1,503
    edited Nov 9, 2016 3:28AM
    9dbff406-2843-45e2-9ace-bb31b351f404 wrote:if I execute the code "new Test().run(5, 3);"I can get the result like this:"5,6,7,8".--Because the codes at line 9,10,11 are not be executed if I execute the code "new Test().run(5, 6);" I can get the result like this:"5,6,9,10,11" --Because the codes at line 7,8 are not be executed How can I do this? Thanks for answering!

    You could add  extra parameter LinkedList and add row numbers manualy, and than extract route for example

    int c = a;  list.add(5);

    and etc it looks ugly, but enough for debuging or statistics

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Nov 9, 2016 9:56AM

    sometimes the public Java API is quite helpfull:

    https://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.html

    public void myMethod(){  System.out.println(Thread.getCurrentThread().getStacktrace()[0].<strong><a href="https://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.html#getLineNumber%28%29">getLineNumber</a></strong>());}

    bye

    TPD

    Emyr
  • Unknown
    edited Nov 9, 2016 11:31AM
    I wanted to count code execution routes of a method.So I want to do something at the end of a method to get the line numbers of the codes had been executed in the method of a callI have a class like this:

    What you suggest would NOT count 'execution routes' - it wouldn't 'count' anything. It would produce a huge, useless list of line numbers. And those line numbers would be totally useless as soon as any one added even one line, even a comment line, to the source code.

    Why don't you tell us WHAT PROBLEM are you trying to solve?

    If you really wanted 'execution routes' your code already illustrates what you need to do. Replace lines 7 and 10 (the System.out.println calls) with lines that LOG that info to a file.

    You would also typically declare a static LOG level and add conditional statements to only log based on the level that was set.

    That way you can include the logging statements during development and test but have the compiler exclude them when you produce the production code.

  • morgalr
    morgalr Member Posts: 457
    edited Dec 20, 2016 6:48PM

    I know this is an old question, but it looks like rp0428 is the closest answer so far, but in reality: Java does not have line numbers, nor can you expect what you have in your code to be the order, or commands, that things are executed. Java runs on what is know as the JIT, just in time, compiler. It does optimizations of your code when it compiles, it compiles as it needs to do so or your put out your executable JAR for your project. Once in the compiled form, there is no guarantee that your code structure even exists any longer.  In that light, what does "line number in JAVA code has been executed' really mean? There is literally nothing to reference it back to in your original source script.

    So if you are looking for optimizations, then my very best recommendation is to right "stupid code". That is code that is text book in it's structure and readability, then let the compiler do what it does best, make it optimal!  I have seen a lot of new people coming from various other languages waste a lot of time in trying to get optimal code, only to find out that the compiler optimizes a lot better than they do or ever will.

    So be technically proficient, and write "stupid code", you'll be much happier in doing so, and your code will run faster and others will be happier reading your source.

    That is not to say that if you have code that has a bottleneck you should not try to fix it, but get a good profiler and pay attention to what is says, rewrite the bottleneck and leave the rest.  In many instances a bottleneck is created by the programmer trying to be optimal instead of making "stupid code" that the compiler can recognize intended process and optimize appropriately.

    Les

  • Jiri.Machotka-Oracle
    Jiri.Machotka-Oracle Member Posts: 5,078
    edited Dec 21, 2016 8:21AM

    Maybe, the answer is that the OP should get familiar with white-box testing methods and frameworks such as JUnit.

    morgalr
  • Unknown
    edited Dec 21, 2016 11:50AM
    Maybe, the answer is that the OP should get familiar with white-box testing methods and frameworks such as JUnit.

    Give us an example of how you would use those to test OPs use case.

    1. a method that does NOT return a value

    2. a method with multiple possible execution paths

    3. a method that, if it DID return a value, could possibly return the same value for different execution paths.

    The use case presented is a very common one. Methods often include multiple execution paths and testing almost never checks all of them. It can be difficult to check all of them without adding additional logging/debugging code. Then you have to deal with either disabling, or removing the added code for actual use.

  • Jiri.Machotka-Oracle
    Jiri.Machotka-Oracle Member Posts: 5,078
    edited Dec 21, 2016 4:02PM

    First of all, it is quite easy to find this answer by Google: Logging line numbers of the java code which has executed - Stack Overflow

    Second, as @morgalr correctly mentioned, unlike in Basic in Java there is nothing like a line number - it is something relevant only to IDE. I remember that one of my uni mates proved this statement by writing the whole program in a single line of code (on a very long paper). Therefore, the problem itself as such is very artificial.

    On the contrary, Java has blocks and if rather than lines of code it is blocks that were executed that need to be traced, and you don't want to use the brute force methods such as println or log4j line tracing, you can use test frameworks such as JUnit to define tests you always want to be true for your code. Of course, it might never be complete, because you might have to use discrete values such as (5, 3) to test relations such as (c>b). In fact, some approaches (test-driven programming) even require that a test is written before the code under test is changed. It might not be an answer to the OP's task, but it is a kind of standard how real (not artificial) requirements are solved.

This discussion has been closed.