forked from phoedos/pmd
Merge branch 'bug-1529' into pmd/5.4.x
This commit is contained in:
@ -3,6 +3,8 @@
|
|||||||
*/
|
*/
|
||||||
package net.sourceforge.pmd.lang.java.rule;
|
package net.sourceforge.pmd.lang.java.rule;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import net.sourceforge.pmd.Rule;
|
import net.sourceforge.pmd.Rule;
|
||||||
import net.sourceforge.pmd.RuleContext;
|
import net.sourceforge.pmd.RuleContext;
|
||||||
import net.sourceforge.pmd.lang.ast.Node;
|
import net.sourceforge.pmd.lang.ast.Node;
|
||||||
@ -13,8 +15,10 @@ import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
|
|||||||
import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
|
import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
|
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
|
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
|
||||||
|
import net.sourceforge.pmd.lang.java.ast.AccessNode;
|
||||||
import net.sourceforge.pmd.lang.java.ast.CanSuppressWarnings;
|
import net.sourceforge.pmd.lang.java.ast.CanSuppressWarnings;
|
||||||
import net.sourceforge.pmd.lang.java.ast.JavaNode;
|
import net.sourceforge.pmd.lang.java.ast.JavaNode;
|
||||||
|
import net.sourceforge.pmd.lang.java.symboltable.ClassNameDeclaration;
|
||||||
import net.sourceforge.pmd.lang.java.symboltable.ClassScope;
|
import net.sourceforge.pmd.lang.java.symboltable.ClassScope;
|
||||||
import net.sourceforge.pmd.lang.java.symboltable.MethodScope;
|
import net.sourceforge.pmd.lang.java.symboltable.MethodScope;
|
||||||
import net.sourceforge.pmd.lang.java.symboltable.SourceFileScope;
|
import net.sourceforge.pmd.lang.java.symboltable.SourceFileScope;
|
||||||
@ -91,7 +95,6 @@ public class JavaRuleViolation extends ParametricRuleViolation<JavaNode> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setClassNameFrom(JavaNode node) {
|
private void setClassNameFrom(JavaNode node) {
|
||||||
|
|
||||||
String qualifiedName = null;
|
String qualifiedName = null;
|
||||||
for (ASTClassOrInterfaceDeclaration parent : node.getParentsOfType(ASTClassOrInterfaceDeclaration.class)) {
|
for (ASTClassOrInterfaceDeclaration parent : node.getParentsOfType(ASTClassOrInterfaceDeclaration.class)) {
|
||||||
String clsName = parent.getScope().getEnclosingScope(ClassScope.class).getClassName();
|
String clsName = parent.getScope().getEnclosingScope(ClassScope.class).getClassName();
|
||||||
@ -101,6 +104,20 @@ public class JavaRuleViolation extends ParametricRuleViolation<JavaNode> {
|
|||||||
qualifiedName = clsName + '$' + qualifiedName;
|
qualifiedName = clsName + '$' + qualifiedName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (qualifiedName == null) {
|
||||||
|
Set<ClassNameDeclaration> classes = node.getScope().getEnclosingScope(SourceFileScope.class).getClassDeclarations().keySet();
|
||||||
|
for (ClassNameDeclaration c : classes) {
|
||||||
|
// find the first public class/enum declaration
|
||||||
|
if (c.getAccessNodeParent() instanceof AccessNode) {
|
||||||
|
if (((AccessNode)c.getAccessNodeParent()).isPublic()) {
|
||||||
|
qualifiedName = c.getImage();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (qualifiedName != null) {
|
if (qualifiedName != null) {
|
||||||
className = qualifiedName;
|
className = qualifiedName;
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ import net.sourceforge.pmd.lang.ParserOptions;
|
|||||||
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
|
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
|
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
|
||||||
|
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
||||||
import net.sourceforge.pmd.lang.java.symboltable.ScopeAndDeclarationFinder;
|
import net.sourceforge.pmd.lang.java.symboltable.ScopeAndDeclarationFinder;
|
||||||
|
|
||||||
@ -56,4 +57,49 @@ public class JavaRuleViolationTest {
|
|||||||
final JavaRuleViolation violation = new JavaRuleViolation(null, context, md, null);
|
final JavaRuleViolation violation = new JavaRuleViolation(null, context, md, null);
|
||||||
assertEquals("bar", violation.getMethodName());
|
assertEquals("bar", violation.getMethodName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that the class name is taken correctly, even if the node is outside of a class scope,
|
||||||
|
* e.g. a import declaration.
|
||||||
|
* @see <a href="https://sourceforge.net/p/pmd/bugs/1529/">#1529</a>
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testPackageAndClassName() {
|
||||||
|
ASTCompilationUnit ast = parse("package pkg; import java.util.List; public 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPackageAndEnumName() {
|
||||||
|
ASTCompilationUnit ast = parse("package pkg; import java.util.List; public enum FooE { }");
|
||||||
|
ASTImportDeclaration importNode = ast.getFirstDescendantOfType(ASTImportDeclaration.class);
|
||||||
|
|
||||||
|
JavaRuleViolation violation = new JavaRuleViolation(null, new RuleContext(), importNode, null);
|
||||||
|
assertEquals("pkg", violation.getPackageName());
|
||||||
|
assertEquals("FooE", violation.getClassName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDefaultPackageAndClassName() {
|
||||||
|
ASTCompilationUnit ast = parse("import java.util.List; public class Foo { }");
|
||||||
|
ASTImportDeclaration importNode = ast.getFirstDescendantOfType(ASTImportDeclaration.class);
|
||||||
|
|
||||||
|
JavaRuleViolation violation = new JavaRuleViolation(null, new RuleContext(), importNode, null);
|
||||||
|
assertEquals("", violation.getPackageName());
|
||||||
|
assertEquals("Foo", violation.getClassName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPackageAndMultipleClassesName() {
|
||||||
|
ASTCompilationUnit ast = parse("package pkg; import java.util.List; class Foo { } public class Bar { }");
|
||||||
|
ASTImportDeclaration importNode = ast.getFirstDescendantOfType(ASTImportDeclaration.class);
|
||||||
|
|
||||||
|
JavaRuleViolation violation = new JavaRuleViolation(null, new RuleContext(), importNode, null);
|
||||||
|
assertEquals("pkg", violation.getPackageName());
|
||||||
|
assertEquals("Bar", violation.getClassName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,8 @@
|
|||||||
* [#1501](https://sourceforge.net/p/pmd/bugs/1501/): \[java] \[apex] CyclomaticComplexity rule causes OOM when class reporting is disabled
|
* [#1501](https://sourceforge.net/p/pmd/bugs/1501/): \[java] \[apex] CyclomaticComplexity rule causes OOM when class reporting is disabled
|
||||||
* java-comments
|
* java-comments
|
||||||
* [#1522](https://sourceforge.net/p/pmd/bugs/1522/): \[java] CommentRequired: false positive
|
* [#1522](https://sourceforge.net/p/pmd/bugs/1522/): \[java] CommentRequired: false positive
|
||||||
|
* java-imports/UnusedImports
|
||||||
|
* [#1529](https://sourceforge.net/p/pmd/bugs/1529/): \[java] UnusedImports: The created rule violation has no class name
|
||||||
* General
|
* General
|
||||||
* [#1499](https://sourceforge.net/p/pmd/bugs/1499/): \[core] CPD test break PMD 5.5.1 build on Windows
|
* [#1499](https://sourceforge.net/p/pmd/bugs/1499/): \[core] CPD test break PMD 5.5.1 build on Windows
|
||||||
* [#1506](https://sourceforge.net/p/pmd/bugs/1506/): \[core] When runing any RuleTst, start/end methods not called
|
* [#1506](https://sourceforge.net/p/pmd/bugs/1506/): \[core] When runing any RuleTst, start/end methods not called
|
||||||
|
Reference in New Issue
Block a user