Skip to content

LeeReindeer/codeflow-core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeFlowicon

Let's convert Java code to flowchart graph!

Features

Java code to flowchart graph:

*: reconsider or will not be supported.

  • Control flow in Java: sequence, loop(for, while, do while) and condition(if)
  • Support switch statement
  • Support break, contuine with labels
  • throw, return keywords
  • Top-level function and statement
  • Multi-function supported, which represented in subgraphs
  • Function overload(different arguments size only)
  • Semantic check and warning
  • *Support try...catch

Quick start

CodeFlow codeFlow = CodeFlow.builder()
              .outDir("tests")
              .format(Format.PNG)
              .build();
codeFlow.parse("if(ok){doSome();}else{doSomeElse();}").toFile("file.png");

Usage

  1. Add dependency

Maven:

<repositories>
    <repository>
        <id>jitpack.io</id>
		<url>https://jitpack.io</url>
    </repository>
</repositories>
<dependency>
    <groupId>com.github.LeeReindeer</groupId>
    <artifactId>codeflow-core</artifactId>
    <version>${latest-version}</version>
</dependency>

Gradle:

allprojects {
    repositories {
        ...
	    maven { url 'https://jitpack.io' }
	}
}
implementation 'com.github.LeeReindeer:codeflow-core:${latest-version}'

For snapshot, use master-SNAPSHOT as the version tag.

  1. Get a builder
CodeFlow codeFlow = CodeFlow.builder()
        .useNative(true)            // Linux x86_64 only.Use native bind of graphviz, faster.
        .supportClass(false)        // whether support class declare
        .failFast(true)		    // not recovery from syntax error
   			            // the graph will not created when syntax error occurred.
        .workDir("examples")        // input file dir
        .outDir("tests")            // output file dir
        .format(Format.PNG)         // output file format
        .build();
  1. Configuration Flowchart style(optional)

All flowchart configuration is in FlowchartConfig as static fields.

  public static boolean virtualStartNode = true; // add a start node in graph 
  public static boolean virtualEndNode = true;   // add a end node
  public static boolean mergeSequences = false;  // merge non-logical relationship sequence statements
  public static String functionColor = "lightblue"; //function call node color
  // You can set decision nodes' compass,
  // but the best you can do is not interfering the layout engine.
  // It will avoid most line intersections in graphviz.
  // Nevertheless, if you sill want to customize, the recommend preference is commented following.
  public static String decisionTrueCompass; // s
  public static String decisionFalseCompass; // w
  public static String doWhileDecisionTrueCompass; // e
  public static String doWhileDecisionFalseCompass; // s
  1. Do Convert
  • parse(String code)

  • parse(Supplier<String> supplier)

  • parse(File file)

  • parseFile(String path)

Examples:

// convert code string to java.awt.image.BufferedImage
codeFlow.parse("if(ok){doSome();}else{doSomeElse();}").toImage();
// convert code in file simple.cf to simple.png
codeFlow.parseFile("simple.cf").toFile("simple.png");

Examples

int binarySearch(int[] a, int k) {
    int len = a.length;
    int l = 0, r = len - 1;
    while (l < r) {
      int mid = (l + r) / 2;
      if (a[mid] < k) {
        l = mid + 1;
      }
      else if (a[mid] > k) {
        r = mid - 1;
      } else {
        return mid;
      }
    }
    return -1;
}

Above code will generate a flowchart like:

binarySearch

See more examples at examples folder and test folder.

How it works

  • It use ANTLR4 to generate parser to parse Java code.

  • And then, visit the AST to build flowchart fragment:

    • The single flowchart fragment is built directly
    • The complex flowchart fragment(like nested if statements and loops) is built recursively
  • Use graphviz-java library convert node data structure to dot file.

  • Finally, use Graphviz to convert dot file to graph image.