From 8810e58a319df2452d5fc9436e916f0f68fd2638 Mon Sep 17 00:00:00 2001 From: hemanshu070 Date: Thu, 20 Dec 2018 23:13:52 +0530 Subject: [PATCH 1/4] 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 From 5b6ac1b2fb884f0d22dbed582b47ab52f8d627c0 Mon Sep 17 00:00:00 2001 From: hemanshu070 Date: Sun, 23 Dec 2018 20:04:43 +0530 Subject: [PATCH 2/4] This commit is regarding the addition of the since and the externalInfo URL and update the message to be more descriptive. --- pmd-java/src/main/resources/category/java/codestyle.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pmd-java/src/main/resources/category/java/codestyle.xml b/pmd-java/src/main/resources/category/java/codestyle.xml index 37ed132429..fb950a2ba6 100644 --- a/pmd-java/src/main/resources/category/java/codestyle.xml +++ b/pmd-java/src/main/resources/category/java/codestyle.xml @@ -1812,8 +1812,10 @@ public class Foo { Use the diamond operator to let the type be inferred 1 From e00b0b83fae4da1651c0833a3b7a6a2e8074c150 Mon Sep 17 00:00:00 2001 From: hemanshu070 Date: Mon, 24 Dec 2018 18:50:17 +0530 Subject: [PATCH 3/4] This commit is regarding the addition of the example tag to codestyle.xml and addition of the licence in the test class "UseDiamondOperator.java" --- .../src/main/resources/category/java/codestyle.xml | 13 +++++++++++-- .../java/rule/codestyle/UseDiamondOperator.java | 4 ++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pmd-java/src/main/resources/category/java/codestyle.xml b/pmd-java/src/main/resources/category/java/codestyle.xml index fb950a2ba6..97d602c139 100644 --- a/pmd-java/src/main/resources/category/java/codestyle.xml +++ b/pmd-java/src/main/resources/category/java/codestyle.xml @@ -1813,11 +1813,14 @@ public class Foo { - Use the diamond operator to let the type be inferred + Use the diamond operator to let the type be inferred With the Diamond operator it is possible to avoid duplication of the type parameters. + Instead, the compiler is now able to infer the parameter types for constructor calls, + which makes the code also more readable. + 1 @@ -1836,6 +1839,12 @@ public class Foo { + + strings = new ArrayList(); // unnecessary duplication of type parameters +List stringsWithDiamond = new ArrayList<>(); // using the diamond operator is more concise +]]> + Date: Thu, 27 Dec 2018 10:29:28 +0100 Subject: [PATCH 4/4] Update release notes, refs #1534, fixes #1517 --- docs/pages/release_notes.md | 8 ++ .../resources/category/java/codestyle.xml | 77 ++++++++++--------- 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index cf0b330b6c..5562146ade 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -14,6 +14,12 @@ This is a {{ site.pmd.release_type }} release. ### New and noteworthy +#### New Rules + +* The new Java rule {% rule "java/codestyle/UseDiamondOperator" %} (`java-codestyle`) looks for constructor + calls with explicit type parameters. Since Java 1.7, these type parameters are not necessary anymore, as they + can be inferred now. + #### Modified Rules * The Java rule {% rule "java/codestyle/LocalVariableCouldBeFinal" %} (`java-codestyle`) has a new @@ -26,6 +32,7 @@ This is a {{ site.pmd.release_type }} release. * [#658](https://github.com/pmd/pmd/issues/658): \[java] OneDeclarationPerLine: False positive for loops * java-codestyle * [#1513](https://github.com/pmd/pmd/issues/1513): \[java] LocalVariableCouldBeFinal: allow excluding the variable in a for-each loop + * [#1517](https://github.com/pmd/pmd/issues/1517): \[java] New Rule: UseDiamondOperator * java-errorprone * [#1035](https://github.com/pmd/pmd/issues/1035): \[java] ReturnFromFinallyBlock: False positive on lambda expression in finally block @@ -37,6 +44,7 @@ This is a {{ site.pmd.release_type }} release. * [#1514](https://github.com/pmd/pmd/pull/1514): \[java] LocalVariableCouldBeFinal: allow excluding the variable in a for-each loop - [Kris Scheibe](https://github.com/kris-scheibe) * [#1516](https://github.com/pmd/pmd/pull/1516): \[java] OneDeclarationPerLine: Don't report multiple variables in a for statement. - [Kris Scheibe](https://github.com/kris-scheibe) * [#1521](https://github.com/pmd/pmd/pull/1521): \[java] Upgrade to ASM7 for JDK 11 support - [Mark Pritchard](https://github.com/markpritchard) +* [#1534](https://github.com/pmd/pmd/pull/1534): \[java] This is the change regarding the usediamondoperator #1517 - [hemanshu070](https://github.com/hemanshu070) {% endtocmaker %} diff --git a/pmd-java/src/main/resources/category/java/codestyle.xml b/pmd-java/src/main/resources/category/java/codestyle.xml index 97d602c139..5ce3e28c78 100644 --- a/pmd-java/src/main/resources/category/java/codestyle.xml +++ b/pmd-java/src/main/resources/category/java/codestyle.xml @@ -1809,44 +1809,6 @@ public class Foo { - - - Use the diamond operator to let the type be inferred With the Diamond operator it is possible to avoid duplication of the type parameters. - Instead, the compiler is now able to infer the parameter types for constructor calls, - which makes the code also more readable. - - 1 - - - - - - - - - strings = new ArrayList(); // unnecessary duplication of type parameters -List stringsWithDiamond = new ArrayList<>(); // using the diamond operator is more concise -]]> - - - + + +Use the diamond operator to let the type be inferred automatically. With the Diamond operator it is possible +to avoid duplication of the type parameters. +Instead, the compiler is now able to infer the parameter types for constructor calls, +which makes the code also more readable. + + 1 + + + + + + + + + strings = new ArrayList(); // unnecessary duplication of type parameters +List stringsWithDiamond = new ArrayList<>(); // using the diamond operator is more concise +]]> + + +