Merge branch 'pr-1744'

This commit is contained in:
Andreas Dangel
2019-04-01 19:35:22 +02:00
3 changed files with 26 additions and 0 deletions

View File

@ -16,6 +16,9 @@ This is a {{ site.pmd.release_type }} release.
### Fixed Issues
* java
* [#1729](https://github.com/pmd/pmd/issues/1729): \[java] JavaRuleViolation loses information in `className` field when class has package-private access level
### API Changes
### External Contributions

View File

@ -119,6 +119,19 @@ public class JavaRuleViolation extends ParametricRuleViolation<JavaNode> {
}
}
}
// Still not found?
if (qualifiedName == null) {
for (ClassNameDeclaration c : classes) {
// find the first package-private class/enum declaration
if (c.getAccessNodeParent() instanceof AccessNode) {
if (((AccessNode) c.getAccessNodeParent()).isPackagePrivate()) {
qualifiedName = c.getImage();
break;
}
}
}
}
}
if (qualifiedName != null) {

View File

@ -120,4 +120,14 @@ public class JavaRuleViolationTest {
assertEquals("pkg", violation.getPackageName());
assertEquals("Bar", violation.getClassName());
}
@Test
public void testPackageAndPackagePrivateClassesName() {
ASTCompilationUnit ast = parse("package pkg; import java.util.List; class Foo { }");
ASTImportDeclaration importNode = ast.getFirstDescendantOfType(ASTImportDeclaration.class);
JavaRuleViolation violation = new JavaRuleViolation(null, new RuleContext(), importNode, null);
assertEquals("pkg", violation.getPackageName());
assertEquals("Foo", violation.getClassName());
}
}