From 928fc37ecfc58a372350d9fd18568168b5f78ad0 Mon Sep 17 00:00:00 2001 From: "U-BASIS\\zhaohui" Date: Tue, 27 Feb 2018 17:56:00 -0500 Subject: [PATCH 1/4] 2229: Use getOpenCase() instead of getCurrentCase() and throws checked excepiton: NoCurrentCaseException. --- .../sleuthkit/autopsy/casemodule/Case.java | 32 ++++++++++--- .../casemodule/NoCurrentCaseException.java | 47 +++++++++++++++++++ 2 files changed, 73 insertions(+), 6 deletions(-) create mode 100755 Core/src/org/sleuthkit/autopsy/casemodule/NoCurrentCaseException.java diff --git a/Core/src/org/sleuthkit/autopsy/casemodule/Case.java b/Core/src/org/sleuthkit/autopsy/casemodule/Case.java index b9665ab811..4529a0ea3f 100644 --- a/Core/src/org/sleuthkit/autopsy/casemodule/Case.java +++ b/Core/src/org/sleuthkit/autopsy/casemodule/Case.java @@ -583,24 +583,44 @@ public class Case { } /** + * Deprecated. Use getOpenCase() instead. + * * Gets the current case, if there is one, at the time of the call. * * @return The current case. * * @throws IllegalStateException if there is no current case. */ + @Deprecated public static Case getCurrentCase() { /* * Throwing an unchecked exception is a bad idea here. * - * TODO (JIRA-2229): Case.getCurrentCase() method throws unchecked - * IllegalStateException; change to throw checked exception or return - * null */ - if (null != currentCase) { - return currentCase; + try { + Case curCase = getOpenCase(); + return curCase; + } catch (NoCurrentCaseException e) { + throw new IllegalStateException(NbBundle.getMessage(Case.class, "Case.getCurCase.exception.noneOpen"), e); + } + } + + /** + * Gets the current open case, if there is one, at the time of the call. + * + * @return The open case. + * + * @throws NoCurrentCaseException if there is no open case. + */ + @Messages({ + "Case.NoCurrentCaseException.message=No open case available." + }) + public static Case getOpenCase() throws NoCurrentCaseException { + Case openCase = currentCase; + if (null != openCase) { + return openCase; } else { - throw new IllegalStateException(NbBundle.getMessage(Case.class, "Case.getCurCase.exception.noneOpen")); + throw new NoCurrentCaseException(Bundle.Case_NoCurrentCaseException_message()); } } diff --git a/Core/src/org/sleuthkit/autopsy/casemodule/NoCurrentCaseException.java b/Core/src/org/sleuthkit/autopsy/casemodule/NoCurrentCaseException.java new file mode 100755 index 0000000000..b52efe8992 --- /dev/null +++ b/Core/src/org/sleuthkit/autopsy/casemodule/NoCurrentCaseException.java @@ -0,0 +1,47 @@ +/* + * Autopsy Forensic Browser + * + * Copyright 2018 Basis Technology Corp. + * Contact: carrier sleuthkit org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.sleuthkit.autopsy.casemodule; + + +/** + * + * Exception thrown when no current case is available + */ +public class NoCurrentCaseException extends Exception { + private static final long serialVersionUID = 1L; + /** + * Constructs an exception with the specified message. + * + * @param message The exception message. + */ + public NoCurrentCaseException(String message) { + super(message); + } + + /** + * Constructs an exception with the specified message and cause. + * + * @param message The exception message. + * @param cause The exception cause. + */ + public NoCurrentCaseException(String message, Throwable cause) { + super(message, cause); + } + +} From 94736502b16c3128fa3671c884d4372e6abf1719 Mon Sep 17 00:00:00 2001 From: "U-BASIS\\zhaohui" Date: Tue, 27 Feb 2018 18:15:38 -0500 Subject: [PATCH 2/4] 2229: Simple return the value instead of storing it in a local variable. --- Core/src/org/sleuthkit/autopsy/casemodule/Case.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/casemodule/Case.java b/Core/src/org/sleuthkit/autopsy/casemodule/Case.java index 4529a0ea3f..aba9d190be 100644 --- a/Core/src/org/sleuthkit/autopsy/casemodule/Case.java +++ b/Core/src/org/sleuthkit/autopsy/casemodule/Case.java @@ -598,8 +598,7 @@ public class Case { * */ try { - Case curCase = getOpenCase(); - return curCase; + return getOpenCase(); } catch (NoCurrentCaseException e) { throw new IllegalStateException(NbBundle.getMessage(Case.class, "Case.getCurCase.exception.noneOpen"), e); } From 8d9cbf832076de253a06f60d118be6b1a8a5afba Mon Sep 17 00:00:00 2001 From: "U-BASIS\\zhaohui" Date: Wed, 28 Feb 2018 10:21:30 -0500 Subject: [PATCH 3/4] 2229: Simply follow Codacy suggestion to avoid negation within an 'if' expression. --- Core/src/org/sleuthkit/autopsy/casemodule/Case.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/casemodule/Case.java b/Core/src/org/sleuthkit/autopsy/casemodule/Case.java index aba9d190be..8cbda83ee2 100644 --- a/Core/src/org/sleuthkit/autopsy/casemodule/Case.java +++ b/Core/src/org/sleuthkit/autopsy/casemodule/Case.java @@ -616,10 +616,10 @@ public class Case { }) public static Case getOpenCase() throws NoCurrentCaseException { Case openCase = currentCase; - if (null != openCase) { - return openCase; - } else { + if (openCase == null) { throw new NoCurrentCaseException(Bundle.Case_NoCurrentCaseException_message()); + } else { + return openCase; } } From 51f1d7044f48e90610b5817628299d383f6ccba9 Mon Sep 17 00:00:00 2001 From: "U-BASIS\\zhaohui" Date: Tue, 6 Mar 2018 11:12:55 -0500 Subject: [PATCH 4/4] 2229: Part 1. Fix the document comment of deprecated method and a codacy issue. --- Core/src/org/sleuthkit/autopsy/casemodule/Case.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/casemodule/Case.java b/Core/src/org/sleuthkit/autopsy/casemodule/Case.java index 8cbda83ee2..abfea4c965 100644 --- a/Core/src/org/sleuthkit/autopsy/casemodule/Case.java +++ b/Core/src/org/sleuthkit/autopsy/casemodule/Case.java @@ -583,14 +583,14 @@ public class Case { } /** - * Deprecated. Use getOpenCase() instead. - * * Gets the current case, if there is one, at the time of the call. * * @return The current case. * * @throws IllegalStateException if there is no current case. - */ + * + * @deprecated. Use getOpenCase() instead. + */ @Deprecated public static Case getCurrentCase() { /* @@ -617,7 +617,7 @@ public class Case { public static Case getOpenCase() throws NoCurrentCaseException { Case openCase = currentCase; if (openCase == null) { - throw new NoCurrentCaseException(Bundle.Case_NoCurrentCaseException_message()); + throw new NoCurrentCaseException(Case_NoCurrentCaseException_message()); } else { return openCase; }