Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 568088

Summary: [AutoRefactor immigration #37/141] [cleanup & saveaction] try-with-resource
Product: [Eclipse Project] JDT Reporter: Fabrice Tiercelin <fabrice.tiercelin>
Component: UIAssignee: Fabrice Tiercelin <fabrice.tiercelin>
Status: VERIFIED FIXED QA Contact: Lars Vogel <Lars.Vogel>
Severity: enhancement    
Priority: P3    
Version: 4.17   
Target Milestone: 4.18 M3   
Hardware: All   
OS: All   
See Also: https://git.eclipse.org/r/c/jdt/eclipse.jdt.ui/+/171097
https://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=b4d4824ed84c5dd3f8af41448ac1c2ced44cde76
https://git.eclipse.org/r/c/www.eclipse.org/eclipse/news/+/172522
https://git.eclipse.org/c/www.eclipse.org/eclipse/news.git/commit/?id=0db30a015a92d9d6b742193f57590a2b92cdfe4d
Whiteboard:
Bug Depends on:    
Bug Blocks: 566539    

Description Fabrice Tiercelin CLA 2020-10-21 23:48:29 EDT
Changes code to make use of Java 7 try-with-resources feature. In particular, it removes now useless finally clauses.

Given:
final FileInputStream inputStream = new FileInputStream("out.txt");
try {
    System.out.println(inputStream.read());
} finally {
    inputStream.close();
}

When:
Clean up the code enabling "Use try-with-resource (1.7 or higher)"

Then:
try (FileInputStream inputStream = new FileInputStream("out.txt")) {
    System.out.println(inputStream.read());
}


This feature is a part of the AutoRefactor plugin immigration into Eclipse
+--------------------------------------------------------------------+-----+
| Rule in AutoRefactor                                               |     |
+--------------------------------------------------------------------+-----+
| Add brackets to control statement                                  | OK  |
| All in one method rather than loop                                 | OK  |
| Arrays.fill() rather than loop                                     | OK  |
| Assign rather than control workflow then assign anyway             | OK  |
| Atomic object rather than mono index array                         | OK  |
| AutoBoxing rather than explicit method                             | OK  |
| Collapse if statements                                             | OK  |
| Inited collection rather than new collection and Collection.add... | OK  |
| Diamond operator                                                   | OK  |
| End of method rather than return                                   | OK  |
| Improve lambda expressions                                         | OK  |
| Inited map rather than new map and Map.putAll()                    | OK  |
| Java 7 hash rather than Eclipse Java 6 hash                        | OK  |
| Lazy logical rather than eager                                     | OK  |
| Literal rather than boolean constant                               | OK  |
| Method on map rather than method on keyset                         | OK  |
| Merge conditional statements                                       | OK  |
| Number suffix in uppercase                                         | OK  |
| Objects equals rather than equals and null check                   | OK  |
| One code that falls through rather than redundant blocks           | OK  |
| Precompiles the regular expressions                                | OK  |
| Push negation down                                                 | OK  |
| Remove semi-colons                                                 | OK  |
| Remove super() call in constructor                                 | OK  |
| Remove unnecessary casts                                           | OK  |
| Remove unneeded this expressions                                   | OK  |
| Remove useless lone continue at the end of a loop                  | OK  |
| Remove useless modifiers                                           | OK  |
| Simple name rather than qualified name                             | OK  |
| String rather than new string                                      | OK  |
| String.join() rather than loop                                     | OK  |
| Unboxing rather than explicit method                               | OK  |
| Break rather than passive loops                                    | ... |
| if-elseif                                                          | ... |
| Remove overridden assignment                                       | ... |
| Use try-with-resource                                              | ... |
| Add underscore for each thousand in number literals when it is...  | ko  |
| Aggregate constructor rather than GWT method                       | ko  |
| Android ViewHolder                                                 | ko  |
| Android WakeLock                                                   | ko  |
| Annotation                                                         | ko  |
| ArrayDeque rather than Stack                                       | ko  |
| ArrayList rather than LinkedList                                   | ko  |
| ArrayList rather than Vector                                       | ko  |
| AssertJ                                                            | ko  |
| Assign rather than ternary filter then assign anyway               | ko  |
| Big number                                                         | ko  |
| Boolean                                                            | ko  |
| Boolean constant rather than valueOf()                             | ko  |
| Boolean equals() rather than null check                            | ko  |
| Boolean primitive rather than wrapper                              | ko  |
| Brackets rather than array instantiation                           | ko  |
| Byte primitive rather than wrapper                                 | ko  |
| Char primitive rather than wrapper                                 | ko  |
| Collection.addAll() rather than list creation                      | ko  |
| Collection.contains() rather than loop                             | ko  |
| Collection.containsAll() rather than loop                          | ko  |
| Collections APIs rather than Vector pre-Collections APIs           | ko  |
| Comments                                                           | ko  |
| Comparison to 0 rather than 1 or -1                                | ko  |
| Declaration outside loop rather than inside                        | ko  |
| Do/while rather than duplicate code                                | ko  |
| Do/while rather than while                                         | ko  |
| Double compare rather than equality                                | ko  |
| Double primitive rather than wrapper                               | ko  |
| Else rather than opposite condition                                | ko  |
| Empty test rather than size                                        | ko  |
| EnumMap rather than HashMap for enum keys                          | ko  |
| EnumSet rather than HashSet for enum types                         | ko  |
| Equals on constant rather than on variable                         | ko  |
| Extract common code in if else statement                           | ko  |
| Float primitive rather than wrapper                                | ko  |
| Generic list rather than raw list                                  | ko  |
| Generic map rather than raw map                                    | ko  |
| HashMap rather than Hashtable                                      | ko  |
| HashMap rather than TreeMap                                        | ko  |
| HashSet rather than TreeSet                                        | ko  |
| If rather than two switch cases                                    | ko  |
| If rather than while and falls through                             | ko  |
| Implicit default constructor rather than written one               | ko  |
| Inline code rather than peremptory condition                       | ko  |
| instanceof rather than isInstance()                                | ko  |
| Int primitive rather than wrapper                                  | ko  |
| JUnit asserts                                                      | ko  |
| Jupiter asserts                                                    | ko  |
| Lambda expression rather than comparator                           | ko  |
| Local variable rather than field                                   | ko  |
| Log parameters rather than log message                             | ko  |
| Long primitive rather than wrapper                                 | ko  |
| Make inner class static if it doesn't use top level class members  | ko  |
| Map.entrySet() rather than Map.keySet() and value search           | ko  |
| Move common inner if statement from then/else clauses around ou... | ko  |
| Moves increment or decrement outside an expression when possible   | ko  |
| Multi-catch                                                        | ko  |
| Named method rather than log level parameter                       | ko  |
| No assignment in if condition                                      | ko  |
| No loop iteration rather than empty check                          | ko  |
| One condition rather than unreachable block                        | ko  |
| One if rather than duplicate blocks that fall through              | ko  |
| One try rather than two                                            | ko  |
| Opposite comparison rather than negative expression                | ko  |
| Opposite condition rather than duplicate condition                 | ko  |
| OR condition rather than redundant clauses                         | ko  |
| Primitive comparison rather than wrapper comparison                | ko  |
| Primitive wrapper creation                                         | ko  |
| Reduce indentation                                                 | ko  |
| Refactors a true or a false assertion with respectively an AND     | ko  |
| Remove empty if                                                    | ko  |
| Remove empty lines                                                 | ko  |
| Remove fields default values                                       | ko  |
| Remove parenthesis                                                 | ko  |
| Remove unchecked exceptions from throws clause                     | ko  |
| Remove unnecessary local before return                             | ko  |
| Remove useless block                                               | ko  |
| Remove useless empty check before a for loop                       | ko  |
| Remove empty statements                                            | ko  |
| Replace for loop with Collections.disjoint(Collection, Collection) | ko  |
| Replace String concatenation by StringBuilder when possible        | ko  |
| Set rather than List                                               | ko  |
| Set rather than map                                                | ko  |
| Short primitive rather than wrapper                                | ko  |
| Simplify expressions                                               | ko  |
| Single declarations rather than multi declaration                  | ko  |
| Standard method rather than Library method                         | ko  |
| Static constant rather than instance constant                      | ko  |
| String                                                             | ko  |
| String.valueOf() rather than concatenation                         | ko  |
| StringBuilder                                                      | ko  |
| StringBuilder method call rather than reassignment                 | ko  |
| StringBuilder rather than StringBuffer                             | ko  |
| substring() with one parameter rather than two                     | ko  |
| Super call rather than useless overriding                          | ko  |
| Switch                                                             | ko  |
| Ternary operator rather than duplicate conditions                  | ko  |
| TestNG asserts                                                     | ko  |
| Update set rather than testing first                               | ko  |
| Use java.nio.* classes instead of java.io.* classes                | ko  |
| Use String.contains()                                              | ko  |
| Variable inside if rather than above                               | ko  |
| While condition rather than inner if                               | ko  |
| XOR rather than duplicate conditions                               | ko  |
+--------------------------------------------------------------------+-----+
Comment 1 Eclipse Genie CLA 2020-10-21 23:49:29 EDT
New Gerrit change created: https://git.eclipse.org/r/c/jdt/eclipse.jdt.ui/+/171097
Comment 3 Eclipse Genie CLA 2020-11-19 12:37:18 EST
New Gerrit change created: https://git.eclipse.org/r/c/www.eclipse.org/eclipse/news/+/172522
Comment 5 Fabrice Tiercelin CLA 2020-11-19 12:39:59 EST
Verified for 4.18M3 using I20201116-1800 build