diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodDeclaration.java index f9e93ac83a..77edfe46ca 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTMethodDeclaration.java @@ -57,7 +57,8 @@ public class ASTMethodDeclaration extends AbstractJavaAccessNode implements DFAG @Override public boolean isPublic() { - if (isInterfaceMember()) { + // interface methods are public by default, but could be private since java9 + if (isInterfaceMember() && !isPrivate()) { return true; } return super.isPublic(); diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ASTMethodDeclarationTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ASTMethodDeclarationTest.java index b4c4cb6189..516f7739b7 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ASTMethodDeclarationTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ASTMethodDeclarationTest.java @@ -5,9 +5,13 @@ package net.sourceforge.pmd.lang.java.ast; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.Test; +import net.sourceforge.pmd.lang.java.ParserTstUtil; + public class ASTMethodDeclarationTest { @Test @@ -21,4 +25,20 @@ public class ASTMethodDeclarationTest { assertEquals("foo", md.getMethodName()); } + + @Test + public void testPrivateInterfaceMethods() { + ASTCompilationUnit node = ParserTstUtil.parseJava9("public interface Foo { private void bar() { } }"); + ASTMethodDeclaration methodDecl = node.getFirstDescendantOfType(ASTMethodDeclaration.class); + assertTrue(methodDecl.isPrivate()); + assertFalse(methodDecl.isPublic()); + } + + @Test + public void testPublicInterfaceMethods() { + ASTCompilationUnit node = ParserTstUtil.parseJava9("public interface Foo { void bar(); }"); + ASTMethodDeclaration methodDecl = node.getFirstDescendantOfType(ASTMethodDeclaration.class); + assertFalse(methodDecl.isPrivate()); + assertTrue(methodDecl.isPublic()); + } }