Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitde0d5b4

Browse files
committed
feat: parse ast
0 parents  commitde0d5b4

File tree

38 files changed

+1854
-0
lines changed

38 files changed

+1854
-0
lines changed

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
java-doc.iml
3+
/target

‎pom.xml‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.github</groupId>
8+
<artifactId>generate-java-doc</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>generate-java-doc</name>
13+
14+
<properties>
15+
<java.version>1.8</java.version>
16+
<project.encoding>UTF-8</project.encoding>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.projectlombok</groupId>
22+
<artifactId>lombok</artifactId>
23+
<version>1.16.16</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.apache.commons</groupId>
28+
<artifactId>commons-lang3</artifactId>
29+
<version>3.3.2</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>commons-io</groupId>
33+
<artifactId>commons-io</artifactId>
34+
<version>1.4</version>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>com.alibaba</groupId>
39+
<artifactId>fastjson</artifactId>
40+
<version>1.2.60</version>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>com.github.javaparser</groupId>
45+
<artifactId>javaparser-symbol-solver-core</artifactId>
46+
<version>3.14.3</version>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-compiler-plugin</artifactId>
55+
<configuration>
56+
<source>${java.version}</source>
57+
<target>${java.version}</target>
58+
<encoding>${project.encoding}</encoding>
59+
</configuration>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-jar-plugin</artifactId>
64+
<version>2.3.1</version>
65+
</plugin>
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-resources-plugin</artifactId>
69+
<version>2.5</version>
70+
<configuration>
71+
<encoding>UTF-8</encoding>
72+
</configuration>
73+
</plugin>
74+
<plugin>
75+
<artifactId>maven-source-plugin</artifactId>
76+
<version>2.1</version>
77+
<configuration>
78+
<attach>true</attach>
79+
</configuration>
80+
<executions>
81+
<execution>
82+
<phase>compile</phase>
83+
<goals>
84+
<goal>jar</goal>
85+
</goals>
86+
</execution>
87+
</executions>
88+
</plugin>
89+
<plugin>
90+
<groupId>org.apache.maven.plugins</groupId>
91+
<artifactId>maven-deploy-plugin</artifactId>
92+
<version>2.5</version>
93+
</plugin>
94+
</plugins>
95+
</build>
96+
</project>
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
packagecom.github.doc;
2+
3+
importcom.github.doc.model.ClassDoc;
4+
importcom.github.doc.model.merge.MergeInterfaceDoc;
5+
importcom.github.doc.parse.ClassParser;
6+
importcom.github.doc.parse.EnumParser;
7+
importcom.github.doc.parse.InterfaceParser;
8+
importcom.github.doc.parse.Parser;
9+
importcom.github.doc.tool.InterfaceMerge;
10+
importcom.github.doc.util.CollectionUtils;
11+
importcom.github.javaparser.JavaParser;
12+
importcom.github.javaparser.ParseResult;
13+
importcom.github.javaparser.ast.CompilationUnit;
14+
importcom.github.javaparser.ast.body.TypeDeclaration;
15+
importcom.google.common.collect.Lists;
16+
importcom.google.common.collect.Sets;
17+
importorg.apache.commons.io.FileUtils;
18+
importorg.apache.commons.io.IOUtils;
19+
20+
importjava.io.File;
21+
importjava.io.FileNotFoundException;
22+
importjava.io.IOException;
23+
importjava.util.Collection;
24+
importjava.util.Collections;
25+
importjava.util.List;
26+
importjava.util.Map;
27+
importjava.util.Set;
28+
29+
/**
30+
* @author wangdongbo
31+
* @since 2020/3/19.
32+
*/
33+
publicclassDocExecute {
34+
35+
privatestaticfinalList<Parser>PARSERS =Lists.newArrayList();
36+
37+
static {
38+
PARSERS.add(newClassParser());
39+
PARSERS.add(newInterfaceParser());
40+
PARSERS.add(newEnumParser());
41+
}
42+
43+
publicstatic <RextendsClassDoc>List<R>generateDoc(List<String>classFilePaths)throwsFileNotFoundException {
44+
if (CollectionUtils.isEmpty(classFilePaths)) {
45+
returnCollections.emptyList();
46+
}
47+
List<R>classDocs =Lists.newArrayList();
48+
for (StringclassFilePath :classFilePaths) {
49+
Filefile =newFile(classFilePath);
50+
if (!file.exists() || !file.isFile()) {
51+
continue;
52+
}
53+
ParseResult<CompilationUnit>unit =newJavaParser().parse(newFile(classFilePath));
54+
if (!unit.getResult().isPresent()) {
55+
continue;
56+
}
57+
if (CollectionUtils.isEmpty(unit.getResult().get().getTypes())) {
58+
continue;
59+
}
60+
TypeDeclarationtypeDeclaration =unit.getResult().get().getType(0);
61+
for (Parserparser :PARSERS) {
62+
Rr =parser.parse(typeDeclaration);
63+
if (r !=null) {
64+
r.setClassFilePath(classFilePath);
65+
classDocs.add(r);
66+
break;
67+
}
68+
}
69+
}
70+
returnclassDocs;
71+
}
72+
73+
@SuppressWarnings("unchecked")
74+
publicstatic <RextendsClassDoc>List<R>generateDoc(List<String>fileOrDirs,List<String>filterClass,booleanrecursive)throwsFileNotFoundException {
75+
if (CollectionUtils.isEmpty(fileOrDirs)) {
76+
returnCollections.emptyList();
77+
}
78+
Set<String>filters =Collections.emptySet();
79+
if (CollectionUtils.isNotEmpty(filterClass)) {
80+
filters =Sets.newHashSet(filterClass);
81+
}
82+
List<String>paths =Lists.newArrayList();
83+
for (Stringdir :fileOrDirs) {
84+
Filefile =newFile(dir);
85+
if (!file.exists()) {
86+
continue;
87+
}
88+
if (file.isFile() && !filters.contains(file.getAbsolutePath())) {
89+
paths.add(file.getAbsolutePath());
90+
continue;
91+
}
92+
if (file.isDirectory()) {
93+
Collection<File>collection =FileUtils.listFiles(file,null,recursive);
94+
if (CollectionUtils.isNotEmpty(collection)) {
95+
Set<String>finalFilters =filters;
96+
collection.stream().map(File::getAbsolutePath).filter(s -> !finalFilters.contains(s)).forEach(paths::add);
97+
}
98+
}
99+
}
100+
returngenerateDoc(paths);
101+
}
102+
103+
publicstatic <RextendsClassDoc>List<R>generateDoc(Map<String,String>fileContentMap)throwsIOException {
104+
if (CollectionUtils.isEmpty(fileContentMap)) {
105+
returnCollections.emptyList();
106+
}
107+
List<R>classDocs =Lists.newArrayList();
108+
for (Map.Entry<String,String>entry :fileContentMap.entrySet()) {
109+
ParseResult<CompilationUnit>unit =newJavaParser().parse(IOUtils.toInputStream(entry.getValue(),"utf-8"));
110+
if (!unit.getResult().isPresent()) {
111+
continue;
112+
}
113+
if (CollectionUtils.isEmpty(unit.getResult().get().getTypes())) {
114+
continue;
115+
}
116+
TypeDeclarationtypeDeclaration =unit.getResult().get().getType(0);
117+
for (Parserparser :PARSERS) {
118+
Rr =parser.parse(typeDeclaration);
119+
if (r !=null) {
120+
r.setClassFilePath(entry.getKey());
121+
classDocs.add(r);
122+
break;
123+
}
124+
}
125+
}
126+
returnclassDocs;
127+
}
128+
129+
publicstaticList<MergeInterfaceDoc>generateMergeInterfaceDoc(List<String>classFilePaths)throwsFileNotFoundException {
130+
returnInterfaceMerge.merge(generateDoc(classFilePaths));
131+
}
132+
133+
publicstaticList<MergeInterfaceDoc>generateMergeInterfaceDoc(List<String>fileOrDirs,List<String>filterClass,booleanrecursive)throwsFileNotFoundException {
134+
returnInterfaceMerge.merge(generateDoc(fileOrDirs,filterClass,recursive));
135+
}
136+
137+
publicstaticList<MergeInterfaceDoc>generateMergeInterfaceDoc(Map<String,String>fileContentMap)throwsIOException {
138+
returnInterfaceMerge.merge(generateDoc(fileContentMap));
139+
}
140+
141+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
packagecom.github.doc;
2+
3+
importcom.github.doc.model.ClassDoc;
4+
importcom.github.doc.model.EnumDoc;
5+
importcom.github.doc.model.InterfaceDoc;
6+
importcom.github.doc.model.ObjectDoc;
7+
importcom.github.doc.model.merge.MergeInterfaceDoc;
8+
importcom.github.doc.tool.markdown.EnumMarkDown;
9+
importcom.github.doc.tool.markdown.InterfaceMarkDown;
10+
importcom.github.doc.tool.markdown.InterfaceMergeMarkDown;
11+
importcom.github.doc.tool.markdown.ObjectMarkDown;
12+
importcom.github.doc.util.CollectionUtils;
13+
importcom.google.common.collect.Lists;
14+
importcom.google.common.collect.Maps;
15+
16+
importjava.util.Collections;
17+
importjava.util.List;
18+
importjava.util.Map;
19+
20+
/**
21+
* @author wangdongbo
22+
* @since 2020/3/23.
23+
*/
24+
publicclassDocMarkDownExecute {
25+
26+
publicstaticList<String>generateMarkDown(List<ClassDoc>classDocs) {
27+
if (CollectionUtils.isEmpty(classDocs)) {
28+
returnCollections.emptyList();
29+
}
30+
List<String>markDowns =Lists.newArrayListWithCapacity(classDocs.size());
31+
classDocs.forEach(c -> {
32+
if (cinstanceofInterfaceDoc) {
33+
markDowns.add(InterfaceMarkDown.generate((InterfaceDoc)c));
34+
}elseif (cinstanceofEnumDoc) {
35+
markDowns.add(EnumMarkDown.generate((EnumDoc)c));
36+
}elseif (cinstanceofObjectDoc) {
37+
markDowns.add(ObjectMarkDown.generate((ObjectDoc)c));
38+
}
39+
});
40+
returnmarkDowns;
41+
}
42+
43+
publicstaticMap<String,String>generateMergeInterfaceMarkDown(List<MergeInterfaceDoc>mergeInterfaceDocList) {
44+
if (CollectionUtils.isEmpty(mergeInterfaceDocList)) {
45+
returnCollections.emptyMap();
46+
}
47+
Map<String,String>map =Maps.newHashMapWithExpectedSize(mergeInterfaceDocList.size());
48+
mergeInterfaceDocList.forEach(doc ->map.put(doc.getFullName(),InterfaceMergeMarkDown.generate(doc)));
49+
returnmap;
50+
}
51+
52+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
packagecom.github.doc.model;
2+
3+
importcom.google.common.base.Joiner;
4+
importcom.google.common.collect.Sets;
5+
importlombok.Data;
6+
importorg.apache.commons.lang3.StringUtils;
7+
8+
importjava.util.List;
9+
importjava.util.Set;
10+
importjava.util.stream.Collectors;
11+
importjava.util.stream.Stream;
12+
13+
/**
14+
* @author wangdongbo
15+
* @since 2020/3/19.
16+
*/
17+
@Data
18+
publicclassClassDoc {
19+
20+
privatebooleanisInterface =false;
21+
22+
privatebooleanisDeprecated =false;
23+
24+
privateStringname;
25+
26+
privateStringpackageName;
27+
28+
privateStringfullName;
29+
30+
privateStringclassFilePath;
31+
32+
privateStringcomment;
33+
34+
privateStringextendsFullName;
35+
36+
privatetransientSet<String>imports =Sets.newLinkedHashSet();
37+
38+
publicvoidsetComment(Stringcomment) {
39+
this.comment =parse(comment);
40+
}
41+
42+
privateStringparse(Stringcomment) {
43+
if (StringUtils.isBlank(comment)) {
44+
returnnull;
45+
}
46+
comment =comment.replace("*","");
47+
comment =comment.replaceAll("(\n|\r\n)\\s+","$1");
48+
comment =comment.replaceAll("[ ]+"," ");
49+
if (StringUtils.isBlank(comment)) {
50+
returnnull;
51+
}
52+
List<String>strings =Stream.of(comment.split("\n")).filter(this::filterStr).map(this::getStr).filter(StringUtils::isNotBlank).collect(Collectors.toList());
53+
returnJoiner.on(", ").join(strings);
54+
}
55+
56+
privatebooleanfilterStr(Strings) {
57+
if (!s.startsWith("@")) {
58+
returntrue;
59+
}
60+
if (s.startsWith("@describe")) {
61+
returntrue;
62+
}
63+
if (s.startsWith("@description")) {
64+
returntrue;
65+
}
66+
returnfalse;
67+
}
68+
69+
privateStringgetStr(Strings) {
70+
if (s.startsWith("@describe")) {
71+
returns.replace("@describe:","");
72+
}
73+
if (s.startsWith("@description")) {
74+
returns.replace("@description:","");
75+
}
76+
returns;
77+
}
78+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp