[java] UseDiamondOperator - add property java7compatibility
This property is disabled by default, so that the rule suggests changes that work on java8+ only by default. Enable the version dependent tests.
This commit is contained in:
@ -1992,6 +1992,11 @@ Use the diamond operator to let the type be inferred automatically. With the Dia
|
||||
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.
|
||||
|
||||
The diamond operator has been introduced with java 7. However, type inference has been improved further
|
||||
with java8, rendering more type parameters unnecessary. This is only possible with java8 and the resulting
|
||||
code won't compile with java7. If you use java7, make sure to enable `java7compatibility` for this rule to avoid
|
||||
false positives.
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
@ -2004,15 +2009,17 @@ which makes the code also more readable.
|
||||
|
|
||||
//StatementExpression[AssignmentOperator and PrimaryExpression/PrimaryPrefix[not(Expression)]]
|
||||
)
|
||||
/Expression/PrimaryExpression[not(PrimarySuffix) and not(ancestor::ArgumentList)]
|
||||
/(Expression | Expression[$java7compatibility = false()]/ConditionalExpression | Expression[$java7compatibility = false()]/ConditionalExpression/Expression)
|
||||
/PrimaryExpression[not(PrimarySuffix) and not(ancestor::ArgumentList)]
|
||||
/PrimaryPrefix
|
||||
/AllocationExpression
|
||||
[@AnonymousClass=false()]
|
||||
[ClassOrInterfaceType/TypeArguments[@Diamond=false() and not(.//TypeArgument[@Wildcard=true()])]]
|
||||
[ClassOrInterfaceType/TypeArguments[@Diamond=false() and not($java7compatibility = true() and .//TypeArgument[@Wildcard=true()])]]
|
||||
[not(ArrayDimsAndInits)]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
<property name="java7compatibility" type="Boolean" description="If disabled, the rule shows also violations that are applicable for java8+" value="false" />
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
|
@ -145,6 +145,7 @@ class Foo {
|
||||
|
||||
<test-code>
|
||||
<description>(J7) Version sensitive tests - avoid possible false positives on Java7</description>
|
||||
<rule-property name="java7compatibility">true</rule-property>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
import java.lang.ref.WeakReference;
|
||||
@ -167,7 +168,7 @@ public class Foo {
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code regressionTest="false">
|
||||
<test-code>
|
||||
<description>(J8) Version sensitive tests - known false negatives on Java8+</description>
|
||||
<expected-problems>2</expected-problems>
|
||||
<expected-linenumbers>6,8</expected-linenumbers>
|
||||
@ -187,8 +188,9 @@ public class Foo {
|
||||
|
||||
<test-code>
|
||||
<description>False negative for nested type parameters (#2545)</description>
|
||||
<expected-problems>2</expected-problems>
|
||||
<expected-linenumbers>7,16</expected-linenumbers>
|
||||
<rule-property name="java7compatibility">true</rule-property>
|
||||
<expected-problems>3</expected-problems>
|
||||
<expected-linenumbers>7,8,17</expected-linenumbers>
|
||||
<code><![CDATA[
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
@ -197,7 +199,8 @@ import java.lang.ref.WeakReference;
|
||||
|
||||
public class UseDiamondOperatorFalseNegative {
|
||||
List<Map<String,Object>> l = new ArrayList<Map<String,Object>>(); // FN
|
||||
WeakReference<Class<?>> typeReference = new WeakReference<Class<?>>(String.class); // FP
|
||||
WeakReference<Class<String>> typeReference = new WeakReference<Class<String>>(String.class); // FN
|
||||
WeakReference<Class<?>> typeReference2 = new WeakReference<Class<?>>(String.class); // FP
|
||||
|
||||
public void test() {
|
||||
final List<String> l2;
|
||||
@ -211,7 +214,7 @@ public class UseDiamondOperatorFalseNegative {
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code regressionTest="false">
|
||||
<test-code>
|
||||
<description>(J8+) False negative for Java8+ and ternary operator (#2545)</description>
|
||||
<expected-problems>2</expected-problems>
|
||||
<expected-linenumbers>7,7</expected-linenumbers>
|
||||
|
Reference in New Issue
Block a user