Interface methods can be private now

This commit is contained in:
Andreas Dangel
2017-09-22 15:22:46 +02:00
parent 672f9216dd
commit fa44c405bd
2 changed files with 22 additions and 1 deletions

View File

@ -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();

View File

@ -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());
}
}