From f37d432b9e07bb907468909943904e7a6804b6c7 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 27 Jun 2024 11:29:00 +0200 Subject: [PATCH] Revert "support TreeSet/Map" This reverts commit 209fc134621862b7043dd05ac72265cd21a838d2. --- .../bestpractices/UseEnumCollectionsRule.java | 9 ++------- .../resources/category/java/bestpractices.xml | 6 +++--- .../bestpractices/xml/UseEnumCollections.xml | 17 ++++------------- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UseEnumCollectionsRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UseEnumCollectionsRule.java index b4acae55e6..a90b971544 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UseEnumCollectionsRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UseEnumCollectionsRule.java @@ -7,8 +7,6 @@ package net.sourceforge.pmd.lang.java.rule.bestpractices; import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.TreeMap; -import java.util.TreeSet; import net.sourceforge.pmd.lang.java.ast.ASTConstructorCall; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule; @@ -35,11 +33,8 @@ public class UseEnumCollectionsRule extends AbstractJavaRulechainRule { JTypeMirror builtType = call.getTypeMirror(); if (!builtType.isRaw()) { - boolean isMap = TypeTestUtil.isExactlyA(HashMap.class, builtType) - || TypeTestUtil.isExactlyA(TreeMap.class, builtType); - if (isMap - || TypeTestUtil.isExactlyA(HashSet.class, builtType) - || TypeTestUtil.isExactlyA(TreeSet.class, builtType)) { + boolean isMap = TypeTestUtil.isExactlyA(HashMap.class, builtType); + if (isMap || TypeTestUtil.isExactlyA(HashSet.class, builtType)) { List typeArgs = ((JClassType) builtType).getTypeArgs(); JTypeDeclSymbol keySymbol = typeArgs.get(0).getSymbol(); diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index b965db740f..3a45bbaacd 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -1731,9 +1731,9 @@ public class Foo { class="net.sourceforge.pmd.lang.java.rule.bestpractices.UseEnumCollectionsRule" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#useenumcollections"> - Wherever possible, use `EnumSet` or `EnumMap` instead of more generic set and map implementations when the keys - are of an enum type. The specialized collections are more space- and time-efficient. - This rule reports constructor expressions for hash and tree sets or maps whose key type is an enum type. + Wherever possible, use `EnumSet` or `EnumMap` instead of `HashSet` and `HashMap` when the keys + are of an enum type. This rule reports constructor expressions for hash sets or maps whose key + type is an enum type. 3 diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UseEnumCollections.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UseEnumCollections.xml index 76f474e3b5..c98b1d7124 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UseEnumCollections.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UseEnumCollections.xml @@ -6,11 +6,10 @@ Use enumset - 2 - 8,12 + 1 + 8 This collection could be an EnumSet - This collection could be an EnumSet set = new HashSet<>(); return set.contains(E.A); } - public static boolean bar2() { - Set set = new TreeSet<>(); - return set.contains(E.A); - } } ]]> @@ -35,11 +30,10 @@ Use enummap - 2 - 7,10 + 1 + 7 This collection could be an EnumMap - This collection could be an EnumMap bar() { return new HashMap<>(); } - public static Map bar() { - return new TreeMap<>(); - } } ]]>