DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • Memory Leak Due to Uncleared ThreadLocal Variables
  • Beyond Java Streams: Exploring Alternative Functional Programming Approaches in Java
  • Java Enterprise Matters: Why It All Comes Back to Jakarta EE
  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java

Trending

  • 8 Steps to Proactively Handle PostgreSQL Database Disaster Recovery
  • What They Don’t Teach You About Starting Your First IT Job
  • Monorepo Development With React, Node.js, and PostgreSQL With Prisma and ClickHouse
  • Revolutionizing Software Development: Agile, Shift-Left, and Cybersecurity Integration
  1. DZone
  2. Coding
  3. Java
  4. How to Visualize Java Module Graphs

How to Visualize Java Module Graphs

Want to learn more about how to visualize Java Module Graphs? Click this tutorial to learn how step-by-step!

By 
Rahman Usta user avatar
Rahman Usta
·
Jun. 14, 18 · Tutorial
Likes (15)
Comment
Save
Tweet
Share
14.9K Views

Join the DZone community and get the full member experience.

Join For Free

This article is a demonstration on how we can visualize a Jigsaw module graph in a Java application. The module API can list Jigsaw modules and its dependents, as shown below.

Set<Module> modules = ModuleLayer.boot().modules();
Set<Requires> requires = module.getDescriptor().requires();


With these two simple commands, we can access the module relation graph in the running application.

To visualize module relations, vis.js can be used. It is easy to create network graphs with vis.js. Take a look at the following code snippet!

// create an array with nodes
  var nodes = new vis.DataSet([
    {id: 'java.base', label: 'java.base'},
    {id: 'java.logging', label: 'java.logging'},
    {id: 'java.sql', label: 'java.sql'}
  ]);

  // create an array with edges
  var edges = new vis.DataSet([
    {from: 'java.sql', to: 'java.base'},
    {from: 'java.sql', to: 'java.logging'},
    {from: 'java.logging', to: 'java.base'}
  ]);

  // create a network
  var container = document.getElementById('mynetwork');
  var data = {
    nodes: nodes,
    edges: edges
  };
  var options = {};
  var network = new vis.Network(container, data, options);


The view should be similar to the image below:

Image title


Let's create a whole alive module graph visualizer.  First, input the following:

public class Node {
    private String id;
    private String label;

    // getters, setters, constructors
}



Node.java represents Node data. Each module name will have one node:

public class Edge {
    private String from;
    private String to;

    // getters, setters, constructors
}


Edge.java represents an edge between two nodes:

@RestController
public class ModuleGraphController {

    @GetMapping("/modules")
    public Map<String, HashSet<?>> moduleInfo() {
        var nodes = new HashSet<Node>(); // <1>
        var edges = new HashSet<Edge>(); // <2>
        fillNodeAndEdges(nodes, edges); // <3>
        return Map.of("nodes", nodes, "edges", edges); // <4>
    }

    private void fillNodeAndEdges(HashSet<Node> nodes, HashSet<Edge> edges) {
        Set<Module> modules = ModuleLayer.boot().modules(); // <5>
        for (Module module : modules) {
            String moduleName = module.getName();

            if (moduleNotContain(moduleName, "jdk")) { // <6>
                nodes.add(new Node(moduleName));
            }

            Set<Requires> requires = module.getDescriptor().requires(); <7>
            for (Requires require : requires) {
                edges.add(new Edge(moduleName, require.name())); <8>
            }
        }
    }

    private boolean moduleNotContain(String moduleName, String text) {
        return !moduleName.startsWith(text);
    }
}
1

Create node set

2

Create edge set

3

Fill node and edge sets

4

Return edge and node sets in a map

5

Access module list

6

Skip jdk internal modules for clarity

7

Access module's dependents

8

Fill edge between module and dependent


ModuleController is a REST controller, and it returns module relations in JSON format. To access this data on JS side, we can use fetch api. Let's look at it.

function showGraph(json) {
        var container = document.getElementById('placeholder');
        var data = {
            nodes: json.nodes,
            edges: json.edges
        };
        var options = {};
        network = new vis.Network(container, data, options);
    }

fetch("/modules") // <1>
  .then(function (res) {
            return res.json()
        })
  .then(showGraph); // <2>
1

Request for /modules json

2

Show the all module relations


That's all!


Here is the final result:

Image title

In order to run the demo, follow these steps:

mvn clean install
java -jar target/module-graph.jar
//  Then open http://localhost:8080

or

mvn clean install
docker build -t rahmanusta/module-graph .
docker run -it -p 8080:8080 rahmanusta/module-graph
//  Then open http://localhost:8080


You can access full source code here: https://github.com/rahmanusta/module-graph.

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Memory Leak Due to Uncleared ThreadLocal Variables
  • Beyond Java Streams: Exploring Alternative Functional Programming Approaches in Java
  • Java Enterprise Matters: Why It All Comes Back to Jakarta EE
  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: