From 33a97af78d11853d9df03f754480681bde0845ac Mon Sep 17 00:00:00 2001 From: Michael Gumowski Date: Wed, 28 Jun 2017 10:53:54 +0200 Subject: [PATCH] SONARJAVA-1377 Handle duplicated elements from javadoc --- .../java/checks/UndocumentedApiCheck.java | 18 +++++++++++------- .../UndocumentedApiIncomplete.java | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/java-checks/src/main/java/org/sonar/java/checks/UndocumentedApiCheck.java b/java-checks/src/main/java/org/sonar/java/checks/UndocumentedApiCheck.java index 28fa936e17e..4dfc48a2a6b 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/UndocumentedApiCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/UndocumentedApiCheck.java @@ -282,8 +282,8 @@ private static class Javadoc { private static final Set PLACEHOLDERS = ImmutableSet.of("TODO", "FIXME", "...", "."); private final String mainDescription; - private final Map parameters; - private final Map thrownExceptions; + private final Map> parameters; + private final Map> thrownExceptions; private final String returnDescription; Javadoc(Tree tree) { @@ -312,8 +312,12 @@ public List undocumentedThrownExceptions(Tree tree) { return getUndocumentedElements(getExceptions(tree), thrownExceptions); } - private static List getUndocumentedElements(List elementNames, Map elementsWithDescription) { - return elementNames.stream().filter(name -> isEmptyDescription(elementsWithDescription.get(name))).collect(Collectors.toList()); + private static List getUndocumentedElements(List elementNames, Map> elementsWithDescriptions) { + return elementNames.stream().filter(name -> isEmptyDescription(elementsWithDescriptions.get(name))).collect(Collectors.toList()); + } + + private static boolean isEmptyDescription(@Nullable List descriptions) { + return descriptions == null || descriptions.stream().anyMatch(Javadoc::isEmptyDescription); } private static boolean isEmptyDescription(@Nullable String part) { @@ -392,13 +396,13 @@ private static String extractMainDescription(List lines) { return sb.toString().trim(); } - private static Map extractToMap(List lines, Pattern pattern) { + private static Map> extractToMap(List 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 lines) { diff --git a/java-checks/src/test/files/checks/UndocumentedApiCheck/UndocumentedApiIncomplete.java b/java-checks/src/test/files/checks/UndocumentedApiCheck/UndocumentedApiIncomplete.java index 83578ed37d5..af68248d093 100644 --- a/java-checks/src/test/files/checks/UndocumentedApiCheck/UndocumentedApiIncomplete.java +++ b/java-checks/src/test/files/checks/UndocumentedApiCheck/UndocumentedApiIncomplete.java @@ -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 @@ -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