Fixes #793 [java] Parser error with private method in nested classes in interfaces

*   Remember old state to allow nesting
*   Fix ASTMethodDeclaration.isInterfaceMember
*   Extended tests
This commit is contained in:
Andreas Dangel
2017-12-22 12:03:20 +01:00
parent 64b862eef9
commit 109f458dbf
6 changed files with 110 additions and 39 deletions

View File

@ -343,7 +343,7 @@ public class JavaParser {
* Keeps track whether we are dealing with an interface or not. Needed since the tree is
* is not fully constructed yet, when we check for private interface methods.
* The flag is updated, if entering ClassOrInterfaceDeclaration and reset when leaving.
* The flag is also reset, if entering a anonymous inner class.
* The flag is also reset, if entering a anonymous inner class or enums.
*/
private boolean inInterface = false;
private void checkForBadPrivateInterfaceMethod(ASTMethodDeclaration node) {
@ -1417,6 +1417,7 @@ void ClassOrInterfaceDeclaration(int modifiers):
{
Token t = null;
jjtThis.setModifiers(modifiers);
boolean inInterfaceOld = inInterface;
inInterface = false;
}
{
@ -1427,7 +1428,7 @@ void ClassOrInterfaceDeclaration(int modifiers):
[ ExtendsList() ]
[ ImplementsList() ]
ClassOrInterfaceBody()
{ inInterface = false; } // always reset the flag after leaving the node
{ inInterface = inInterfaceOld; } // always restore the flag after leaving the node
}
void ExtendsList():
@ -1468,13 +1469,18 @@ jjtThis.setModifiers(modifiers);
}
void EnumBody():
{}
{
boolean inInterfaceOld = inInterface;
inInterface = false;
}
{
"{"
[( Annotation() )* EnumConstant() ( LOOKAHEAD(2) "," ( Annotation() )* EnumConstant() )* ]
[ "," ]
[ ";" ( ClassOrInterfaceBodyDeclaration() )* ]
"}"
{ inInterface = inInterfaceOld; } // always restore the flag after leaving the node
}
void EnumConstant():
@ -2013,7 +2019,12 @@ void AllocationExpression():
(
ArrayDimsAndInits()
|
Arguments() [ {inInterface = false;} ClassOrInterfaceBody() ]
Arguments()
[
{ boolean inInterfaceOld = inInterface; inInterface = false; /* a anonymous class is not a interface */ }
ClassOrInterfaceBody()
{ inInterface = inInterfaceOld; } // always restore the flag after leaving the node
]
)
)
{ checkForBadAnonymousDiamondUsage(); }