This is an automated email from the ASF dual-hosted git repository. sunlan pushed a commit to branch refine-groovydoc in repository https://gitbox.apache.org/repos/asf/groovy.git
commit b40dffc3e8310075d58b76d796f169ea920fc4fd Author: Daniel Sun <sun...@apache.org> AuthorDate: Sat Dec 29 22:59:51 2018 +0800 Initial commit: refine groovydoc with javaparser --- .../org/codehaus/groovy/antlr/SourceBuffer.java | 4 + subprojects/groovy-groovydoc/build.gradle | 6 + .../groovydoc/SimpleJavaClassDocAssembler.java | 130 +++++++++++++++++++++ .../SimpleJavaClassDocAssemblerTest.groovy} | 21 ++-- .../SimpleJavaClassDocAssemblerTest1.java | 84 +++++++++++++ 5 files changed, 232 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/codehaus/groovy/antlr/SourceBuffer.java b/src/main/java/org/codehaus/groovy/antlr/SourceBuffer.java index 558370f..87add9c 100644 --- a/src/main/java/org/codehaus/groovy/antlr/SourceBuffer.java +++ b/src/main/java/org/codehaus/groovy/antlr/SourceBuffer.java @@ -108,4 +108,8 @@ public class SourceBuffer { lines.add(current); } } + + public String getContent() { + return String.join("", lines); + } } diff --git a/subprojects/groovy-groovydoc/build.gradle b/subprojects/groovy-groovydoc/build.gradle index 2e9f98c..43f01c7 100644 --- a/subprojects/groovy-groovydoc/build.gradle +++ b/subprojects/groovy-groovydoc/build.gradle @@ -16,8 +16,14 @@ * specific language governing permissions and limitations * under the License. */ + +ext { + javaparserVersion = '3.8.3' +} + dependencies { compile rootProject + compile "com.github.javaparser:javaparser-symbol-solver-core:$javaparserVersion" testCompile rootProject.sourceSets.test.runtimeClasspath compile project(':groovy-cli-picocli') compile project(':groovy-templates') diff --git a/subprojects/groovy-groovydoc/src/main/java/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssembler.java b/subprojects/groovy-groovydoc/src/main/java/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssembler.java new file mode 100644 index 0000000..49579b9 --- /dev/null +++ b/subprojects/groovy-groovydoc/src/main/java/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssembler.java @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.groovy.tools.groovydoc; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ast.body.AnnotationDeclaration; +import com.github.javaparser.ast.body.AnnotationMemberDeclaration; +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; +import com.github.javaparser.ast.body.ConstructorDeclaration; +import com.github.javaparser.ast.body.EnumConstantDeclaration; +import com.github.javaparser.ast.body.EnumDeclaration; +import com.github.javaparser.ast.body.FieldDeclaration; +import com.github.javaparser.ast.body.MethodDeclaration; +import com.github.javaparser.ast.nodeTypes.NodeWithJavadoc; +import com.github.javaparser.ast.visitor.GenericVisitorAdapter; +import com.github.javaparser.javadoc.Javadoc; +import org.codehaus.groovy.groovydoc.GroovyClassDoc; +import org.codehaus.groovy.tools.groovydoc.LinkArgument; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Properties; + +public class SimpleJavaClassDocAssembler extends GenericVisitorAdapter<Object, Object> { + private final String packagePath; + private final String javaSourceContent; + private final String className; + private final List<LinkArgument> links; + private final Properties properties; + private final Map<String, GroovyClassDoc> classDocs = new LinkedHashMap<>(); + + public SimpleJavaClassDocAssembler(String packagePath, String file, String javaSourceContent, List<LinkArgument> links, Properties properties) { + this.packagePath = packagePath; + this.javaSourceContent = javaSourceContent; + this.links = links; + this.properties = properties; + + if (file != null && file.contains(".")) { + // todo: replace this simple idea of default class name + int idx = file.lastIndexOf("."); + className = file.substring(0, idx); + } else { + className = file; + } + } + + public Map<String, GroovyClassDoc> getGroovyClassDocs() { + return classDocs; + } + + public void assemble() { + this.visit(JavaParser.parse(javaSourceContent), null); + } + + @Override + public Object visit(final ClassOrInterfaceDeclaration n, final Object arg) { + processJavadoc(n); + return super.visit(n, arg); + } + + @Override + public Object visit(final MethodDeclaration n, final Object arg) { + processJavadoc(n); + return super.visit(n, arg); + } + + @Override + public Object visit(final ConstructorDeclaration n, final Object arg) { + processJavadoc(n); + return super.visit(n, arg); + } + + @Override + public Object visit(final FieldDeclaration n, final Object arg) { + processJavadoc(n); + return super.visit(n, arg); + } + + @Override + public Object visit(final EnumDeclaration n, final Object arg) { + processJavadoc(n); + return super.visit(n, arg); + } + + @Override + public Object visit(final EnumConstantDeclaration n, final Object arg) { + processJavadoc(n); + return super.visit(n, arg); + } + + @Override + public Object visit(final AnnotationDeclaration n, final Object arg) { + processJavadoc(n); + return super.visit(n, arg); + } + + @Override + public Object visit(final AnnotationMemberDeclaration n, final Object arg) { + processJavadoc(n); + return super.visit(n, arg); + } + + private void processJavadoc(NodeWithJavadoc n) { + Optional<Javadoc> optionalJavadoc = n.getJavadoc(); + + if (!optionalJavadoc.isPresent()) { + return; + } + + System.out.println(optionalJavadoc.get().getDescription().toText()); + } +} \ No newline at end of file diff --git a/subprojects/groovy-groovydoc/build.gradle b/subprojects/groovy-groovydoc/src/test/groovy/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssemblerTest.groovy similarity index 64% copy from subprojects/groovy-groovydoc/build.gradle copy to subprojects/groovy-groovydoc/src/test/groovy/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssemblerTest.groovy index 2e9f98c..9f96175 100644 --- a/subprojects/groovy-groovydoc/build.gradle +++ b/subprojects/groovy-groovydoc/src/test/groovy/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssemblerTest.groovy @@ -16,18 +16,13 @@ * specific language governing permissions and limitations * under the License. */ -dependencies { - compile rootProject - testCompile rootProject.sourceSets.test.runtimeClasspath - compile project(':groovy-cli-picocli') - compile project(':groovy-templates') - runtime project(':groovy-docgenerator') - testCompile project(':groovy-test') - testCompile "org.apache.ant:ant-testutil:$antVersion" -} +package org.apache.groovy.tools.groovydoc -compileJava { - doLast { - mkdir "$sourceSets.main.java.outputDir/META-INF" +class SimpleJavaClassDocAssemblerTest extends GroovyTestCase { + void testClassDoc() { + def src = SimpleJavaClassDocAssemblerTest.getResourceAsStream('/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssemblerTest1.java').text + def sjcda = new SimpleJavaClassDocAssembler('org/apache/groovy/tools/groovydoc', 'SimpleJavaClassDocAssemblerTest1.java', src, [], new Properties()) + sjcda.assemble() } -} + +} \ No newline at end of file diff --git a/subprojects/groovy-groovydoc/src/test/resources/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssemblerTest1.java b/subprojects/groovy-groovydoc/src/test/resources/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssemblerTest1.java new file mode 100644 index 0000000..027991c --- /dev/null +++ b/subprojects/groovy-groovydoc/src/test/resources/org/apache/groovy/tools/groovydoc/SimpleJavaClassDocAssemblerTest1.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.groovy.tools.groovydoc; + +/** + * This a class doc + */ +public class SimpleJavaClassDocAssemblerTest1 { + /** + * The greeting word + */ + public static final String HELLO = "hello"; + + /** + * constructor of SimpleJavaClassDocAssemblerTest1 + */ + public SimpleJavaClassDocAssemblerTest1() { + } + + /** + * This an inner class doc + */ + public static class SomeInnerClass { + /** + * The greeting word of inner class + */ + public static final String INNER_HELLO = "hello"; + + /** + * constructor of SomeInnerClass + */ + public SomeInnerClass() {} + + /** + * say hello from inner class + * + * @param name some name + * @return the greeting words + */ + public String innerHello(String name) { + return HELLO + "," + name; + } + } + + /** + * say hello + * + * @param name some name + * @return the greeting words + */ + public String hello(String name) { + return HELLO + "," + name; + } +} + +/** + * some class + */ +class SomeClass { + +} + +/** + * some enum + */ +enum SomeEnum { + +}