Skip to content

Commit

Permalink
GH-3418: Fix Poller Undeclared Checked Exceptions
Browse files Browse the repository at this point in the history
Resolves #3418

`Class.newInstance()` can propagate checked exceptions that are not declared.

**cherry-pick/back-port to 5.3.x, 5.2.x, 4.3.x**
  • Loading branch information
garyrussell authored and artembilan committed Nov 2, 2020
1 parent 3d59182 commit 27b464a
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ErrorHandler;
import org.springframework.util.ReflectionUtils;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -402,7 +403,7 @@ private Message<?> pollForMessage() {

private Message<?> doPoll() {
IntegrationResourceHolder holder = bindResourceHolderIfNecessary(getResourceKey(), getResourceToBind());
Message<?> message;
Message<?> message = null;
try {
message = receiveMessage();
}
Expand All @@ -414,7 +415,7 @@ private Message<?> doPoll() {
return null;
}
else {
throw (RuntimeException) e;
ReflectionUtils.rethrowRuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2020 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.
* You may obtain a copy of the License at
*
* https://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.springframework.integration.endpoint;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.aopalliance.intercept.MethodInterceptor;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.Pollers;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

/**
* @author Gary Russell
* @since 4.3.23
*
*/
@SpringJUnitConfig
@DirtiesContext
public class SourcePollingChannelAdapterTests {

@Autowired
Config config;

@Test
void undeclaredCheckedException() throws InterruptedException {
assertThat(this.config.latchl.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(this.config.caught).isInstanceOf(UndeclaredThrowableException.class);
}

@Configuration
@EnableIntegration
public static class Config {

volatile Exception caught;

volatile CountDownLatch latchl = new CountDownLatch(1);

@Bean
public IntegrationFlow flow() {
return IntegrationFlows.from(new BadMessageSource(), e -> e.poller(Pollers.fixedDelay(500)
.advice(exceptionCaptor())))
.nullChannel();
}

private MethodInterceptor exceptionCaptor() {
return invocation -> {
try {
return invocation.proceed();
}
catch (Exception e) {
Config.this.caught = e;
throw e;
}
finally {
Config.this.latchl.countDown();
}
};
}

}

public static class BadMessageSource implements MessageSource<String> {

@Override
@Nullable
public Message<String> receive() {
try {
Foo.class.newInstance();
}
catch (InstantiationException | IllegalAccessException e) {
}
return null;
}

}

public static class Foo {

public Foo() throws IOException {
throw new IOException();
}

}

}

0 comments on commit 27b464a

Please sign in to comment.