|
| 1 | +# Java Dependency Mapper |
| 2 | + |
| 3 | +A tool for analyzing Java dependencies from extracted EAR files. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This tool parses JSONL files containing dependency information extracted from Java EAR/JAR/WAR files. It helps visualize and analyze dependencies between classes and artifacts. |
| 8 | + |
| 9 | +The input JSONL files are typically generated from Jarviz-lib static analysis of Java applications. Jarviz-lib analyzes the bytecode of Java applications and extracts method-level dependencies between classes. |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- Parse JSONL files with Java dependency information |
| 14 | +- Build a dependency graph of classes and artifacts |
| 15 | +- Find cycles in the dependency graph |
| 16 | +- Generate statistics about dependencies |
| 17 | +- View the most depended-upon classes |
| 18 | +- Extract and analyze package dependencies |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +1. Make sure you have Node.js installed (v14+ recommended) |
| 23 | +2. Clone this repository |
| 24 | +3. Install dependencies: |
| 25 | + |
| 26 | +```bash |
| 27 | +npm install |
| 28 | +``` |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +### Dependency Mapper |
| 33 | + |
| 34 | +Run the tool with a path to your JSONL file: |
| 35 | + |
| 36 | +```bash |
| 37 | +npm start -- path/to/your/dependencies.jsonl |
| 38 | +``` |
| 39 | + |
| 40 | +### Package Dependencies Extractor |
| 41 | + |
| 42 | +The package dependencies extractor tool generates a Markdown report of all base packages that a project depends on: |
| 43 | + |
| 44 | +```bash |
| 45 | +npx ts-node package-dependencies.ts <jsonl-file-path> [--output <output-file-path>] |
| 46 | +``` |
| 47 | + |
| 48 | +Where: |
| 49 | +- `<jsonl-file-path>` is the path to the JSONL file containing dependency data (required) |
| 50 | +- `--output` or `-o` followed by path where the Markdown output will be written (optional, defaults to `package-dependencies.md`) |
| 51 | + |
| 52 | +Example usage: |
| 53 | +```bash |
| 54 | +# Basic usage with default output file |
| 55 | +npx ts-node package-dependencies.ts sample-dependencies.jsonl |
| 56 | + |
| 57 | +# Specify custom output file |
| 58 | +npx ts-node package-dependencies.ts sample-dependencies.jsonl --output reports/packages.md |
| 59 | + |
| 60 | +# Using shorthand parameter |
| 61 | +npx ts-node package-dependencies.ts sample-dependencies.jsonl -o custom-output.md |
| 62 | +``` |
| 63 | + |
| 64 | +## Development |
| 65 | + |
| 66 | +### Running Tests |
| 67 | + |
| 68 | +The project includes unit tests for the package dependencies extractor. To run the tests: |
| 69 | + |
| 70 | +```bash |
| 71 | +npm test |
| 72 | +``` |
| 73 | + |
| 74 | +This will execute all Jest tests in the `tests` directory. |
| 75 | + |
| 76 | +To run tests with coverage reports: |
| 77 | + |
| 78 | +```bash |
| 79 | +npm run test:coverage |
| 80 | +``` |
| 81 | + |
| 82 | +This will generate a detailed coverage report showing which parts of the code are covered by tests. |
| 83 | + |
| 84 | +### Input Data |
| 85 | + |
| 86 | +This tool is designed to work with JSONL output from Jarviz-lib analysis. Jarviz-lib is a static analysis tool that extracts method-level dependencies from Java bytecode. To generate the input data: |
| 87 | + |
| 88 | +1. Extract your EAR/WAR/JAR files |
| 89 | +2. Run Jarviz-lib analysis on the extracted files |
| 90 | +3. Use the resulting JSONL file as input to this tool |
| 91 | + |
| 92 | +### Expected JSONL Format |
| 93 | + |
| 94 | +Each line in the JSONL file should be a JSON object with the following structure: |
| 95 | + |
| 96 | +```json |
| 97 | +{ |
| 98 | + "appSetName": "AppName", |
| 99 | + "applicationName": "AppName", |
| 100 | + "artifactFileName": "example.jar", |
| 101 | + "artifactId": "exampleModule", |
| 102 | + "artifactGroup": "com.example", |
| 103 | + "artifactVersion": "1.0.0", |
| 104 | + "sourceClass": "com.example.SourceClass", |
| 105 | + "sourceMethod": "methodName", |
| 106 | + "targetClass": "com.example.TargetClass", |
| 107 | + "targetMethod": "targetMethod" |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Dependency Mapper Output |
| 112 | + |
| 113 | +The dependency mapper will output: |
| 114 | +- General statistics about dependencies |
| 115 | +- Artifact dependency relationships |
| 116 | +- Cycles in the dependency graph (if any) |
| 117 | +- Top 10 most depended upon classes |
| 118 | + |
| 119 | +### Example Output |
| 120 | + |
| 121 | +``` |
| 122 | +Parsing dependencies from sample-dependencies.jsonl... |
| 123 | +
|
| 124 | +Dependency Statistics: |
| 125 | +Total components: 4 |
| 126 | +Total artifacts: 1 |
| 127 | +Total dependencies: 3 |
| 128 | +
|
| 129 | +Top 10 most depended upon classes: |
| 130 | +1. com.example.sample.component.servicelocator.ServiceLocatorException: 1 dependents |
| 131 | +2. java.lang.Object: 1 dependents |
| 132 | +3. java.lang.Boolean: 1 dependents |
| 133 | +4. com.example.sample.component.servicelocator.ejb.ServiceLocator: 0 dependents |
| 134 | +
|
| 135 | +Artifact Dependencies: |
| 136 | +No inter-artifact dependencies found. |
| 137 | +
|
| 138 | +Cycles in Dependencies: |
| 139 | +No cycles found. |
| 140 | +``` |
| 141 | + |
| 142 | +## Package Dependencies Extractor Output |
| 143 | + |
| 144 | +The package dependencies extractor generates a Markdown file with the following sections: |
| 145 | + |
| 146 | +1. **Base Packages**: A list of all base packages used by the project, grouped by: |
| 147 | + - External Dependencies (e.g., `java.lang`, `javax.servlet`) |
| 148 | + - Internal Packages (e.g., `com.example`) |
| 149 | + |
| 150 | +2. **Dependency Relationships**: Shows which base packages depend on other base packages |
| 151 | + |
| 152 | +3. **Package Details**: Detailed information about each base package, including: |
| 153 | + - Type (Internal or External) |
| 154 | + - Number of sub-packages |
| 155 | + - Number of classes |
| 156 | + - Dependencies on other base packages |
| 157 | + - List of all sub-packages |
| 158 | + |
| 159 | +### How It Works |
| 160 | + |
| 161 | +1. The tool reads the JSONL file line by line |
| 162 | +2. For each record, it extracts the source and target class names and their packages |
| 163 | +3. It categorizes packages as internal or external based on their artifact group |
| 164 | +4. Base packages are determined by: |
| 165 | + - For standard packages (java, javax, org, com, net), the first two segments are used (e.g., `java.lang`) |
| 166 | + - For other packages, the first three segments are used (or fewer if there aren't three) |
| 167 | +5. Dependencies between packages are tracked and rolled up to the base package level |
| 168 | + |
| 169 | +### Example |
| 170 | + |
| 171 | +For a sample input like: |
| 172 | + |
| 173 | +```json |
| 174 | +{"sourceClass":"com.example.sample.component.servicelocator.ejb.ServiceLocator","targetClass":"java.lang.Object"} |
| 175 | +``` |
| 176 | + |
| 177 | +The tool will extract: |
| 178 | +- Base packages: `com.example` and `java.lang` |
| 179 | +- Dependencies: `com.example` depends on `java.lang` |
| 180 | + |
| 181 | +## Using as a Library |
| 182 | + |
| 183 | +You can also use the various analyzer classes in your own code: |
| 184 | + |
| 185 | +```typescript |
| 186 | +import { DependencyAnalyzer } from './dependency-mapper'; |
| 187 | + |
| 188 | +async function analyze() { |
| 189 | + const analyzer = new DependencyAnalyzer(); |
| 190 | + await analyzer.parseJsonlFile('path/to/dependencies.jsonl'); |
| 191 | + |
| 192 | + // Get artifact dependency summary |
| 193 | + const artifactDeps = analyzer.getArtifactDependencySummary(); |
| 194 | + |
| 195 | + // Find cycles |
| 196 | + const cycles = analyzer.findCycles(); |
| 197 | + |
| 198 | + // Get more data as needed |
| 199 | +} |
| 200 | + |
| 201 | +analyze().catch(console.error); |
| 202 | +``` |
0 commit comments