From 5894b434aaf30933c26eb44689970926d274ed19 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Tue, 11 Aug 2015 13:29:24 -0700 Subject: [PATCH] Use try-with-resources and multi-catch exceptions in IMMS. This CL applies new Java 7 language features try-with-resources and multi-catch exceptions to InputMethodManagerService. Basically this is a compile-time replacement hence there should be neither behavior difference nor performance impact. Bug: 22285167 Change-Id: I971bd12b63649802859410ee6a91351b1261f055 --- .../server/InputMethodManagerService.java | 27 +++---------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/services/core/java/com/android/server/InputMethodManagerService.java b/services/core/java/com/android/server/InputMethodManagerService.java index e26b34dcf605d..f9f617ef4c267 100644 --- a/services/core/java/com/android/server/InputMethodManagerService.java +++ b/services/core/java/com/android/server/InputMethodManagerService.java @@ -2923,10 +2923,7 @@ public class InputMethodManagerService extends IInputMethodManager.Stub if (DEBUG) { Slog.d(TAG, "Found an input method " + p); } - - } catch (XmlPullParserException e) { - Slog.w(TAG, "Unable to load input method " + compName, e); - } catch (IOException e) { + } catch (XmlPullParserException | IOException e) { Slog.w(TAG, "Unable to load input method " + compName, e); } } @@ -3635,9 +3632,7 @@ public class InputMethodManagerService extends IInputMethodManager.Stub HashMap> allSubtypes, AtomicFile subtypesFile) { if (allSubtypes == null || subtypesFile == null) return; allSubtypes.clear(); - FileInputStream fis = null; - try { - fis = subtypesFile.openRead(); + try (final FileInputStream fis = subtypesFile.openRead()) { final XmlPullParser parser = Xml.newPullParser(); parser.setInput(fis, StandardCharsets.UTF_8.name()); int type = parser.getEventType(); @@ -3692,23 +3687,9 @@ public class InputMethodManagerService extends IInputMethodManager.Stub tempSubtypesArray.add(subtype); } } - } catch (XmlPullParserException e) { - Slog.w(TAG, "Error reading subtypes: " + e); + } catch (XmlPullParserException | IOException | NumberFormatException e) { + Slog.w(TAG, "Error reading subtypes", e); return; - } catch (java.io.IOException e) { - Slog.w(TAG, "Error reading subtypes: " + e); - return; - } catch (NumberFormatException e) { - Slog.w(TAG, "Error reading subtypes: " + e); - return; - } finally { - if (fis != null) { - try { - fis.close(); - } catch (java.io.IOException e1) { - Slog.w(TAG, "Failed to close."); - } - } } } }