Discussions
Categories
- 197.1K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.7K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 555 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.3K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
How to get line number in JAVA code has been executed

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:
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!
Answers
-
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
-
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
-
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.
-
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
-
Maybe, the answer is that the OP should get familiar with white-box testing methods and frameworks such as JUnit.
-
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.
-
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.