From 62fc70e8161f721cb5cffdd688580fc2543fc029 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 30 Mar 2013 09:25:04 +0100 Subject: [PATCH] pmd: fix #958 Intermittent NullPointerException while loading XPath node attributes * synchronizing the method cache * adding additional null check --- pmd/etc/changelog.txt | 1 + .../lang/ast/xpath/AttributeAxisIterator.java | 6 ++- .../ast/xpath/AttributeAxisIteratorTest.java | 43 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 pmd/src/test/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIteratorTest.java diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index f63f9a529b..7bccf648e4 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -1,5 +1,6 @@ ????? ??, 2013 - 5.0.3: +Fixed bug 958: Intermittent NullPointerException while loading XPath node attributes Fixed bug 968: Issues with JUnit4 @Test annotation with expected exception (Thanks to Yiannis Paschalidis) Fixed bug 975: false positive in ClassCastExceptionWithToArray Fixed bug 976: UselessStringValueOf wrong when appending character arrays diff --git a/pmd/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIterator.java b/pmd/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIterator.java index 3c372dd3f2..874281b4f0 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIterator.java +++ b/pmd/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIterator.java @@ -5,6 +5,7 @@ package net.sourceforge.pmd.lang.ast.xpath; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -48,7 +49,8 @@ public class AttributeAxisIterator implements Iterator { private int position; private Node node; - private static Map, MethodWrapper[]> methodCache = new HashMap, MethodWrapper[]>(); + private static Map, MethodWrapper[]> methodCache = + Collections.synchronizedMap(new HashMap, MethodWrapper[]>()); public AttributeAxisIterator(Node contextNode) { this.node = contextNode; @@ -86,7 +88,7 @@ public class AttributeAxisIterator implements Iterator { } private Attribute getNextAttribute() { - if (position == methodWrappers.length) { + if (methodWrappers == null || position == methodWrappers.length) { return null; } MethodWrapper m = methodWrappers[position++]; diff --git a/pmd/src/test/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIteratorTest.java b/pmd/src/test/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIteratorTest.java new file mode 100644 index 0000000000..7344e379dc --- /dev/null +++ b/pmd/src/test/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIteratorTest.java @@ -0,0 +1,43 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +package net.sourceforge.pmd.lang.ast.xpath; + +import java.util.HashMap; +import java.util.Map; + +import net.sourceforge.pmd.lang.java.ast.DummyJavaNode; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Unit test for {@link AttributeAxisIterator} + */ +public class AttributeAxisIteratorTest { + + /** + * Test hasNext and next. + */ + @Test + public void testAttributeAxisIterator() { + DummyJavaNode dummyNode = new DummyJavaNode(1); + dummyNode.testingOnly__setBeginLine(1); + dummyNode.testingOnly__setBeginColumn(1); + + AttributeAxisIterator it = new AttributeAxisIterator(dummyNode); + Map atts = new HashMap(); + while (it.hasNext()) { + Attribute attribute = it.next(); + atts.put(attribute.getName(), attribute); + } + Assert.assertEquals(7, atts.size()); + Assert.assertTrue(atts.containsKey("BeginColumn")); + Assert.assertTrue(atts.containsKey("BeginLine")); + Assert.assertTrue(atts.containsKey("FindBoundary")); + Assert.assertTrue(atts.containsKey("Image")); + Assert.assertTrue(atts.containsKey("SingleLine")); + Assert.assertTrue(atts.containsKey("EndColumn")); + Assert.assertTrue(atts.containsKey("EndLine")); + } +}