Collapse catch clauses

Use multi-catch for exceptions whenever possible.

See gh-9781
This commit is contained in:
Emanuel Campolo 2017-07-18 11:52:47 -03:00 committed by Phillip Webb
parent 4a189bdee7
commit 798fe5ed53
17 changed files with 19 additions and 76 deletions

View File

@ -107,16 +107,13 @@ public abstract class AbstractJmxCacheStatisticsProvider<C extends Cache>
Object attribute = getMBeanServer().getAttribute(objectName, attributeName); Object attribute = getMBeanServer().getAttribute(objectName, attributeName);
return type.cast(attribute); return type.cast(attribute);
} }
catch (MBeanException ex) { catch (MBeanException | ReflectionException ex) {
throw new IllegalStateException(ex); throw new IllegalStateException(ex);
} }
catch (AttributeNotFoundException ex) { catch (AttributeNotFoundException ex) {
throw new IllegalStateException("Unexpected: MBean with name '" + objectName throw new IllegalStateException("Unexpected: MBean with name '" + objectName
+ "' " + "does not expose attribute with name " + attributeName, ex); + "' " + "does not expose attribute with name " + attributeName, ex);
} }
catch (ReflectionException ex) {
throw new IllegalStateException(ex);
}
catch (InstanceNotFoundException ex) { catch (InstanceNotFoundException ex) {
logger.warn("Cache statistics are no longer available", ex); logger.warn("Cache statistics are no longer available", ex);
return null; return null;

View File

@ -255,10 +255,7 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
ClassUtils.forName(type, classLoader), considerHierarchy); ClassUtils.forName(type, classLoader), considerHierarchy);
return result; return result;
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException | NoClassDefFoundError ex) {
return Collections.emptySet();
}
catch (NoClassDefFoundError ex) {
return Collections.emptySet(); return Collections.emptySet();
} }
} }

View File

@ -240,10 +240,7 @@ public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>>
try { try {
list.add(Class.forName(className)); list.add(Class.forName(className));
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException | NoClassDefFoundError ex) {
// Ignore
}
catch (NoClassDefFoundError ex) {
// Ignore // Ignore
} }
} }

View File

@ -27,7 +27,6 @@ import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages; import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.mongo.city.City; import org.springframework.boot.autoconfigure.data.mongo.city.City;
@ -135,9 +134,6 @@ public class MongoDataAutoConfigurationTests {
fail("Create FieldNamingStrategy interface should fail"); fail("Create FieldNamingStrategy interface should fail");
} }
// We seem to have an inconsistent exception, accept either // We seem to have an inconsistent exception, accept either
catch (UnsatisfiedDependencyException ex) {
// Expected
}
catch (BeanCreationException ex) { catch (BeanCreationException ex) {
// Expected // Expected
} }

View File

@ -131,10 +131,7 @@ public class AetherGrapeEngine implements GrapeEngine {
classLoader.addURL(file.toURI().toURL()); classLoader.addURL(file.toURI().toURL());
} }
} }
catch (ArtifactResolutionException ex) { catch (ArtifactResolutionException | MalformedURLException ex) {
throw new DependencyResolutionFailedException(ex);
}
catch (MalformedURLException ex) {
throw new DependencyResolutionFailedException(ex); throw new DependencyResolutionFailedException(ex);
} }
return null; return null;

View File

@ -80,10 +80,7 @@ public class ClassPathChangeUploader
try { try {
this.uri = new URL(url).toURI(); this.uri = new URL(url).toURI();
} }
catch (URISyntaxException ex) { catch (URISyntaxException | MalformedURLException ex) {
throw new IllegalArgumentException("Malformed URL '" + url + "'");
}
catch (MalformedURLException ex) {
throw new IllegalArgumentException("Malformed URL '" + url + "'"); throw new IllegalArgumentException("Malformed URL '" + url + "'");
} }
this.requestFactory = requestFactory; this.requestFactory = requestFactory;

View File

@ -84,10 +84,7 @@ public class HttpTunnelConnection implements TunnelConnection {
try { try {
this.uri = new URL(url).toURI(); this.uri = new URL(url).toURI();
} }
catch (URISyntaxException ex) { catch (URISyntaxException | MalformedURLException ex) {
throw new IllegalArgumentException("Malformed URL '" + url + "'");
}
catch (MalformedURLException ex) {
throw new IllegalArgumentException("Malformed URL '" + url + "'"); throw new IllegalArgumentException("Malformed URL '" + url + "'");
} }
this.requestFactory = requestFactory; this.requestFactory = requestFactory;

View File

@ -371,9 +371,6 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
addDependencies(urls); addDependencies(urls);
return urls.toArray(new URL[urls.size()]); return urls.toArray(new URL[urls.size()]);
} }
catch (MalformedURLException ex) {
throw new MojoExecutionException("Unable to build classpath", ex);
}
catch (IOException ex) { catch (IOException ex) {
throw new MojoExecutionException("Unable to build classpath", ex); throw new MojoExecutionException("Unable to build classpath", ex);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -73,10 +73,7 @@ class SpringApplicationAdminClient {
throw new MojoExecutionException("Failed to retrieve Ready attribute", throw new MojoExecutionException("Failed to retrieve Ready attribute",
ex.getCause()); ex.getCause());
} }
catch (MBeanException ex) { catch (MBeanException | IOException ex) {
throw new MojoExecutionException(ex.getMessage(), ex);
}
catch (IOException ex) {
throw new MojoExecutionException(ex.getMessage(), ex); throw new MojoExecutionException(ex.getMessage(), ex);
} }
} }

View File

@ -94,11 +94,7 @@ public class StartMojo extends AbstractRunMojo {
try { try {
waitForSpringApplication(); waitForSpringApplication();
} }
catch (MojoExecutionException ex) { catch (MojoExecutionException | MojoFailureException ex) {
runProcess.kill();
throw ex;
}
catch (MojoFailureException ex) {
runProcess.kill(); runProcess.kill();
throw ex; throw ex;
} }

View File

@ -191,10 +191,7 @@ class BeanDefinitionLoader {
try { try {
return load(ClassUtils.forName(resolvedSource, null)); return load(ClassUtils.forName(resolvedSource, null));
} }
catch (IllegalArgumentException ex) { catch (IllegalArgumentException | ClassNotFoundException ex) {
// swallow exception and continue
}
catch (ClassNotFoundException ex) {
// swallow exception and continue // swallow exception and continue
} }
// Attempt as resources // Attempt as resources

View File

@ -91,10 +91,7 @@ public class AtomikosDependsOnBeanFactoryPostProcessor
try { try {
return beanFactory.getBeanNamesForType(Class.forName(type), true, false); return beanFactory.getBeanNamesForType(Class.forName(type), true, false);
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException | NoClassDefFoundError ex) {
// Ignore
}
catch (NoClassDefFoundError ex) {
// Ignore // Ignore
} }
return NO_BEANS; return NO_BEANS;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -67,10 +67,7 @@ public class BitronixDependentBeanFactoryPostProcessor
try { try {
return beanFactory.getBeanNamesForType(Class.forName(type), true, false); return beanFactory.getBeanNamesForType(Class.forName(type), true, false);
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException | NoClassDefFoundError ex) {
// Ignore
}
catch (NoClassDefFoundError ex) {
// Ignore // Ignore
} }
return NO_BEANS; return NO_BEANS;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -70,10 +70,7 @@ public class NarayanaBeanFactoryPostProcessor
try { try {
return beanFactory.getBeanNamesForType(Class.forName(type), true, false); return beanFactory.getBeanNamesForType(Class.forName(type), true, false);
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException | NoClassDefFoundError ex) {
// Ignore
}
catch (NoClassDefFoundError ex) {
// Ignore // Ignore
} }
return NO_BEANS; return NO_BEANS;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -78,11 +78,7 @@ public class SpringPackageScanClassResolver extends DefaultPackageScanClassResol
MetadataReader reader = readerFactory.getMetadataReader(resource); MetadataReader reader = readerFactory.getMetadataReader(resource);
return ClassUtils.forName(reader.getClassMetadata().getClassName(), loader); return ClassUtils.forName(reader.getClassMetadata().getClassName(), loader);
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException | LinkageError ex) {
handleFailure(resource, ex);
return null;
}
catch (LinkageError ex) {
handleFailure(resource, ex); handleFailure(resource, ex);
return null; return null;
} }

View File

@ -58,10 +58,7 @@ class TomcatErrorPage {
.instantiateClass(ClassUtils.forName(ERROR_PAGE_CLASS, null)); .instantiateClass(ClassUtils.forName(ERROR_PAGE_CLASS, null));
} }
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException | LinkageError ex) {
// Swallow and continue
}
catch (LinkageError ex) {
// Swallow and continue // Swallow and continue
} }
return null; return null;

View File

@ -273,13 +273,7 @@ public class ServletWebServerApplicationContext extends GenericWebApplicationCon
+ elapsedTime + " ms"); + elapsedTime + " ms");
} }
} }
catch (RuntimeException ex) { catch (RuntimeException | Error ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
}
catch (Error ex) {
logger.error("Context initialization failed", ex); logger.error("Context initialization failed", ex);
servletContext.setAttribute( servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex); WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);