public static List<JavaDoc> parseFile(String javaSourceText, String fileName, String relativePath) { ASTParser parser = parserCache.get(); parser.setSource(javaSourceText.toCharArray()); parser.setResolveBindings(true); parser.setEnvironment(new String[]{}, SOURCE_PATH, SOURCE_ENCODING, true); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setCompilerOptions(JavaCore.getOptions()); parser.setUnitName(fileName); CompilationUnit cu = (CompilationUnit) parser.createAST(null); JavadocVisitor visitor = new JavadocVisitor(fileName, relativePath, javaSourceText); cu.accept(visitor); return visitor.getJavaDocs(); }
package com.github.igorsuhorukov.javadoc.parser; import com.github.igorsuhorukov.javadoc.model.*; import com.github.igorsuhorukov.javadoc.model.Type; import org.eclipse.jdt.core.dom.*; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; public class JavadocVisitor extends ASTVisitor { private String file; private String relativePath; private String sourceText; private CompilationUnit compilationUnit; private String packageName; private List<? extends Comment> commentList; private List<JavaDoc> javaDocs = new ArrayList<>(); public JavadocVisitor(String file, String relativePath, String sourceText) { this.file = file; this.relativePath = relativePath; this.sourceText = sourceText; } @Override public boolean visit(PackageDeclaration node) { packageName = node.getName().getFullyQualifiedName(); javaDocs.addAll(getTypes().stream().map(astTypeNode -> { JavaDoc javaDoc = getJavaDoc(astTypeNode); Type type = getType(astTypeNode); type.setUnitInfo(getUnitInfo()); javaDoc.setSourcePoint(type); return javaDoc; }).collect(Collectors.toList())); javaDocs.addAll(getMethods().stream().map(astMethodNode -> { JavaDoc javaDoc = getJavaDoc(astMethodNode); Method method = new Method(); method.setUnitInfo(getUnitInfo()); method.setName(astMethodNode.getName().getFullyQualifiedName()); method.setConstructor(astMethodNode.isConstructor()); fillMethodDeclaration(astMethodNode, method); Type type = getType((AbstractTypeDeclaration) astMethodNode.getParent()); method.setType(type); javaDoc.setSourcePoint(method); return javaDoc; }).collect(Collectors.toList())); return super.visit(node); } private CompilationUnitInfo getUnitInfo() { return new CompilationUnitInfo(packageName, relativePath, file); } @SuppressWarnings("unchecked") private void fillMethodDeclaration(MethodDeclaration methodAstNode, Method method) { List<SingleVariableDeclaration> parameters = methodAstNode.parameters(); org.eclipse.jdt.core.dom.Type returnType2 = methodAstNode.getReturnType2(); method.setParams(parameters.stream().map(param -> param.getType().toString()).collect(Collectors.toList())); if(returnType2!=null) { method.setReturnType(returnType2.toString()); } } private Type getType(AbstractTypeDeclaration astNode) { String binaryName = astNode.resolveBinding().getBinaryName(); Type type = new Type(); type.setName(binaryName); return type; } @SuppressWarnings("unchecked") private JavaDoc getJavaDoc(BodyDeclaration astNode) { JavaDoc javaDoc = new JavaDoc(); Javadoc javadoc = astNode.getJavadoc(); List<TagElement> tags = javadoc.tags(); Optional<TagElement> comment = tags.stream().filter(tag -> tag.getTagName() == null).findFirst(); comment.ifPresent(tagElement -> javaDoc.setComment(tagElement.toString().replace("\n *","").trim())); List<Tag> fragments = tags.stream().filter(tag -> tag.getTagName() != null).map(tag-> { Tag tagResult = new Tag(); tagResult.setName(tag.getTagName()); tagResult.setFragments(getTags(tag.fragments())); return tagResult; }).collect(Collectors.toList()); javaDoc.setTags(fragments); return javaDoc; } @SuppressWarnings("unchecked") private List<String> getTags(List fragments){ return ((List<IDocElement>)fragments).stream().map(Objects::toString).collect(Collectors.toList()); } private List<AbstractTypeDeclaration> getTypes() { return commentList.stream().map(ASTNode::getParent).filter(Objects::nonNull).filter(AbstractTypeDeclaration.class::isInstance).map(astNode -> (AbstractTypeDeclaration) astNode).collect(Collectors.toList()); } private List<MethodDeclaration> getMethods() { return commentList.stream().map(ASTNode::getParent).filter(Objects::nonNull).filter(MethodDeclaration.class::isInstance).map(astNode -> (MethodDeclaration) astNode).collect(Collectors.toList()); } @Override @SuppressWarnings("unchecked") public boolean visit(CompilationUnit node) { commentList = node.getCommentList(); this.compilationUnit = node; return super.visit(node); } public List<JavaDoc> getJavaDocs() { return javaDocs; } }
<profile> <id>extract-javadoc</id> <activation> <file> <exists>${basedir}/src/main/java</exists> </file> </activation> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <executions> <execution> <id>extract-javadoc</id> <phase>package</phase> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <includeProjectDependencies>true</includeProjectDependencies> <includePluginDependencies>true</includePluginDependencies> <mainClass>com.github.igorsuhorukov.javadoc.ExtractJavadocModel</mainClass> <arguments> <argument>${project.basedir}/src</argument> <argument>${project.build.directory}/javadoc.json.xz</argument> </arguments> </configuration> <dependencies> <dependency> <groupId>com.github.igor-suhorukov</groupId> <artifactId>extract-javadoc</artifactId> <version>1.0</version> <type>jar</type> <scope>compile</scope> </dependency> </dependencies> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>attach-extracted-javadoc</id> <phase>package</phase> <goals> <goal>attach-artifact</goal> </goals> <configuration> <artifacts> <artifact> <file>${project.build.directory}/javadoc.json.xz</file> <type>xz</type> <classifier>javadoc</classifier> </artifact> </artifacts> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile>
git clone https://github.com/spring-projects/spring-boot.git
<profile> <id>extract-javadoc</id> <activation> <file> <exists>${basedir}/src/main/java</exists> </file> </activation> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <executions> <execution> <id>extract-javadoc</id> <phase>package</phase> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <includeProjectDependencies>true</includeProjectDependencies> <includePluginDependencies>true</includePluginDependencies> <mainClass>com.github.igorsuhorukov.javadoc.ExtractJavadocModel</mainClass> <arguments> <argument>${project.basedir}/src</argument> <argument>${project.build.directory}/javadoc.json</argument> </arguments> </configuration> <dependencies> <dependency> <groupId>com.github.igor-suhorukov</groupId> <artifactId>extract-javadoc</artifactId> <version>1.0</version> <type>jar</type> <scope>compile</scope> </dependency> </dependencies> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>attach-extracted-javadoc</id> <phase>package</phase> <goals> <goal>attach-artifact</goal> </goals> <configuration> <artifacts> <artifact> <file>${project.build.directory}/javadoc.json</file> <type>json</type> <classifier>javadoc</classifier> </artifact> </artifacts> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile>
[ { "comment" : "Ant task to find a main class.", "tags" : [ { "name" : "@author", "fragments" : [ " Matt Benson" ] }, { "name" : "@since", "fragments" : [ " 1.3.0" ] } ], "sourcePoint" : { "@type" : "Type", "unitInfo" : { "packageName" : "org.springframework.boot.ant", "relativePath" : "main/java/org/springframework/boot/ant", "file" : "FindMainClass.java" }, "name" : "org.springframework.boot.ant.FindMainClass" } }, { "comment" : "Set the main class, which will cause the search to be bypassed.", "tags" : [ { "name" : "@param", "fragments" : [ "mainClass", " the main class name" ] } ], "sourcePoint" : { "@type" : "Method", "unitInfo" : { "packageName" : "org.springframework.boot.ant", "relativePath" : "main/java/org/springframework/boot/ant", "file" : "FindMainClass.java" }, "type" : { "@type" : "Type", "unitInfo" : null, "name" : "org.springframework.boot.ant.FindMainClass" }, "name" : "setMainClass", "constructor" : false, "params" : [ "String" ], "returnType" : "void" } }, { "comment" : "Set the root location of classes to be searched.", "tags" : [ { "name" : "@param", "fragments" : [ "classesRoot", " the root location" ] } ], "sourcePoint" : { "@type" : "Method", "unitInfo" : { "packageName" : "org.springframework.boot.ant", "relativePath" : "main/java/org/springframework/boot/ant", "file" : "FindMainClass.java" }, "type" : { "@type" : "Type", "unitInfo" : null, "name" : "org.springframework.boot.ant.FindMainClass" }, "name" : "setClassesRoot", "constructor" : false, "params" : [ "File" ], "returnType" : "void" } }, { "comment" : "Set the ANT property to set (if left unset, result will be printed to the log).", "tags" : [ { "name" : "@param", "fragments" : [ "property", " the ANT property to set" ] } ], "sourcePoint" : { "@type" : "Method", "unitInfo" : { "packageName" : "org.springframework.boot.ant", "relativePath" : "main/java/org/springframework/boot/ant", "file" : "FindMainClass.java" }, "type" : { "@type" : "Type", "unitInfo" : null, "name" : "org.springframework.boot.ant.FindMainClass" }, "name" : "setProperty", "constructor" : false, "params" : [ "String" ], "returnType" : "void" } }, { "comment" : "Quiet task that establishes a reference to its loader.", "tags" : [ { "name" : "@author", "fragments" : [ " Matt Benson" ] }, { "name" : "@since", "fragments" : [ " 1.3.0" ] } ], "sourcePoint" : { "@type" : "Type", "unitInfo" : { "packageName" : "org.springframework.boot.ant", "relativePath" : "main/java/org/springframework/boot/ant", "file" : "ShareAntlibLoader.java" }, "name" : "org.springframework.boot.ant.ShareAntlibLoader" } } ]
Source: https://habr.com/ru/post/336732/
All Articles