From fc089ebcb28b26c55bc44dc57474f1d058df342f Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 15 Mar 2021 11:48:47 +0700 Subject: [PATCH] [java] Inspection description updated GitOrigin-RevId: 264a3590ff70d79e5645d7a5ac7c804541bc4bb2 --- .../EnhancedSwitchMigration.html | 30 ++++++++++++++-- .../ForwardCompatibility.html | 11 ++++-- .../inspectionDescriptions/StreamToLoop.html | 36 ++++++++++++++++++- .../ConstantConditionalExpression.html | 13 ++++--- .../DoubleNegation.html | 15 +++++--- .../inspectionDescriptions/EmptyTryBlock.html | 4 +-- .../ForCanBeForeach.html | 31 +++++++++++----- .../UnnecessaryInitCause.html | 27 ++++++++++++-- 8 files changed, 140 insertions(+), 27 deletions(-) diff --git a/java/java-impl/src/inspectionDescriptions/EnhancedSwitchMigration.html b/java/java-impl/src/inspectionDescriptions/EnhancedSwitchMigration.html index efe8801ad8b2..1056f12ee153 100644 --- a/java/java-impl/src/inspectionDescriptions/EnhancedSwitchMigration.html +++ b/java/java-impl/src/inspectionDescriptions/EnhancedSwitchMigration.html @@ -1,7 +1,33 @@ -Reports switch statements which can be replaced with an enhanced switch statement or expression. - +Reports switch statements which can be automatically replaced with an enhanced switch statement or expression. +

Example:

+
+  double getPrice(String fruit) {
+    // Switch statement can be replaced with enhanced 'switch'
+    switch (fruit) {
+      case "Apple":
+        return 1.0;
+      case "Orange":
+        return 1.5;
+      case "Mango":
+        return 2.0;
+      default:
+        throw new IllegalArgumentException();
+    }
+  }
+
+

After the quick-fix is applied, the code will look like this:

+
+  double getPrice(String fruit) {
+    return switch (fruit) {
+      case "Apple" -> 1.0;
+      case "Orange" -> 1.5;
+      case "Mango" -> 2.0;
+      default -> throw new IllegalArgumentException();
+    };
+  }
+

This inspection only applies to language level 14 or higher

New in 2019.1

diff --git a/java/java-impl/src/inspectionDescriptions/ForwardCompatibility.html b/java/java-impl/src/inspectionDescriptions/ForwardCompatibility.html index 52cb46618134..9329547af239 100644 --- a/java/java-impl/src/inspectionDescriptions/ForwardCompatibility.html +++ b/java/java-impl/src/inspectionDescriptions/ForwardCompatibility.html @@ -1,12 +1,19 @@ -Reports the Java code constructs that may fail to compile in future Java versions. The following problems are reported: +Reports the Java code constructs that may fail to compile in future Java versions. +The following problems are reported: +

Example:

+
+  // Since Java 14, this class will stop compiling, as 'yield' becomes a restricted identifier.
+  public class yield {} 
+
+

Fixing these issues timely may simplify migration to future Java versions.

\ No newline at end of file diff --git a/java/java-impl/src/inspectionDescriptions/StreamToLoop.html b/java/java-impl/src/inspectionDescriptions/StreamToLoop.html index 18993e5bf82d..252ac5a2da4a 100644 --- a/java/java-impl/src/inspectionDescriptions/StreamToLoop.html +++ b/java/java-impl/src/inspectionDescriptions/StreamToLoop.html @@ -1,12 +1,46 @@ -Finds Stream API chains, Iterable.forEach and Map.forEach calls and provides quick fix to convert them into classical loops. +Reports Stream API chains, Iterable.forEach and Map.forEach calls that can be automatically converted into classical loops. +

Example:

+
+  String joinNonEmpty(List<String> list) {
+    return list.stream() // Stream can be converted to loop
+      .filter(s -> !s.isEmpty())
+      .map(String::trim)
+      .collect(Collectors.joining(", "));
+  }
+
+

After the fix is applied, the result will be like this:

+
+  String joinNonEmpty(List<String> list) {
+    StringJoiner joiner = new StringJoiner(", ");
+    for (String s : list) {
+      if (!s.isEmpty()) {
+        String trim = s.trim();
+        joiner.add(trim);
+      }
+    }
+    return joiner.toString();
+  }
+

Note that sometimes this inspection might cause slight semantic changes. Special care should be taken when it comes to short-circuiting, as it's not specified how many elements will be actually read when the stream short-circuits.

+

Use checkbox below to suggest conversions for streams with unrecognized source. In this case, iterator will be created from the stream. + For example, when checkbox is checked, the conversion will be suggested here:

+
+  List<ProcessHandle> handles = ProcessHandle.allProcesses().collect(Collectors.toList());
+

In this case, the result will be like this:

+
+  List<ProcessHandle> handles = new ArrayList<>();
+  for (Iterator<ProcessHandle> it = ProcessHandle.allProcesses().iterator(); it.hasNext(); ) {
+    ProcessHandle allProcess = it.next();
+    handles.add(allProcess);
+  }
+

New in 2017.1

\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/ConstantConditionalExpression.html b/plugins/InspectionGadgets/src/inspectionDescriptions/ConstantConditionalExpression.html index 1cf472796e2f..8d5728256cb9 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/ConstantConditionalExpression.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/ConstantConditionalExpression.html @@ -1,10 +1,15 @@ -Reports conditional expressions of the form -true?result1:result2 or false?result1:result2. +Reports conditional expressions where the condition is either true or false constant. These expressions sometimes occur as the result of automatic refactorings, and may obviously be simplified. +

Example:

+
+  return true ? "Yes" : "No";
+
+

After quick-fix is applied, the result will be

+
+  return "Yes";
+
-

- \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/DoubleNegation.html b/plugins/InspectionGadgets/src/inspectionDescriptions/DoubleNegation.html index c99a02bc1cbd..154f8d7a43b0 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/DoubleNegation.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/DoubleNegation.html @@ -1,9 +1,16 @@ -Reports double negation. -

-For example: -

if (!!functionCall())
+Reports double negation that can be simplified. +

Examples:

+
+  if (!!functionCall()) {} // double negation
+  if (!(a != b)) {} // double negation
+
+

After quick-fix is applied, the result will be

+
+  if (functionCall()) {}
+  if (a == b) {}
+

diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/EmptyTryBlock.html b/plugins/InspectionGadgets/src/inspectionDescriptions/EmptyTryBlock.html index c3fcf8e8179b..f55f2748053d 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/EmptyTryBlock.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/EmptyTryBlock.html @@ -1,9 +1,9 @@ -Reports empty try blocks. +Reports empty try blocks, including try-with-resources statements.

-At present, this inspection is disabled in JSP files. +This inspection does not report in JSP files.

diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/ForCanBeForeach.html b/plugins/InspectionGadgets/src/inspectionDescriptions/ForCanBeForeach.html index 79312654409e..1eb74a123101 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/ForCanBeForeach.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/ForCanBeForeach.html @@ -1,18 +1,31 @@ -Reports for loops which iterate -over collections or arrays, and can be replaced with an enhanced for loop (i.e. the foreach iteration syntax). +Reports for loops that iterate over collections or arrays, +and can be automatically replaced with an enhanced for loop (i.e. the foreach iteration syntax). +

Example:

+
+  for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
+    String item = iterator.next();
+    System.out.println(item);
+  }
+
+

After the quick-fix is applied the result looks like:

+
+  for (String item : list) {
+    System.out.println(item);
+  }
+

-Use the first checkbox below to find loops involving list.get(index) calls. -These loops generally can be replaced with an enhanced for loop, unless they modify underlying list in the process, e.g. by calling list.remove(index). -If latter is the case, the enhanced for loop may throw ConcurrentModificationException. + Use the first checkbox below to find loops involving list.get(index) calls. + These loops generally can be replaced with an enhanced for loop, + unless they modify underlying list in the process, e.g. by calling list.remove(index). + If latter is the case, the enhanced for loop may throw ConcurrentModificationException. + Also, in some cases, list.get(index) loops may work a little bit faster.

-Use the second checkbox below to ignore collections which do not have type parameters. This prevents the creation of -enhanced for loop variables of type java.lang.Object and the insertion of casts where the loop variable is used. + Use the second checkbox below to ignore collections which do not have type parameters. This prevents the creation of + enhanced for loop variables of type java.lang.Object and the insertion of casts where the loop variable is used.

This inspection only reports if the language level of the project or module is 5 or higher

-

- \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryInitCause.html b/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryInitCause.html index 68afd2950b11..9552025780f8 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryInitCause.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryInitCause.html @@ -1,9 +1,30 @@ -Reports any calls to Throwable.initCause() where the exception constructor also takes a Throwable cause argument. -In this case the initCause() call can be removed and its argument added to the call to the exception's constructor. +Reports any calls to Throwable.initCause() where the exception constructor also takes a Throwable cause argument. +In this case, the initCause() call can be removed and its argument added to the call to the exception's constructor. +

Example:

+
+  try {
+      process();
+  }
+  catch (RuntimeException ex) {
+    RuntimeException wrapper = new RuntimeException("Error while processing");
+    wrapper.initCause(ex); // Unnecessary call to 'Throwable.initCause()'
+    throw wrapper;
+  }
+
+

A quick-fix is provided to pass the cause to constructor:

+
+  try {
+      process();
+  }
+  catch (RuntimeException ex) {
+    RuntimeException wrapper = new RuntimeException("Error while processing", ex);
+    throw wrapper;
+  }
+

-New in 16 +New in 2016.1 \ No newline at end of file