Skip to content

Commit

Permalink
add a new maven plugin to visualize class hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
gaopeng71 committed Aug 22, 2023
1 parent 2373bb8 commit dc46936
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright DDDplus Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package io.github.dddplus.maven;

import io.github.dddplus.ast.DomainModelAnalyzer;
import io.github.dddplus.ast.model.ReverseEngineeringModel;
import io.github.dddplus.ast.view.ClassHierarchyRenderer;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;

/**
* 无需标注,类的层级结构可视化.
*/
@Mojo(name = "clazz", aggregator = true)
public class ClassHierarchyMojo extends AbstractMojo {

/**
* Colon separated directories.
*/
@Parameter(property = "rootDir", required = true)
String rootDir;

@Parameter(property = "targetFile", required = true)
String targetFile;

/**
* Comma separated ignored parent classes.
*/
@Parameter(property = "ignoreParents")
String ignoreParents;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Reverse modeling starting ...");
try {
String[] dirPaths = rootDir.split(":");
File[] dirs = new File[dirPaths.length];
for (int i = 0; i < dirPaths.length; i++) {
dirs[i] = new File(dirPaths[i]);
}

ReverseEngineeringModel model = new DomainModelAnalyzer()
.scan(dirs)
.classHierarchyOnly()
.analyze();
getLog().info("Rendering ...");
if (ignoreParents == null) {
ignoreParents = "";
}
new ClassHierarchyRenderer()
.withModel(model)
.ignores(ignoreParents.split(","))
.targetDotFile(targetFile)
.render();

getLog().info("Reverse Modeling Executed OK");
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class DomainModelAnalyzer {
private Set<String> ignoredAnnotations = new HashSet<>(); // in simpleName
private boolean rawSimilarity = false;
private boolean disableCallGraph = false;
private boolean classHierarchyOnly = false;
private List<Map<String, String>> keyModelPackageFixes = new ArrayList<>();

public DomainModelAnalyzer scan(File... dirs) {
Expand All @@ -38,6 +39,11 @@ public DomainModelAnalyzer fixKeyModelPackage(String fromPkg, String toPkg) {
return this;
}

public DomainModelAnalyzer classHierarchyOnly() {
this.classHierarchyOnly = true;
return this;
}

public DomainModelAnalyzer rawSimilarity() {
this.rawSimilarity = true;
return this;
Expand Down Expand Up @@ -102,6 +108,9 @@ public ReverseEngineeringModel analyze(FileWalker.Filter filter) {
new FileWalker(actualFilter, (level, path, file) -> {
new ClassHierarchyAstNodeVisitor().visit(FileWalker.silentParse(file), model.getClassHierarchyReport());
}).walkFrom(dir);
if (classHierarchyOnly) {
continue;
}

// class method distribution
log.debug("parsing {}", ClassMethodDistributionAstNodeVisitor.class.getSimpleName());
Expand Down Expand Up @@ -158,6 +167,10 @@ public ReverseEngineeringModel analyze(FileWalker.Filter filter) {
}).walkFrom(dir);
}

if (classHierarchyOnly) {
return model;
}

// similarity
log.debug("calculating key models similarity");
JaccardModelSimilarity similarityAnalyzer = new JaccardModelSimilarity();
Expand Down

0 comments on commit dc46936

Please sign in to comment.