Skip to content

Commit

Permalink
SONARJAVA-1377 Handle duplicated elements from javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Wohops committed Jun 28, 2017
1 parent 8175eb6 commit 33a97af
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ private static class Javadoc {
private static final Set<String> PLACEHOLDERS = ImmutableSet.of("TODO", "FIXME", "...", ".");

private final String mainDescription;
private final Map<String, String> parameters;
private final Map<String, String> thrownExceptions;
private final Map<String, List<String>> parameters;
private final Map<String, List<String>> thrownExceptions;
private final String returnDescription;

Javadoc(Tree tree) {
Expand Down Expand Up @@ -312,8 +312,12 @@ public List<String> undocumentedThrownExceptions(Tree tree) {
return getUndocumentedElements(getExceptions(tree), thrownExceptions);
}

private static List<String> getUndocumentedElements(List<String> elementNames, Map<String, String> elementsWithDescription) {
return elementNames.stream().filter(name -> isEmptyDescription(elementsWithDescription.get(name))).collect(Collectors.toList());
private static List<String> getUndocumentedElements(List<String> elementNames, Map<String, List<String>> elementsWithDescriptions) {
return elementNames.stream().filter(name -> isEmptyDescription(elementsWithDescriptions.get(name))).collect(Collectors.toList());
}

private static boolean isEmptyDescription(@Nullable List<String> descriptions) {
return descriptions == null || descriptions.stream().anyMatch(Javadoc::isEmptyDescription);
}

private static boolean isEmptyDescription(@Nullable String part) {
Expand Down Expand Up @@ -392,13 +396,13 @@ private static String extractMainDescription(List<String> lines) {
return sb.toString().trim();
}

private static Map<String, String> extractToMap(List<String> lines, Pattern pattern) {
private static Map<String, List<String>> extractToMap(List<String> lines, Pattern pattern) {
return lines.stream()
.map(pattern::matcher)
.filter(Matcher::matches)
.collect(Collectors.toMap(
.collect(Collectors.groupingBy(
matcher -> matcher.group("name"),
matcher -> matcher.group("descr")));
Collectors.mapping(matcher -> matcher.group("descr"), Collectors.toList())));
}

private static String extractReturnDescription(List<String> lines) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void foo(String s, Object o) { } // Noncompliant {{Document the parameter
/**
* This is documented
* @param o
* @param o FIXME
*/
public void foo(Object o) { } // Noncompliant {{Document the parameter(s): o}} - undocumented

Expand Down Expand Up @@ -113,9 +114,24 @@ public void toi() throws MyException { } // Noncompliant {{Document this method
/**
* This is documented
* @throws MyException FIXME
* @throws MyException when it does not like you
*/
public void tou() throws MyException { } // Noncompliant {{Document this method thrown exception(s): MyException}}

/**
* This is documented
* @throws MyException when it does not like you
* @throws MyException FIXME
*/
public void tul() throws MyException { } // Noncompliant {{Document this method thrown exception(s): MyException}}

/**
* This is documented
* @throws MyException TODO
* @throws MyException FIXME
*/
public void tac() throws MyException { } // Noncompliant {{Document this method thrown exception(s): MyException}}

/**
* This is documented
* @throws MyException when it does not like you
Expand Down

0 comments on commit 33a97af

Please sign in to comment.