From b4706b49fd0a872290bba5700c3bb9eda2bf0228 Mon Sep 17 00:00:00 2001 From: Romain Pelisse Date: Fri, 15 Feb 2008 19:08:03 +0000 Subject: [PATCH] Fix for bug 1842505 - xml output incorrect for inner classes plus some minor refactoring. git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5779 51baf565-9d33-0410-a72c-fc3788e3496d --- .../net/sourceforge/pmd/RuleViolation.java | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/pmd/src/net/sourceforge/pmd/RuleViolation.java b/pmd/src/net/sourceforge/pmd/RuleViolation.java index 86e294d9a4..7a75542c44 100644 --- a/pmd/src/net/sourceforge/pmd/RuleViolation.java +++ b/pmd/src/net/sourceforge/pmd/RuleViolation.java @@ -3,6 +3,10 @@ */ package net.sourceforge.pmd; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + import net.sourceforge.pmd.ast.ASTClassOrInterfaceBodyDeclaration; import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.ast.ASTFieldDeclaration; @@ -14,11 +18,6 @@ import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; import net.sourceforge.pmd.ast.CanSuppressWarnings; import net.sourceforge.pmd.ast.SimpleNode; -import java.util.Comparator; -import java.util.Iterator; -import java.util.List; -import java.util.ArrayList; - public class RuleViolation implements IRuleViolation { public static class RuleViolationComparator implements Comparator { @@ -80,18 +79,32 @@ public class RuleViolation implements IRuleViolation { // default to symbol table lookup className = node.getScope().getEnclosingClassScope().getClassName() == null ? "" : node.getScope().getEnclosingClassScope().getClassName(); } - + // default to symbol table lookup + String qualifiedName = null; + List parents = node.getParentsOfType(ASTClassOrInterfaceDeclaration.class); + for ( ASTClassOrInterfaceDeclaration parent : parents ) + { + if (qualifiedName == null) { + qualifiedName = parent.getScope().getEnclosingClassScope().getClassName(); + } else { + qualifiedName = qualifiedName + "$" + parent.getScope().getEnclosingClassScope().getClassName(); + } + } + // Sourcefile does not have an enclosing class scope... + if ( ! "net.sourceforge.pmd.symboltable.SourceFileScope".equals(node.getScope().getClass().getName() ) ) { + className = node.getScope().getEnclosingClassScope().getClassName() == null ? "" : qualifiedName; + } setVariableNameIfExists(node); - + methodName = node.getFirstParentOfType(ASTMethodDeclaration.class) == null ? "" : node.getScope().getEnclosingMethodScope().getName(); - + packageName = node.getScope().getEnclosingSourceFileScope().getPackageName() == null ? "" : node.getScope().getEnclosingSourceFileScope().getPackageName(); - + beginLine = node.getBeginLine(); endLine = node.getEndLine(); beginColumn = node.getBeginColumn(); endColumn = node.getEndColumn(); - + // TODO combine this duplicated code // TODO same for duplicated code in ASTTypeDeclaration && ASTClassOrInterfaceBodyDeclaration List parentTypes = new ArrayList(node.getParentsOfType(ASTTypeDeclaration.class)); @@ -110,8 +123,8 @@ public class RuleViolation implements IRuleViolation { if (node instanceof ASTLocalVariableDeclaration) { parentTypes.add(node); } - for (Iterator i = parentTypes.iterator(); i.hasNext();) { - CanSuppressWarnings t = (CanSuppressWarnings) i.next(); + for (SimpleNode parentType : parentTypes) { + CanSuppressWarnings t = (CanSuppressWarnings) parentType; if (t.hasSuppressWarningsAnnotationFor(getRule())) { isSuppressed = true; }