|
| 1 | +# Java SDG Slicer |
| 2 | + |
| 3 | +A program slicer for Java, based on the system dependence graph (SDG). *Program slicing* is a software analysis technique to extract the subset of statements that are relevant to the value of a variable in a specific statement (the *slicing criterion*). The subset of statements is called a *slice*, and it can be used for debugging, parallelization, clone detection, etc. This repository contains two modules: |
| 4 | + |
| 5 | +* `sdg-core`, a library that obtains slices from Java source code via the SDG, a data structure that represents statements as nodes and their dependencies as arcs. |
| 6 | +* `sdg-cli`, a command line client for `sdg-core`, which takes as input a Java program and the slicing criterion, and outputs the corresponding slice. |
| 7 | + |
| 8 | +Warning: all method calls must resolve to a method declaration. If your Java program requires additional libraries, their source code must be available and included in the analysis with the `-i` option. Any method call that cannot be resolved will result in a runtime error. |
| 9 | + |
| 10 | +## Quick start |
| 11 | + |
| 12 | +### Build the project |
| 13 | + |
| 14 | +``` |
| 15 | +cd sdg-core |
| 16 | +mvn install |
| 17 | +cd ../sdg-cli |
| 18 | +mvn package |
| 19 | +cd .. |
| 20 | +``` |
| 21 | + |
| 22 | +A fat jar containing all the project's dependencies can be then located at `./sdg-cli/target/sdg-cli-{version}-jar-with-dependencies.jar`. |
| 23 | + |
| 24 | +### Slice a Java program |
| 25 | + |
| 26 | +The slicing criterion can be specified with the flag `-c {file}#{line}:{var}[!{occurrence}`, where the file, line and variable can be specified. If the variable appears multiple times in the given line, an occurrence can be set (append `:2` to select the second occurrence). |
| 27 | + |
| 28 | +If we wish to slice following program with respect to variable `sum` in line 11, |
| 29 | + |
| 30 | +```java= |
| 31 | +public class Example { |
| 32 | + public static void main(String[] args) { |
| 33 | + int sum = 0; |
| 34 | + int prod = 0; |
| 35 | + int i; |
| 36 | + int n = 10; |
| 37 | + for (i = 0; i < 10; i++) { |
| 38 | + sum += 1; |
| 39 | + prod += n; |
| 40 | + } |
| 41 | + System.out.println(sum); |
| 42 | + System.out.println(prod); |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | +The program can be saved to `Example.java`, and the slicer run with: |
| 47 | + |
| 48 | +``` |
| 49 | +java -jar sdg-cli.jar -c Example.java#11:sum |
| 50 | +``` |
| 51 | + |
| 52 | +A more detailed description of the available options can be seen with: |
| 53 | + |
| 54 | +``` |
| 55 | +java -jar sdg-cli.jar --help |
| 56 | +``` |
| 57 | + |
| 58 | +## Library usage |
| 59 | + |
| 60 | +A good usage example of `sdg-core` to obtain a slice from source code is available at [Slicer.java#slice()](/sdg-cli/src/main/java/tfm/cli/Slicer.java#L204), where the following steps are performed: |
| 61 | + |
| 62 | +1. JavaParser is configured to (a) resolve calls in the JRE and the user-defined libraries, and to (b) ignore comments. |
| 63 | +2. The user-defined Java files are parsed to build a list of `CompilationUnit`s. |
| 64 | +3. The SDG is created based on that list. The kind of SDG created depends on a flag. |
| 65 | +4. A `SlicingCriterion` is created, from the input arguments, and the slice is obtained. |
| 66 | +5. The slice is converted to a list of `CompilationUnit` (each representing a file). |
| 67 | +6. The contents of each `CompilationUnit` are dumped to their corresponding file. |
| 68 | + |
| 69 | +If the graph is of interest, it can be outputted in `dot` or PDF format via `SDGLog#generateImages()`, as can be seen in [PHPSlice.java#124](/sdg-cli/src/main/java/tfm/cli/PHPSlice.java#L124) (this class presents a frontend for an unreleased web Java slicer). |
| 70 | + |
| 71 | +## Missing Java features |
| 72 | + |
| 73 | +* Object-oriented features: abstract classes, interfaces, class, method and field inheritance, anonymous classes, lambdas. |
| 74 | +* Parallel features: threads, shared memory, synchronized methods, etc. |
| 75 | +* Exception handling: `finally`, try with resources. |
0 commit comments