Skip to content

Commit

Permalink
Merge pull request #703 from shalousun/master
Browse files Browse the repository at this point in the history
Support for Java Record
  • Loading branch information
shalousun authored Jan 13, 2024
2 parents 97565b0 + 908723c commit 9638ba4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>com.ly.smart-doc</groupId>
<artifactId>qdox</artifactId>
<version>2.0.3.3</version>
<version>2.0.3.4</version>
</dependency>
<dependency>
<groupId>net.datafaker</groupId>
Expand All @@ -71,7 +71,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.7</version>
<version>2.0.11</version>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
Expand Down
38 changes: 28 additions & 10 deletions src/main/java/com/ly/doc/utils/DocUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public static boolean isMatch(String packageFilters, String controllerName) {
/**
* match the controller package
*
* @param packageFilters package filter
* @param packageFilters package filter
* @param controllerClass controller class
* @return boolean
*/
Expand All @@ -256,7 +256,7 @@ public static boolean isMatch(String packageFilters, JavaClass controllerClass)
// if the 'packageFilters' is point to the method, add all candidate methods into this container
int capacity = Math.max((int) (controllerClass.getMethods().size() / 0.75F) + 1, 16);
Set<String> filterMethods = new HashSet<>(capacity);

String[] filters = packageFilters.split(",");

for (String filter : filters) {
Expand Down Expand Up @@ -300,7 +300,7 @@ public static boolean isMatch(String packageFilters, JavaClass controllerClass)
cacheFilterMethods(controllerName, filterMethods);
return true;
}

return false;
}

Expand All @@ -324,7 +324,7 @@ private static Pattern getPattern(String regex) {
* Put the specified method names into a cache.
*
* @param controller the controller canonical name
* @param methods the methods will be cached
* @param methods the methods will be cached
*/
private static void cacheFilterMethods(String controller, Set<String> methods) {
filterMethodCache.put(controller, methods);
Expand Down Expand Up @@ -372,7 +372,7 @@ public static String formatAndRemove(String str, Map<String, String> values) {
List<String> finalPaths = new ArrayList<>(pathList.size());
for (String pathParam : pathList) {
if (pathParam.startsWith("http:") || pathParam.startsWith("https:")) {
finalPaths.add(pathParam+"/");
finalPaths.add(pathParam + "/");
continue;
}
if (pathParam.startsWith("${")) {
Expand Down Expand Up @@ -548,13 +548,32 @@ public static List<String> split(String url) {
*/
public static Map<String, String> getCommentsByTag(final JavaMethod javaMethod, final String tagName, final String className) {
List<DocletTag> paramTags = javaMethod.getTagsByName(tagName);
String tagValNullMsg = "ERROR: #" + javaMethod.getName()
+ "() - bad @" + tagName + " javadoc from " + javaMethod.getDeclaringClass()
.getCanonicalName() + ", This is an invalid comment.";
String tagValErrorMsg = "ERROR: An invalid comment was written [@" + tagName + " |]," +
"Please @see " + javaMethod.getDeclaringClass().getCanonicalName() + "." + javaMethod.getName() + "()";
return getCommentsByTag(paramTags, tagName, className, tagValNullMsg, tagValErrorMsg);
}

public static Map<String, String> getRecordCommentsByTag(JavaClass javaClass, final String tagName) {
List<DocletTag> paramTags = javaClass.getTagsByName(tagName);
String className = javaClass.getCanonicalName();
String tagValNullMsg = "ERROR: "
+ "Bad @" + tagName + " javadoc from " + className
+ ", This is an invalid comment.";
String tagValErrorMsg = "ERROR: An invalid comment was written [@" + tagName + " |]," +
"Please @see " + className;
return getCommentsByTag(paramTags, tagName, className, tagValNullMsg, tagValErrorMsg);
}

private static Map<String, String> getCommentsByTag(List<DocletTag> paramTags, final String tagName, String className,
String tagValNullMsg, String tagValErrorMsg) {
Map<String, String> paramTagMap = new HashMap<>();
for (DocletTag docletTag : paramTags) {
String value = docletTag.getValue();
if (StringUtil.isEmpty(value) && StringUtil.isNotEmpty(className)) {
throw new RuntimeException("ERROR: #" + javaMethod.getName()
+ "() - bad @" + tagName + " javadoc from " + javaMethod.getDeclaringClass()
.getCanonicalName() + ", This is an invalid comment.");
throw new RuntimeException(tagValNullMsg);
}
if (DocTags.PARAM.equals(tagName)) {
String pName = value;
Expand All @@ -566,8 +585,7 @@ public static Map<String, String> getCommentsByTag(final JavaMethod javaMethod,
pValue = value.substring(idx + 1);
}
if ("|".equals(StringUtil.trim(pValue)) && StringUtil.isNotEmpty(className)) {
throw new RuntimeException("ERROR: An invalid comment was written [@" + tagName + " |]," +
"Please @see " + javaMethod.getDeclaringClass().getCanonicalName() + "." + javaMethod.getName() + "()");
throw new RuntimeException(tagValErrorMsg);
}
paramTagMap.put(pName, pValue);
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/ly/doc/utils/JavaClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ private static List<DocJavaField> getFields(JavaClass cls1, int counter, Map<Str
}
}
if (!cls1.isInterface()) {
Map<String,String> recordComments = new HashMap<>(0);
if (cls1.isRecord()) {
recordComments = DocUtil.getRecordCommentsByTag(cls1,DocTags.PARAM);
}
for (JavaField javaField : cls1.getFields()) {
String fieldName = javaField.getName();
String subTypeName = javaField.getType().getFullyQualifiedName();
Expand Down Expand Up @@ -228,6 +232,9 @@ private static List<DocJavaField> getFields(JavaClass cls1, int counter, Map<Str
docJavaField.setEnum(true);
}
String comment = javaField.getComment();
if (cls1.isRecord()) {
comment = recordComments.get(fieldName);
}
if (Objects.isNull(comment)) {
comment = DocGlobalConstants.NO_COMMENTS_FOUND;
}
Expand Down

0 comments on commit 9638ba4

Please sign in to comment.