Skip to content

Commit a3ef076

Browse files
cliviuZNT\lcarausu
authored andcommitted
GROOVY-11755: Support groovydoc for records (#2290)
* support groovydoc for records * added tests for record * add license header --------- Co-authored-by: ZNT\lcarausu <[email protected]> (cherry picked from commit 3fa8def)
1 parent 3007e02 commit a3ef076

File tree

4 files changed

+108
-7
lines changed

4 files changed

+108
-7
lines changed

subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/antlr4/GroovydocJavaVisitor.java

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
import com.github.javaparser.ast.body.AnnotationMemberDeclaration;
2626
import com.github.javaparser.ast.body.CallableDeclaration;
2727
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
28+
import com.github.javaparser.ast.body.CompactConstructorDeclaration;
2829
import com.github.javaparser.ast.body.ConstructorDeclaration;
2930
import com.github.javaparser.ast.body.EnumConstantDeclaration;
3031
import com.github.javaparser.ast.body.EnumDeclaration;
3132
import com.github.javaparser.ast.body.FieldDeclaration;
3233
import com.github.javaparser.ast.body.MethodDeclaration;
3334
import com.github.javaparser.ast.body.Parameter;
35+
import com.github.javaparser.ast.body.RecordDeclaration;
3436
import com.github.javaparser.ast.body.TypeDeclaration;
3537
import com.github.javaparser.ast.expr.AnnotationExpr;
3638
import com.github.javaparser.ast.expr.Name;
@@ -62,13 +64,14 @@
6264
import java.util.Map;
6365
import java.util.Optional;
6466

65-
public class GroovydocJavaVisitor extends VoidVisitorAdapter<Object> {
67+
public class GroovydocJavaVisitor
68+
extends VoidVisitorAdapter<Object> {
6669
private final List<LinkArgument> links;
6770
private SimpleGroovyClassDoc currentClassDoc = null;
68-
private Map<String, GroovyClassDoc> classDocs = new LinkedHashMap<>();
69-
private String packagePath;
71+
private final Map<String, GroovyClassDoc> classDocs = new LinkedHashMap<>();
72+
private final String packagePath;
7073
private final Map<String, String> aliases = new LinkedHashMap<>();
71-
private List<String> imports = new ArrayList<>();
74+
private final List<String> imports = new ArrayList<>();
7275
private static final String FS = "/";
7376

7477
public GroovydocJavaVisitor(String packagePath, List<LinkArgument> links) {
@@ -148,7 +151,7 @@ public void visit(AnnotationMemberDeclaration n, Object arg) {
148151
n.getJavadocComment().ifPresent(javadocComment ->
149152
fieldDoc.setRawCommentText(javadocComment.getContent()));
150153
n.getDefaultValue().ifPresent(defValue -> {
151-
fieldDoc.setRawCommentText(fieldDoc.getRawCommentText() + "\n* @default " + defValue.toString());
154+
fieldDoc.setRawCommentText(fieldDoc.getRawCommentText() + "\n* @default " + defValue);
152155
fieldDoc.setConstantValueExpression(defValue.toString());
153156
});
154157
super.visit(n, arg);
@@ -181,6 +184,26 @@ public void visit(ClassOrInterfaceDeclaration n, Object arg) {
181184
}
182185
}
183186

187+
@Override
188+
public void visit(final RecordDeclaration n, final Object arg) {
189+
SimpleGroovyClassDoc parent = visit(n);
190+
if (n.isRecordDeclaration()) {
191+
currentClassDoc.setTokenType(SimpleGroovyDoc.RECORD_DEF);
192+
}
193+
super.visit(n,arg);
194+
if (parent != null) {
195+
currentClassDoc = parent;
196+
}
197+
}
198+
199+
@Override
200+
public void visit(final CompactConstructorDeclaration c, Object arg) {
201+
SimpleGroovyConstructorDoc meth = new SimpleGroovyConstructorDoc(c.getNameAsString(), currentClassDoc);
202+
setCompactConstructor(c, meth);
203+
currentClassDoc.add(meth);
204+
super.visit(c, arg);
205+
}
206+
184207
private String fullName(ClassOrInterfaceType et) {
185208
StringBuilder name = new StringBuilder();
186209
et.getScope().ifPresent(sc -> name.append(sc.toString()));
@@ -303,6 +326,23 @@ private void setConstructorOrMethodCommon(CallableDeclaration<? extends Callable
303326
}
304327
}
305328

329+
private void setCompactConstructor(CompactConstructorDeclaration n, SimpleGroovyExecutableMemberDoc methOrCons) {
330+
n.getComment().ifPresent(javadocComment ->
331+
methOrCons.setRawCommentText(javadocComment.getContent()));
332+
NodeList<Modifier> mods = n.getModifiers();
333+
if (currentClassDoc.isInterface()) {
334+
mods.add(Modifier.publicModifier());
335+
}
336+
setModifiers(mods, methOrCons);
337+
processAnnotations(methOrCons, n);
338+
for (TypeParameter param : n.getTypeParameters()) {
339+
SimpleGroovyParameter p = new SimpleGroovyParameter(param.getNameAsString());
340+
processAnnotations(p, param);
341+
p.setType(makeType(param));
342+
methOrCons.add(p);
343+
}
344+
}
345+
306346
@Override
307347
public void visit(FieldDeclaration f, Object arg) {
308348
String name = f.getVariable(0).getNameAsString();

subprojects/groovy-groovydoc/src/test/groovy/org/codehaus/groovy/tools/groovydoc/GroovyDocTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void testSupportedJavadocVersion() throws Exception {
102102
}
103103

104104
@Test
105-
public void testUnsupportedJavadocVersion() throws Exception {
105+
public void testUnsupportedJavadocVersion() {
106106
rule.executeTarget("unsupportedGroovyDocJava");
107107

108108
final File testfilesPackageDir = new File(tmpDir, "org/codehaus/groovy/tools/groovydoc/testfiles/generics");
@@ -113,7 +113,7 @@ public void testUnsupportedJavadocVersion() throws Exception {
113113
}
114114

115115
@Test
116-
public void testInvalidJavaVersion() throws Exception {
116+
public void testInvalidJavaVersion() {
117117
try {
118118
rule.executeTarget("invalidJavaVersion");
119119
}
@@ -134,4 +134,19 @@ public void testFileEncoding() throws Exception {
134134

135135
assertEquals("The generated groovydoc must be in 'UTF-16LE' file encoding.'", StandardCharsets.UTF_16LE, charsetToolkit.getCharset());
136136
}
137+
138+
@Test
139+
public void testJavadocForRecords() throws Exception {
140+
rule.executeTarget("testJavadocForRecords");
141+
142+
final File testfilesPackageDir = new File(tmpDir, "org/codehaus/groovy/tools/groovydoc/testfiles/records");
143+
final String[] list = testfilesPackageDir.list((file, name) -> name.equals("Record.html"));
144+
145+
assertNotNull("Dir not found: " + testfilesPackageDir.getAbsolutePath(), list);
146+
assertEquals(1, list.length);
147+
File documentedClassHtmlDoc = new File(testfilesPackageDir, list[0]);
148+
149+
List<String> lines = ResourceGroovyMethods.readLines(documentedClassHtmlDoc);
150+
assertTrue("\"<title>Record</title>\" not in: " + lines, lines.contains("<title>Record</title>"));
151+
}
137152
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.codehaus.groovy.tools.groovydoc.testfiles.records;
20+
21+
/**
22+
* Javadoc for a record.
23+
*/
24+
public record Record(String recordName)
25+
{
26+
/**
27+
* Constructor of a record.
28+
* @param recordName The record name.
29+
*/
30+
public Record
31+
{
32+
}
33+
}

subprojects/groovy-groovydoc/src/test/resources/groovydoc/groovyDocTests.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,17 @@
9696
<link packages="org.codehaus.gmaven." href="http://groovy.github.io/gmaven/apidocs/"/>
9797
</groovydoc>
9898
</target>
99+
100+
<target name="testJavadocForRecords">
101+
<groovydoc destdir="${tmpdir}" sourcepath="${test}"
102+
packagenames="org/codehaus/groovy/tools/groovydoc/testfiles/**.*"
103+
use="true" windowtitle="GroovyDoc" private="false" javaVersion="JAVA_21">
104+
<link packages="java.,org.groovy.xml.,javax.,org.groovy.w3c." href="http://docs.oracle.com/javase/7/docs/api/"/>
105+
<link packages="org.apache.tools.ant." href="http://docs.groovy-lang.org/docs/ant/api/"/>
106+
<link packages="org.junit.,junit.framework." href="http://junit.org/junit4/javadoc/latest/"/>
107+
<link packages="groovy.,org.codehaus.groovy." href="http://groovy.codehaus.org/api/"/>
108+
<link packages="org.codehaus.gmaven." href="http://groovy.github.io/gmaven/apidocs/"/>
109+
</groovydoc>
110+
</target>
111+
99112
</project>

0 commit comments

Comments
 (0)