Skip to content

Commit

Permalink
Issue sevntu-checkstyle#427: fixed NPE in MapIterationInForEachLoopCh…
Browse files Browse the repository at this point in the history
…eck for map classes
  • Loading branch information
rnveach authored and kariem committed Jul 26, 2018
1 parent c456371 commit 12483cf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Expand Up @@ -400,7 +400,13 @@ private DetailAST getKeySetOrEntrySetNode(DetailAST forEachNode) {
|| ENTRY_SET_METHOD_NAME.equals(identNode.getText())) {
final String mapClassName;
if (isMapClassField) {
mapClassName = identNode.getPreviousSibling().getLastChild().getText();
final DetailAST lastChild = identNode.getPreviousSibling().getLastChild();
if (lastChild == null) {
mapClassName = null;
}
else {
mapClassName = lastChild.getText();
}
}
else {
mapClassName = identNode.getPreviousSibling().getText();
Expand Down
Expand Up @@ -27,6 +27,7 @@

import com.github.sevntu.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

public class MapIterationInForEachLoopCheckTest extends BaseCheckTestSupport {

Expand Down Expand Up @@ -95,4 +96,15 @@ public final void skipIfConditionTest() throws Exception {
expected);
}

@Test
public final void testClassExtendingMap() throws Exception {
checkConfig.addAttribute("proposeValuesUsage", "true");
checkConfig.addAttribute("proposeKeySetUsage", "true");
checkConfig.addAttribute("proposeEntrySetUsage", "true");

verify(checkConfig,
getPath("InputMapIterationInForEachLoopCheckExtendingMap.java"),
CommonUtils.EMPTY_STRING_ARRAY);
}

}
@@ -0,0 +1,12 @@
package com.github.sevntu.checkstyle.checks.coding;

import java.util.HashMap;

public class InputMapIterationInForEachLoopCheckExtendingMap {
public static class TestMap extends HashMap<Integer, Integer> {
public void test() {
for (Entry<Integer, Integer> entry : this.entrySet()) {
}
}
}
}

0 comments on commit 12483cf

Please sign in to comment.