From 0dc5b63469eb40814b13d3d33c70fc1600dc2e60 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Mon, 5 Jun 2023 13:29:47 +0200 Subject: [PATCH] Replace identical catch sections with multi-catch blocks --- .../batch/item/data/RepositoryItemWriter.java | 7 ++---- .../batch/item/xml/StaxEventItemWriter.java | 7 ++---- .../chunk/ChunkProcessorChunkHandler.java | 17 +++---------- .../sample/SkipSampleFunctionalTests.java | 24 ++++--------------- .../batch/test/StepRunner.java | 14 +++-------- 5 files changed, 14 insertions(+), 55 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/RepositoryItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/RepositoryItemWriter.java index 20325f6fc..1cb30e874 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/RepositoryItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/RepositoryItemWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -140,10 +140,7 @@ public class RepositoryItemWriter implements ItemWriter, InitializingBean try { invoker.prepare(); } - catch (ClassNotFoundException e) { - throw new DynamicMethodInvocationException(e); - } - catch (NoSuchMethodException e) { + catch (ClassNotFoundException | NoSuchMethodException e) { throw new DynamicMethodInvocationException(e); } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java index 0519ec2b0..05d79a6cc 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java @@ -505,15 +505,12 @@ public class StaxEventItemWriter extends AbstractItemStreamItemWriter } } } - catch (XMLStreamException xse) { - throw new ItemStreamException("Unable to write to file resource: [" + resource + "]", xse); - } catch (UnsupportedEncodingException e) { throw new ItemStreamException( "Unable to write to file resource: [" + resource + "] with encoding=[" + encoding + "]", e); } - catch (IOException e) { - throw new ItemStreamException("Unable to write to file resource: [" + resource + "]", e); + catch (XMLStreamException | IOException xse) { + throw new ItemStreamException("Unable to write to file resource: [" + resource + "]", xse); } } diff --git a/spring-batch-integration/src/main/java/org/springframework/batch/integration/chunk/ChunkProcessorChunkHandler.java b/spring-batch-integration/src/main/java/org/springframework/batch/integration/chunk/ChunkProcessorChunkHandler.java index 4b68bcbbb..9ef65a46a 100644 --- a/spring-batch-integration/src/main/java/org/springframework/batch/integration/chunk/ChunkProcessorChunkHandler.java +++ b/spring-batch-integration/src/main/java/org/springframework/batch/integration/chunk/ChunkProcessorChunkHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -106,19 +106,8 @@ public class ChunkProcessorChunkHandler implements ChunkHandler, Initializ try { chunkProcessor.process(stepContribution, chunk); } - catch (SkipLimitExceededException e) { - failure = e; - } - catch (NonSkippableReadException e) { - failure = e; - } - catch (SkipListenerFailedException e) { - failure = e; - } - catch (RetryException e) { - failure = e; - } - catch (JobInterruptedException e) { + catch (SkipLimitExceededException | NonSkippableReadException | SkipListenerFailedException | RetryException + | JobInterruptedException e) { failure = e; } catch (Exception e) { diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java index 800f4928a..a04c759d1 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/SkipSampleFunctionalTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2022 the original author or authors. + * Copyright 2008-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -321,25 +321,9 @@ class SkipSampleFunctionalTests { try { return this.jobOperator.startNextInstance("skipJob"); } - catch (NoSuchJobException e) { - throw new RuntimeException(e); - } - catch (JobExecutionAlreadyRunningException e) { - throw new RuntimeException(e); - } - catch (JobParametersNotFoundException e) { - throw new RuntimeException(e); - } - catch (JobRestartException e) { - throw new RuntimeException(e); - } - catch (JobInstanceAlreadyCompleteException e) { - throw new RuntimeException(e); - } - catch (UnexpectedJobExecutionException e) { - throw new RuntimeException(e); - } - catch (JobParametersInvalidException e) { + catch (NoSuchJobException | JobExecutionAlreadyRunningException | JobParametersNotFoundException + | JobRestartException | JobInstanceAlreadyCompleteException | UnexpectedJobExecutionException + | JobParametersInvalidException e) { throw new RuntimeException(e); } } diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/StepRunner.java b/spring-batch-test/src/main/java/org/springframework/batch/test/StepRunner.java index 9694612cd..42a596add 100755 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/StepRunner.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/StepRunner.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -166,16 +166,8 @@ public class StepRunner { try { return this.launcher.run(job, jobParameters); } - catch (JobExecutionAlreadyRunningException e) { - throw new UnexpectedJobExecutionException("Step runner encountered exception.", e); - } - catch (JobRestartException e) { - throw new UnexpectedJobExecutionException("Step runner encountered exception.", e); - } - catch (JobInstanceAlreadyCompleteException e) { - throw new UnexpectedJobExecutionException("Step runner encountered exception.", e); - } - catch (JobParametersInvalidException e) { + catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException + | JobParametersInvalidException e) { throw new UnexpectedJobExecutionException("Step runner encountered exception.", e); } }