This is a dynamic code analyzer for Java Programming Language to generate statement and branches code coverage reports.
Statement coverage VS. Branch coverage
if(a || b || (c && d && !e)) {
test1 = true;
} else {
test2 = false;
}
- Statement Coverage: when our code says that test1 and test2 are hit.
- Branch Coverage: to test the cases when a is true, when a is false but b is true, when a and b are false but c and d are true and e is false, and so on.
100% branch coverage => 100% statement coverage, while 100% statement coverage does not imply 100% branch coverage
- Java
- IntelliJ
- ANTLR plug-in
- CSS & HTML
β Delivery 1
-
Github repository creation
<div align="center"> <img src="https://github.com/Clara-Raef/Dynamic-Code-Analyzer--Compiler-Course-Project/blob/29d7ccda243cebb76c896872c9171a2abd8f4892/Delivery1/repo-qr-code.png" /> </div>
-
ANTLR Java Lexer & Parser used
- Credits to https://github.com/antlr/grammars-v4/tree/master/java/java
- Custom labels were added to Java Parser later in Delivery 3
-
Testing the grammar & showing the parse tree using ANTLR with Intelli-J
- π§ͺ Simple If condition program that states Success/Failure for a certain grade
public class IfCond { public static void main(String[] args) { int grade=72; if(grade>50){ System.out.print("Succeeded"); } else{ System.out.println("Failed"); } } }
- π· If condition Parse Tree
-
π§ͺ Simple While loop that prints value of variable k of type integer while it's less than or equal to 10
public class WhileLoop { public static void main(String[] args) { int k=4; while(k<=10){ System.out.println(k); } } }
-
π· While loop Parse Tree
- π§ͺ Simple If condition program that states Success/Failure for a certain grade
-
π· While loop fault Parse Tree
-
π· String Operation Parse Tree
-
Starting rule of the grammar: compilationUnit
-
A Java program based on Antlr (USING LISTENER) that takes a java file as an input and outputs a modified intermediate java file (injected code): a comment is added in each code block indicating the block number
β Delivery 2
- A Java program based on Antlr that takes a java code (input.txt) and injects code into it, generating a modified java file (output1.java). When (output1.java) is run, the visited blocks from this code are detected and stated in a text file (output2.txt).
β Delivery3
- Generate an HTML where red-highlighted code blocks are the ones that have not been visited and the green-highlighted code blocks are the ones that have been visited
- Branch coverage report: note the highlighted
x == 0 || y == 1 || y == 5
in red
class main {
public static void main(String[] args) {
int x = 0;
int y = 0;
if(x == 0 || y == 1 || y == 5){
y = 10;
}
else {
if(true){
x++;
}
}
for(int i = 0; i<3 ; i++) x++;
while(y < 5) y = 5;
while(y < 3 || y < 5) y = 4;
if(true){
y++;
}
int a[] = {1,2,3};
for (int i = 0 ; i<4 ;i++){
}
{
int z;
}
}
}
π· Input code, Output modified code, text files containing the indeces of covered blocks & branches
- Automated pipeline
- Testing diverse code cases
public class Prime {
public static void main(String[] args) {
int low = 20, high = 50;
while (low < high) {
if(checkPrimeNumber(low))
System.out.print(low + " ");
++low;
}
}
public static boolean checkPrimeNumber(int num) {
boolean flag = true;
for(int i = 2; i <= num/2; ++i) {
if(num % i == 0) {
flag = false;
break;
}
}
return flag;
}
}
public class PositiveNegative {
public static void main(String[] args) {
double number = 12.3;
if (number < 0.0)
System.out.println(number + " is a negative number.");
else
System.out.println(number + " is a positive number.");
}
}