From 9a5c2523f646b380256edac8b7378dbd264a6c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Wed, 16 Nov 2022 14:41:06 +0100 Subject: [PATCH] Fix pmd-java --- pmd-java/etc/grammar/Java.jjt | 2 +- .../CommentDefaultAccessModifierRule.java | 6 +- .../pmd/lang/java/ast/ParserCornerCases18.txt | 81 ++++++++++++++++++- 3 files changed, 83 insertions(+), 6 deletions(-) diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 298d21b55b..76c27ea8bd 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -1774,7 +1774,7 @@ void InstanceOfExpression() #void: { AbstractJavaNode id = jjtree.popNode(); ASTRecordPattern pat = (ASTRecordPattern) jjtree.peekNode(); - pat.addChild(id); + pat.addChild(id, pat.getNumChildren()); } ] ] diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/CommentDefaultAccessModifierRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/CommentDefaultAccessModifierRule.java index e83a7eadad..1eb282b5f3 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/CommentDefaultAccessModifierRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/CommentDefaultAccessModifierRule.java @@ -143,7 +143,7 @@ public class CommentDefaultAccessModifierRule extends AbstractJavaRulechainRule boolean isConcreteClass = !enclosing.isInterface() && !enclosing.isEnum(); // ignore if it's inside an interface / Annotation - return isConcreteClass && isMissingComment(decl) && !hasIgnoredAnnotation(parentClassOrInterface); + return isConcreteClass && isMissingComment(decl) && isNotIgnored(decl) && isNotIgnored(enclosing); } private boolean isMissingComment(AccessNode decl) { @@ -152,9 +152,7 @@ public class CommentDefaultAccessModifierRule extends AbstractJavaRulechainRule return decl.getVisibility() == Visibility.V_PACKAGE // if is a default access modifier check if there is a comment // in this line - && !interestingLineNumberComments.contains(decl.getBeginLine()) - // that it is not annotated with e.g. @VisibleForTesting - && isNotIgnored(decl); + && !interestingLineNumberComments.contains(decl.getBeginLine()); } private boolean isNotIgnored(AccessNode decl) { diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/ParserCornerCases18.txt b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/ParserCornerCases18.txt index c2331de61c..66aa97547f 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/ParserCornerCases18.txt +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/ParserCornerCases18.txt @@ -11,7 +11,7 @@ +- ImportDeclaration[@ImportOnDemand = false, @ImportedName = "java.util.stream.Stream", @ImportedSimpleName = "Stream", @PackageName = "java.util.stream", @Static = false] +- ClassOrInterfaceDeclaration[@Abstract = false, @Annotation = false, @Anonymous = false, @BinaryName = "ParserCornerCases18", @CanonicalName = "ParserCornerCases18", @EffectiveVisibility = Visibility.V_PUBLIC, @Enum = false, @Final = false, @Interface = false, @Local = false, @Nested = false, @PackageName = "", @PackagePrivate = false, @Record = false, @RegularClass = true, @RegularInterface = false, @SimpleName = "ParserCornerCases18", @TopLevel = true, @Visibility = Visibility.V_PUBLIC] | +- ModifierList[@EffectiveModifiers = "{public}", @ExplicitModifiers = "{public}"] - | +- ClassOrInterfaceBody[@Empty = false, @Size = 26] + | +- ClassOrInterfaceBody[@Empty = false, @Size = 30] | +- MethodDeclaration[@Abstract = false, @Arity = 0, @EffectiveVisibility = Visibility.V_PUBLIC, @Image = "lambdas", @Name = "lambdas", @Overridden = false, @Varargs = false, @Visibility = Visibility.V_PUBLIC, @Void = true] | | +- ModifierList[@EffectiveModifiers = "{public}", @ExplicitModifiers = "{public}"] | | +- VoidType[] @@ -966,6 +966,85 @@ | | +- Annotation[@SimpleName = "NonNull"] | | | +- ClassOrInterfaceType[@FullyQualified = false, @SimpleName = "NonNull"] | | +- NumericLiteral[@Base = 10, @CompileTimeConstant = true, @DoubleLiteral = false, @FloatLiteral = false, @Image = "0", @IntLiteral = true, @Integral = true, @LongLiteral = false, @ParenthesisDepth = 0, @Parenthesized = false, @ValueAsDouble = 0.0, @ValueAsFloat = 0.0, @ValueAsInt = 0, @ValueAsLong = 0] + | +- MethodDeclaration[@Abstract = false, @Arity = 1, @EffectiveVisibility = Visibility.V_PUBLIC, @Image = "check", @Name = "check", @Overridden = false, @Varargs = false, @Visibility = Visibility.V_PUBLIC, @Void = false] + | | +- ModifierList[@EffectiveModifiers = "{public, static}", @ExplicitModifiers = "{public, static}"] + | | +- TypeParameters[@Empty = false, @Size = 1] + | | | +- TypeParameter[@Image = "T", @Name = "T", @TypeBound = false] + | | +- ArrayType[@ArrayDepth = 2] + | | | +- ClassOrInterfaceType[@FullyQualified = false, @SimpleName = "T"] + | | | +- ArrayDimensions[@Empty = false, @Size = 2] + | | | +- ArrayTypeDim[@Varargs = false] + | | | +- ArrayTypeDim[@Varargs = false] + | | +- FormalParameters[@Empty = false, @Size = 1] + | | | +- FormalParameter[@EffectiveVisibility = Visibility.V_LOCAL, @Final = false, @Varargs = false, @Visibility = Visibility.V_LOCAL] + | | | +- ModifierList[@EffectiveModifiers = "{}", @ExplicitModifiers = "{}"] + | | | +- ArrayType[@ArrayDepth = 2] + | | | | +- ClassOrInterfaceType[@FullyQualified = false, @SimpleName = "T"] + | | | | +- ArrayDimensions[@Empty = false, @Size = 2] + | | | | +- ArrayTypeDim[@Varargs = false] + | | | | | +- Annotation[@SimpleName = "Anno"] + | | | | | +- ClassOrInterfaceType[@FullyQualified = false, @SimpleName = "Anno"] + | | | | +- ArrayTypeDim[@Varargs = false] + | | | | +- Annotation[@SimpleName = "Anno"] + | | | | +- ClassOrInterfaceType[@FullyQualified = false, @SimpleName = "Anno"] + | | | +- VariableDeclaratorId[@ArrayType = true, @EffectiveVisibility = Visibility.V_LOCAL, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = false, @Final = false, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = true, @LambdaParameter = false, @LocalVariable = false, @Name = "arr", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = false, @TypeInferred = false, @Visibility = Visibility.V_LOCAL] + | | +- Block[@Empty = false, @Size = 2, @containsComment = false] + | | +- IfStatement[@Else = false] + | | | +- InfixExpression[@CompileTimeConstant = false, @Operator = BinaryOp.EQ, @ParenthesisDepth = 0, @Parenthesized = false] + | | | | +- VariableAccess[@AccessType = AccessType.READ, @CompileTimeConstant = false, @Image = "arr", @Name = "arr", @ParenthesisDepth = 0, @Parenthesized = false] + | | | | +- NullLiteral[@CompileTimeConstant = false, @ParenthesisDepth = 0, @Parenthesized = false] + | | | +- Block[@Empty = false, @Size = 1, @containsComment = false] + | | | +- ThrowStatement[] + | | | +- ConstructorCall[@AnonymousClass = false, @CompileTimeConstant = false, @DiamondTypeArgs = false, @MethodName = "new", @ParenthesisDepth = 0, @Parenthesized = false, @QualifiedInstanceCreation = false] + | | | +- ClassOrInterfaceType[@FullyQualified = false, @SimpleName = "NullPointerException"] + | | | +- ArgumentList[@Empty = true, @Size = 0] + | | +- ReturnStatement[] + | | +- VariableAccess[@AccessType = AccessType.READ, @CompileTimeConstant = false, @Image = "arr", @Name = "arr", @ParenthesisDepth = 0, @Parenthesized = false] + | +- MethodDeclaration[@Abstract = false, @Arity = 0, @EffectiveVisibility = Visibility.V_PUBLIC, @Image = "func", @Name = "func", @Overridden = false, @Varargs = false, @Visibility = Visibility.V_PUBLIC, @Void = false] + | | +- ModifierList[@EffectiveModifiers = "{public}", @ExplicitModifiers = "{public}"] + | | +- ClassOrInterfaceType[@FullyQualified = false, @SimpleName = "Function"] + | | +- FormalParameters[@Empty = true, @Size = 0] + | | | +- ReceiverParameter[] + | | | +- ClassOrInterfaceType[@FullyQualified = false, @SimpleName = "Main"] + | | +- Block[@Empty = false, @Size = 1, @containsComment = true] + | | +- ReturnStatement[] + | | +- MethodReference[@CompileTimeConstant = false, @ConstructorReference = true, @MethodName = "new", @ParenthesisDepth = 0, @Parenthesized = false] + | | +- TypeExpression[@CompileTimeConstant = false, @ParenthesisDepth = 0, @Parenthesized = false] + | | +- ClassOrInterfaceType[@FullyQualified = false, @SimpleName = "Inner"] + | | +- ClassOrInterfaceType[@FullyQualified = false, @SimpleName = "Main"] + | | +- Annotation[@SimpleName = "A"] + | | +- ClassOrInterfaceType[@FullyQualified = false, @SimpleName = "A"] + | +- MethodDeclaration[@Abstract = false, @Arity = 1, @EffectiveVisibility = Visibility.V_PUBLIC, @Image = "max", @Name = "max", @Overridden = false, @Varargs = true, @Visibility = Visibility.V_PUBLIC, @Void = false] + | | +- ModifierList[@EffectiveModifiers = "{public, static}", @ExplicitModifiers = "{public, static}"] + | | +- PrimitiveType[@Kind = PrimitiveTypeKind.BYTE] + | | +- FormalParameters[@Empty = false, @Size = 1] + | | | +- FormalParameter[@EffectiveVisibility = Visibility.V_LOCAL, @Final = true, @Varargs = true, @Visibility = Visibility.V_LOCAL] + | | | +- ModifierList[@EffectiveModifiers = "{final}", @ExplicitModifiers = "{final}"] + | | | +- ArrayType[@ArrayDepth = 1] + | | | | +- PrimitiveType[@Kind = PrimitiveTypeKind.BYTE] + | | | | +- ArrayDimensions[@Empty = false, @Size = 1] + | | | | +- ArrayTypeDim[@Varargs = true] + | | | | +- Annotation[@SimpleName = "NotNull"] + | | | | +- ClassOrInterfaceType[@FullyQualified = false, @SimpleName = "NotNull"] + | | | +- VariableDeclaratorId[@ArrayType = true, @EffectiveVisibility = Visibility.V_LOCAL, @EnumConstant = false, @ExceptionBlockParameter = false, @Field = false, @Final = true, @ForLoopVariable = false, @ForeachVariable = false, @FormalParameter = true, @LambdaParameter = false, @LocalVariable = false, @Name = "array", @PatternBinding = false, @RecordComponent = false, @ResourceDeclaration = false, @TypeInferred = false, @Visibility = Visibility.V_LOCAL] + | | +- Block[@Empty = false, @Size = 1, @containsComment = false] + | | +- ReturnStatement[] + | | +- NumericLiteral[@Base = 10, @CompileTimeConstant = true, @DoubleLiteral = false, @FloatLiteral = false, @Image = "0", @IntLiteral = true, @Integral = true, @LongLiteral = false, @ParenthesisDepth = 0, @Parenthesized = false, @ValueAsDouble = 0.0, @ValueAsFloat = 0.0, @ValueAsInt = 0, @ValueAsLong = 0] + | +- AnnotationTypeDeclaration[@Abstract = true, @Annotation = true, @Anonymous = false, @BinaryName = "ParserCornerCases18$Anno", @CanonicalName = "ParserCornerCases18.Anno", @EffectiveVisibility = Visibility.V_PACKAGE, @Enum = false, @Final = false, @Interface = true, @Local = false, @Nested = true, @PackageName = "", @Record = false, @RegularClass = false, @RegularInterface = false, @SimpleName = "Anno", @TopLevel = false, @Visibility = Visibility.V_PACKAGE] + | | +- ModifierList[@EffectiveModifiers = "{abstract, static}", @ExplicitModifiers = "{}"] + | | | +- Annotation[@SimpleName = "Retention"] + | | | | +- ClassOrInterfaceType[@FullyQualified = false, @SimpleName = "Retention"] + | | | | +- AnnotationMemberList[@Empty = false, @Size = 1] + | | | | +- MemberValuePair[@Image = "value", @Name = "value", @Shorthand = true] + | | | | +- FieldAccess[@AccessType = AccessType.READ, @CompileTimeConstant = false, @Image = "CLASS", @Name = "CLASS", @ParenthesisDepth = 0, @Parenthesized = false] + | | | | +- AmbiguousName[@CompileTimeConstant = false, @Image = "RetentionPolicy", @Name = "RetentionPolicy", @ParenthesisDepth = 0, @Parenthesized = false] + | | | +- Annotation[@SimpleName = "Target"] + | | | +- ClassOrInterfaceType[@FullyQualified = false, @SimpleName = "Target"] + | | | +- AnnotationMemberList[@Empty = false, @Size = 1] + | | | +- MemberValuePair[@Image = "value", @Name = "value", @Shorthand = true] + | | | +- MemberValueArrayInitializer[] + | | | +- VariableAccess[@AccessType = AccessType.READ, @CompileTimeConstant = false, @Image = "TYPE_USE", @Name = "TYPE_USE", @ParenthesisDepth = 0, @Parenthesized = false] + | | +- AnnotationTypeBody[@Empty = true, @Size = 0] | +- MethodDeclaration[@Abstract = false, @Arity = 0, @EffectiveVisibility = Visibility.V_PRIVATE, @Image = "testMultiDimArrayWithAnnotations", @Name = "testMultiDimArrayWithAnnotations", @Overridden = false, @Varargs = false, @Visibility = Visibility.V_PRIVATE, @Void = true] | | +- ModifierList[@EffectiveModifiers = "{private, static}", @ExplicitModifiers = "{private, static}"] | | +- VoidType[]