Skip to content

Commit

Permalink
Cleanup: Combine same body catch blocks (spotbugs#3223)
Browse files Browse the repository at this point in the history
* combine same body catch blocks

* add changelog
  • Loading branch information
JuditKnoll authored Dec 10, 2024
1 parent 0594c1a commit 1503920
Show file tree
Hide file tree
Showing 49 changed files with 57 additions and 206 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Currently the versioning policy of this project follows [Semantic Versioning v2.
- Remove unnecessary throws declarations ([#3220](https://github.com/spotbugs/spotbugs/pull/3220))
- Use `Boolean.parseBoolean()` for string-to-boolean conversion. ([#3217](https://github.com/spotbugs/spotbugs/pull/3217))
- Rename shadowing fields ([#3221](https://github.com/spotbugs/spotbugs/pull/3221))
- Combine catch blocks with the same body ([#3223](https://github.com/spotbugs/spotbugs/pull/3223))

### Changed
- Bump up Java version to 11
Expand Down
4 changes: 1 addition & 3 deletions eclipsePlugin/src/de/tobject/findbugs/FindBugsJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,14 @@ public IStatus run(IProgressMonitor monitor) {
}

runWithProgress(monitor);
} catch (OperationCanceledException e) {
} catch (OperationCanceledException | InterruptedException e) {
// Do nothing when operation cancelled.
return Status.CANCEL_STATUS;
} catch (CoreException ex) {
if (DEBUG) {
FindbugsPlugin.getDefault().logException(ex, createErrorMessage());
}
return ex.getStatus();
} catch (InterruptedException e) {
return Status.CANCEL_STATUS;
} finally {
if (acquired) {
if (DEBUG) {
Expand Down
5 changes: 1 addition & 4 deletions eclipsePlugin/src/de/tobject/findbugs/FindbugsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -657,10 +657,7 @@ public static SortedBugCollection getBugCollection(IProject project, IProgressMo
try {
readBugCollectionAndProject(project, monitor);
bugCollection = (SortedBugCollection) project.getSessionProperty(SESSION_PROPERTY_BUG_COLLECTION);
} catch (IOException e) {
FindbugsPlugin.getDefault().logException(e, "Could not read bug collection for project");
bugCollection = createDefaultEmptyBugCollection(project);
} catch (DocumentException e) {
} catch (IOException | DocumentException e) {
FindbugsPlugin.getDefault().logException(e, "Could not read bug collection for project");
bugCollection = createDefaultEmptyBugCollection(project);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ private IEditorPart openEditor(File file) {
fileStore = EFS.getLocalFileSystem().getStore(new Path(file.getCanonicalPath()));
IEditorInput input = new FileStoreEditorInput(fileStore);
return page.openEditor(input, editorId);
} catch (IOException e) {
FindbugsPlugin.getDefault().logException(e, "Could not get canonical file path");
} catch (CoreException e) {
} catch (IOException | CoreException e) {
FindbugsPlugin.getDefault().logException(e, "Could not get canonical file path");
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ private static List<String> readArgumentsFile(String textFile) {
while ((next = reader.readLine()) != null) {
compiledXmlFiles.add(next);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public void execute(Runnable command) {
}
try {
SwingUtilities.invokeAndWait(command);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
} catch (InvocationTargetException e) {
} catch (InterruptedException | InvocationTargetException e) {
throw new IllegalStateException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ public void visitClassContext(ClassContext classContext) {
reportMatch(classContext, method, match);
}
}
} catch (DataflowAnalysisException e) {
getBugReporter().logError(getDetectorName() + " caught exception", e);
} catch (CFGBuilderException e) {
} catch (DataflowAnalysisException | CFGBuilderException e) {
getBugReporter().logError(getDetectorName() + " caught exception", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,7 @@ private List<String> findFullyQualifiedSourceFileNames(IClassPathBuilder builder
try {
String fullyQualifiedSourceFileName = findFullyQualifiedSourceFileName(classPath, classDesc);
fullyQualifiedSourceFileNameList.add(fullyQualifiedSourceFileName);
} catch (IOException e) {
errorLogger.logError("Couldn't scan class " + classDesc.toDottedClassName(), e);
} catch (CheckedAnalysisException e) {
} catch (IOException | CheckedAnalysisException e) {
errorLogger.logError("Couldn't scan class " + classDesc.toDottedClassName(), e);
}
}
Expand Down
12 changes: 2 additions & 10 deletions spotbugs/src/main/java/edu/umd/cs/findbugs/FindBugs2.java
Original file line number Diff line number Diff line change
Expand Up @@ -662,12 +662,7 @@ public static void registerPluginAnalysisEngines(DetectorFactoryCollection detec
try {
IAnalysisEngineRegistrar engineRegistrar = engineRegistrarClass.newInstance();
engineRegistrar.registerAnalysisEngines(analysisCache);
} catch (InstantiationException e) {
IOException ioe = new IOException("Could not create analysis engine registrar for plugin "
+ plugin.getPluginId());
ioe.initCause(e);
throw ioe;
} catch (IllegalAccessException e) {
} catch (InstantiationException | IllegalAccessException e) {
IOException ioe = new IOException("Could not create analysis engine registrar for plugin "
+ plugin.getPluginId());
ioe.initCause(e);
Expand Down Expand Up @@ -999,10 +994,7 @@ private void analyzeApplication() throws InterruptedException {
try {
XClass info = Global.getAnalysisCache().getClassAnalysis(XClass.class, desc);
factory.intern(info);
} catch (CheckedAnalysisException e) {
AnalysisContext.logError("Couldn't get class info for " + desc, e);
badClasses.add(desc);
} catch (RuntimeException e) {
} catch (CheckedAnalysisException | RuntimeException e) {
AnalysisContext.logError("Couldn't get class info for " + desc, e);
badClasses.add(desc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,7 @@ public boolean isSignificant() {
}
}
return match;
} catch (DataflowAnalysisException e) {
AnalysisContext.logError("", e);
} catch (CFGBuilderException e) {
} catch (DataflowAnalysisException | CFGBuilderException e) {
AnalysisContext.logError("", e);
}
return null;
Expand Down
4 changes: 1 addition & 3 deletions spotbugs/src/main/java/edu/umd/cs/findbugs/PluginLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1618,9 +1618,7 @@ public static Summary validate(File file) throws IllegalArgumentException {
String shortDesc = findMessageText(msgDocuments,
XPATH_PLUGIN_SHORT_DESCRIPTION, "");
return new Summary(pluginId, shortDesc, provider, website);
} catch (DocumentException e) {
throw new IllegalArgumentException(e);
} catch (IOException e) {
} catch (DocumentException | IOException e) {
throw new IllegalArgumentException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ public boolean getBoolean(String name, boolean defaultValue) {
return defaultValue;
}
result = toBoolean(value);
} catch (IllegalArgumentException e) {
} catch (NullPointerException e) {
} catch (IllegalArgumentException | NullPointerException ignored) {
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ public void visitClassContext(ClassContext classContext) {
}

analyzeMethod(classContext, method, resourceTracker, resourceCollection);
} catch (CFGBuilderException e) {
bugReporter.logError("Error analyzing method " + method.toString(), e);
} catch (DataflowAnalysisException e) {
} catch (CFGBuilderException | DataflowAnalysisException e) {
bugReporter.logError("Error analyzing method " + method.toString(), e);
}
bugAccumulator.reportAccumulatedBugs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,7 @@ private void doReadXML(@WillClose InputStream in, @CheckForNull File base) throw
checkInputStream(in);
Reader reader = Util.getReader(in);
doReadXML(reader, base);
} catch (RuntimeException e) {
in.close();
throw e;
} catch (IOException e) {
} catch (RuntimeException | IOException e) {
in.close();
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ public static boolean getBoolean(String name, boolean defaultValue) {
return defaultValue;
}
result = toBoolean(value);
} catch (IllegalArgumentException e) {
} catch (NullPointerException e) {
} catch (IllegalArgumentException | NullPointerException ignored) {
}
return result;
}
Expand Down
16 changes: 3 additions & 13 deletions spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ClassContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -882,27 +882,19 @@ public void dumpSimpleDataflowInformation(Method method) {
try {
dumpDataflowInformation(method, getCFG(method), getValueNumberDataflow(method), getIsNullValueDataflow(method), null,
null);
} catch (DataflowAnalysisException e) {
} catch (DataflowAnalysisException | CFGBuilderException e) {
AnalysisContext.logError(
"Could not dump data information for " + getJavaClass().getClassName() + "." + method.getName(), e);
} catch (CFGBuilderException e) {
AnalysisContext.logError(
"Could not dump data information for " + getJavaClass().getClassName() + "." + method.getName(), e);

}
}

public void dumpDataflowInformation(Method method) {
try {
dumpDataflowInformation(method, getCFG(method), getValueNumberDataflow(method), getIsNullValueDataflow(method),
getUnconditionalValueDerefDataflow(method), getTypeDataflow(method));
} catch (DataflowAnalysisException e) {
AnalysisContext.logError(
"Could not dump data information for " + getJavaClass().getClassName() + "." + method.getName(), e);
} catch (CFGBuilderException e) {
} catch (DataflowAnalysisException | CFGBuilderException e) {
AnalysisContext.logError(
"Could not dump data information for " + getJavaClass().getClassName() + "." + method.getName(), e);

}
}

Expand Down Expand Up @@ -1008,9 +1000,7 @@ private <Analysis> Analysis getMethodAnalysis(Class<Analysis> analysisClass, Met
try {
MethodDescriptor methodDescriptor = BCELUtil.getMethodDescriptor(jclass, method);
return Global.getAnalysisCache().getMethodAnalysis(analysisClass, methodDescriptor);
} catch (DataflowAnalysisException e) {
throw e;
} catch (CFGBuilderException e) {
} catch (DataflowAnalysisException | CFGBuilderException e) {
throw e;
} catch (CheckedAnalysisException e) {
Throwable cause = e.getCause();
Expand Down
4 changes: 1 addition & 3 deletions spotbugs/src/main/java/edu/umd/cs/findbugs/ba/XFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,7 @@ private XMethod resolveXMethod(MethodDescriptor originalDescriptor) {
desc = DescriptorFactory.instance().getMethodDescriptor(superClass.getClassName(), desc.getName(),
desc.getSignature(), desc.isStatic());
}
} catch (CheckedAnalysisException e) {
assert true;
} catch (RuntimeException e) {
} catch (CheckedAnalysisException | RuntimeException e) {
assert true;
}
UnresolvedXMethod xmethod = new UnresolvedXMethod(originalDescriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ private TypeQualifierValue(ClassDescriptor typeQualifier, @CheckForNull Object v
new Class[] { qualifierClass }, handler));
}

} catch (ClassNotFoundException e) {
assert true; // ignore
} catch (CheckedAnalysisException e) {
} catch (ClassNotFoundException | CheckedAnalysisException e) {
assert true; // ignore
} catch (Exception e) {
AnalysisContext.logError("Unable to construct type qualifier checker " + checkerName, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,7 @@ public static UsagesRequiringNonNullValues getAnalysis(ClassContext classContext
}
}

} catch (CFGBuilderException e) {
AnalysisContext.logError("Error generating derefs for " + thisMethod, e);
} catch (DataflowAnalysisException e) {
} catch (CFGBuilderException | DataflowAnalysisException e) {
AnalysisContext.logError("Error generating derefs for " + thisMethod, e);
}
return derefs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,7 @@ private void handleInvoke(InvokeInstruction obj) {
}
}
}
} catch (DataflowAnalysisException e) {
result = IsNullValue.nonReportingNotNullValue();
} catch (ClassNotFoundException e) {
} catch (DataflowAnalysisException | ClassNotFoundException e) {
result = IsNullValue.nonReportingNotNullValue();
}
modelInstruction(obj, getNumWordsConsumed(obj), getNumWordsProduced(obj), result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ private E createUsingStaticCreateMethod() throws CheckedAnalysisException {

try {
return databaseClass.cast(createMethod.invoke(null, new Object[0]));
} catch (InvocationTargetException e) {
throw new CheckedAnalysisException("Could not create " + databaseClass.getName(), e);
} catch (IllegalAccessException e) {
} catch (InvocationTargetException | IllegalAccessException e) {
throw new CheckedAnalysisException("Could not create " + databaseClass.getName(), e);
}
}
Expand All @@ -111,11 +109,7 @@ private E createUsingConstructor() throws CheckedAnalysisException {

try {
return constructor.newInstance(new Object[0]);
} catch (InstantiationException e) {
throw new CheckedAnalysisException("Could not create " + databaseClass.getName(), e);
} catch (IllegalAccessException e) {
throw new CheckedAnalysisException("Could not create " + databaseClass.getName(), e);
} catch (InvocationTargetException e) {
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new CheckedAnalysisException("Could not create " + databaseClass.getName(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,9 +734,7 @@ private void parseClassName(ICodeBaseEntry entry) {
if (!trueResourceName.equals(entry.getResourceName())) {
entry.overrideResourceName(trueResourceName);
}
} catch (IOException e) {
errorLogger.logError("Invalid class resource " + entry.getResourceName() + " in " + entry, e);
} catch (InvalidClassFileFormatException e) {
} catch (IOException | InvalidClassFileFormatException e) {
errorLogger.logError("Invalid class resource " + entry.getResourceName() + " in " + entry, e);
} finally {
IO.close(in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,7 @@ public int parse(String argv[], int minArgs, int maxArgs, String usage) {
return count;
} catch (HelpRequestedException e) {
// fall through
} catch (RuntimeException e) {
e.printStackTrace();
} catch (IOException e) {
} catch (RuntimeException | IOException e) {
e.printStackTrace();
}
System.out.println(usage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,9 @@ private void analyzeMethod(ClassContext classContext, Method method) {

}

} catch (CFGBuilderException e) {
} catch (CFGBuilderException | DataflowAnalysisException e) {
XMethod xmethod = XFactory.createXMethod(classContext.getJavaClass(), method);

AnalysisContext.currentAnalysisContext().getLookupFailureCallback()
.logError("Error analyzing " + xmethod + " for unconditional deref training", e);
} catch (DataflowAnalysisException e) {
XMethod xmethod = XFactory.createXMethod(classContext.getJavaClass(), method);
AnalysisContext.currentAnalysisContext().getLookupFailureCallback()
.logError("Error analyzing " + xmethod + " for unconditional deref training", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,7 @@ public void visitClassContext(ClassContext classContext) {
try {

analyzeMethod(classContext, method);
} catch (CFGBuilderException e) {
bugReporter.logError(
"Error checking for infinite recursive loop in "
+ SignatureConverter.convertMethodSignature(classContext.getJavaClass(), method), e);
} catch (DataflowAnalysisException e) {
} catch (CFGBuilderException | DataflowAnalysisException e) {
bugReporter.logError(
"Error checking for infinite recursive loop in "
+ SignatureConverter.convertMethodSignature(classContext.getJavaClass(), method), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ public void visitClassContext(ClassContext classContext) {
analyzeMethod(classContext, method);
} catch (MethodUnprofitableException e) {
assert true; // move along; nothing to see
} catch (CFGBuilderException e) {
String msg = "Detector " + this.getClass().getName() + " caught exception while analyzing "
+ javaClass.getClassName() + "." + method.getName() + " : " + method.getSignature();
bugReporter.logError(msg, e);
} catch (DataflowAnalysisException e) {
} catch (CFGBuilderException | DataflowAnalysisException e) {
String msg = "Detector " + this.getClass().getName() + " caught exception while analyzing "
+ javaClass.getClassName() + "." + method.getName() + " : " + method.getSignature();
bugReporter.logError(msg, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ public void visitClassContext(ClassContext classContext) {
try {
System.out.println("Analyzing " + SignatureConverter.convertMethodSignature(classContext.getJavaClass(), method));
analyzeMethod(classContext, method);
} catch (CFGBuilderException e) {
bugReporter.logError("Error", e);
} catch (DataflowAnalysisException e) {
} catch (CFGBuilderException | DataflowAnalysisException e) {
bugReporter.logError("Error", e);
} catch (ClassNotFoundException e) {
bugReporter.reportMissingClass(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ public void visitClassContext(ClassContext classContext) {

try {
analyzeMethod(classContext, method);
} catch (DataflowAnalysisException e) {
bugReporter.logError("Error analyzing " + method.toString(), e);
} catch (CFGBuilderException e) {
} catch (DataflowAnalysisException | CFGBuilderException e) {
bugReporter.logError("Error analyzing " + method.toString(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ public void visitClassContext(ClassContext classContext) {
// report
bugReporter.logError("skipping unprofitable method in " + getClass().getName());
}
} catch (CFGBuilderException e) {
bugReporter.logError("Detector " + this.getClass().getName() + " caught exception", e);
} catch (DataflowAnalysisException e) {
} catch (CFGBuilderException | DataflowAnalysisException e) {
bugReporter.logError("Detector " + this.getClass().getName() + " caught exception", e);
}
}
Expand Down
Loading

0 comments on commit 1503920

Please sign in to comment.