From 8810e58a319df2452d5fc9436e916f0f68fd2638 Mon Sep 17 00:00:00 2001 From: hemanshu070 Date: Thu, 20 Dec 2018 23:13:52 +0530 Subject: [PATCH] This is the change regarding the usediamondoperator #1517 --- .../resources/category/java/codestyle.xml | 27 ++++++++ .../rule/codestyle/UseDiamondOperator.java | 7 +++ .../rule/codestyle/xml/UseDiamondOperator.xml | 62 +++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/UseDiamondOperator.java create mode 100644 pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UseDiamondOperator.xml diff --git a/pmd-java/src/main/resources/category/java/codestyle.xml b/pmd-java/src/main/resources/category/java/codestyle.xml index b5ded22baf..37ed132429 100644 --- a/pmd-java/src/main/resources/category/java/codestyle.xml +++ b/pmd-java/src/main/resources/category/java/codestyle.xml @@ -1809,6 +1809,33 @@ public class Foo { + + + Use the diamond operator to let the type be inferred + 1 + + + + + + + + + + + + Use Diamond + 2 + 8,11 + field; + public void foo() { + List strings = new ArrayList(); + List strings2 = new ArrayList<>(); + this.field = new ArrayList(); + } +} + ]]> + + + False positive cases: anonymous classes, methods calls + 0 + > typeReference; + public void foo() { + Collections.sort(files, new Comparator() { + @Override + public int compare(DataSource left, DataSource right) { + String leftString = left.getNiceFileName(useShortNames, inputPaths); + String rightString = right.getNiceFileName(useShortNames, inputPaths); + return leftString.compareTo(rightString); + } + }); + final TreeSet> sortedKeySet = new TreeSet<>( + new Comparator>() { + @Override + public int compare(final Entry o1, final Entry o2) { + return Long.compare(o1.getValue().selfTimeNanos.get(), o2.getValue().selfTimeNanos.get()); + } + }); + new ThreadLocal>() { + @Override + protected Queue initialValue() { + return Collections.asLifoQueue(new LinkedList()); + } + }; + Iterator EMPTY_ITERATOR = new ArrayList().iterator(); + Class type = null; + typeReference = new WeakReference>(type); + ((ListNode) rev).reverseCache = new SoftReference>(this); + } + public Map, Object> getOverriddenPropertiesByPropertyDescriptor() { + return propertyValues == null ? new HashMap, Object>() : new HashMap<>(propertyValues); + } +} + ]]> + + \ No newline at end of file