Repository: checkstyle/checkstyle

Problem statement:
MissingJavadocMethod flagging private methods even target scope is public methods only
https://checkstyle.sourceforge.io/config_javadoc.html#MissingJavadocMethod

```
$ javac CheckstylePrivateMethodTest.java
$ 
```
```
$ cat config.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name = "Checker">
    <module name="TreeWalker">
        <module name="MissingJavadocMethod">
            <property name="scope" value="public"/>
            <property name="minLineCount" value="2"/>
            <property name="tokens" value="METHOD_DEF"/>
        </module>
    </module>
</module>
```

```
$ cat CheckstylePrivateMethodTest.java
public interface CheckstylePrivateMethodTest {
  private static void whyIsMissingJavadocMethodBeingFlagged() {
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
  }
}
```

```
$ java -jar checkstyle-8.32-all.jar -c config.xml CheckstylePrivateMethodTest.java
Starting audit...
[ERROR] CheckstylePrivateMethodTest.java:2:3: Missing a Javadoc comment. [MissingJavadocMethod]
Audit done.
Checkstyle ends with 1 errors.

```
---------------
Expected - no violation.

When an interface has a private static method that goes beyond the line limit, it gets flagged as needing a javadoc comment, even though private methods are supposed to be exempt.

--------------




Hints:
@ChrisTrenkamp Please correct your config example to only one failing check.
Issue is in line 
https://github.com/checkstyle/checkstyle/blob/6275303e6b5fd4d5e9a6504b390efd0e4aee564d/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocMethodCheck.java#L453-L455
interfaces can have private methods since Java 9
So it looks like a bug
> 
> 
> @ChrisTrenkamp Please correct your config example to only one failing check.

Done.

Patch:
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocMethodCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocMethodCheck.java
--- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocMethodCheck.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocMethodCheck.java
@@ -450,12 +450,18 @@ private boolean shouldCheck(final DetailAST ast, final Scope nodeScope) {
     private static Scope calculateScope(final DetailAST ast) {
         final Scope scope;
 
-        if (ScopeUtil.isInInterfaceOrAnnotationBlock(ast)) {
+        if (ScopeUtil.isInAnnotationBlock(ast)) {
             scope = Scope.PUBLIC;
         }
         else {
             final DetailAST mods = ast.findFirstToken(TokenTypes.MODIFIERS);
-            scope = ScopeUtil.getScopeFromMods(mods);
+            final Scope modifiersScope = ScopeUtil.getScopeFromMods(mods);
+            if (modifiersScope == Scope.PACKAGE && ScopeUtil.isInInterfaceBlock(ast)) {
+                scope = Scope.PUBLIC;
+            }
+            else {
+                scope = modifiersScope;
+            }
         }
         return scope;
     }


Test patch:
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocMethodCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocMethodCheckTest.java
--- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocMethodCheckTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocMethodCheckTest.java
@@ -463,4 +463,29 @@ public void testConstructor() throws Exception {
         };
         verify(checkConfig, getPath("InputMissingJavadocMethodConstructor.java"), expected);
     }
+
+    @Test
+    public void testNotPublicInterfaceMethods() throws Exception {
+        final DefaultConfiguration checkConfig = createModuleConfig(
+                MissingJavadocMethodCheck.class);
+        checkConfig.addAttribute("scope", "public");
+        final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
+        verify(checkConfig, getNonCompilablePath(
+            "InputMissingJavadocMethodInterfacePrivateMethod.java"), expected);
+    }
+
+    @Test
+    public void testPublicMethods() throws Exception {
+        final DefaultConfiguration checkConfig = createModuleConfig(
+                MissingJavadocMethodCheck.class);
+        final String[] expected = {
+            "9:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
+            "11:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
+            "15:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
+            "18:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
+            "22:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
+        };
+        verify(checkConfig, getPath("InputMissingJavadocMethodPublicMethods.java"), expected);
+
+    }
 }
diff --git a/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/javadoc/missingjavadocmethod/InputMissingJavadocMethodInterfacePrivateMethod.java b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/javadoc/missingjavadocmethod/InputMissingJavadocMethodInterfacePrivateMethod.java
new file mode 100644
--- /dev/null
+++ b/src/test/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/javadoc/missingjavadocmethod/InputMissingJavadocMethodInterfacePrivateMethod.java
@@ -0,0 +1,11 @@
+//non-compiled with javac: Compilable with Java9
+// private method in interface
+
+package com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadocmethod;
+
+public interface InputMissingJavadocMethodInterfacePrivateMethod {
+
+    private static void myPrivateMethod() {
+
+    }
+}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/missingjavadocmethod/InputMissingJavadocMethodPublicMethods.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/missingjavadocmethod/InputMissingJavadocMethodPublicMethods.java
new file mode 100644
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/missingjavadocmethod/InputMissingJavadocMethodPublicMethods.java
@@ -0,0 +1,58 @@
+package com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadocmethod;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Target;
+
+/* Config = default */
+public class InputMissingJavadocMethodPublicMethods {
+
+    public void annotation(final char @SomeAnnotation [] arg) {}
+
+    public @SomeAnnotation String @SomeAnnotation [] annotationInSignature() {
+        return new String[]{};
+    }
+
+    public static <T> T[] typeInSignature(T[] array) {
+        return null;
+    }
+    public static <T> T[][] typeInSignature2(T[][] array) {
+        return null;
+    }
+
+    public static void main(String[] args) throws Exception {}
+
+    /**
+     * Some javadoc
+     */
+    public void annotation1(final char @SomeAnnotation [] arg) {}
+
+    /**
+     * Some javadoc
+     */
+    public @SomeAnnotation String @SomeAnnotation [] annotationInSignature1() {
+        return new String[]{};
+    }
+
+    /**
+     * Some javadoc
+     */
+    public static <T> T[] typeInSignature3(T[] array) {
+        return null;
+    }
+
+    /**
+     * Some javadoc
+     */
+    public static <T> T[][] typeInSignature4(T[][] array) {
+        return null;
+    }
+
+    /**
+     * Some javadoc
+     */
+    public static void mainWithArgs(String[] args) throws Exception {}
+
+    @Target(ElementType.TYPE_USE)
+    @interface SomeAnnotation {
+    }
+}