add method of obtain params comments

This commit is contained in:
oppofind 2019-10-13 23:53:38 +08:00
parent f00dafd1b0
commit 360aabe9e7
2 changed files with 36 additions and 0 deletions

View File

@ -79,4 +79,6 @@ public class DocGlobalConstants {
public static final String FIELD_SPACE = "     ";
public static final String ANY_OBJECT_MSG = "any object.";
public static final String NO_COMMENTS_FOUND = "No comments found.";
}

View File

@ -6,7 +6,9 @@ import com.power.common.util.IDCardUtil;
import com.power.common.util.RandomUtil;
import com.power.common.util.StringUtil;
import com.power.doc.constants.DocGlobalConstants;
import com.thoughtworks.qdox.model.DocletTag;
import com.thoughtworks.qdox.model.JavaAnnotation;
import com.thoughtworks.qdox.model.JavaMethod;
import java.util.*;
@ -254,4 +256,36 @@ public class DocUtil {
return annotation.getNamedParameter("value").toString();
}
}
/**
* obtain params comments
* @param javaMethod JavaMethod
* @param tagName java comments tag
* @param className class name
* @return
*/
public static Map<String,String> getParamsComments(final JavaMethod javaMethod, final String tagName, final String className) {
List<DocletTag> paramTags = javaMethod.getTagsByName(tagName);
Map<String, String> paramTagMap = new HashMap<>();
for (DocletTag docletTag : paramTags) {
String value = docletTag.getValue();
if (StringUtil.isEmpty(value)) {
throw new RuntimeException("ERROR: #" + javaMethod.getName()
+ "() - bad @param javadoc from " + className);
}
String pName;
String pValue;
int idx = value.indexOf("\n");
//existed \n
if (idx > -1) {
pName = value.substring(0, idx);
pValue = value.substring(idx + 1);
} else {
pName = (value.indexOf(" ") > -1) ? value.substring(0, value.indexOf(" ")) : value;
pValue = value.indexOf(" ") > -1 ? value.substring(value.indexOf(' ') + 1) : DocGlobalConstants.NO_COMMENTS_FOUND;
}
paramTagMap.put(pName, pValue);
}
return paramTagMap;
}
}