Skip to content

Commit

Permalink
Use MethodInvocationInfo class loader in case of JDK platform loader …
Browse files Browse the repository at this point in the history
…as well

Closes spring-projectsgh-30210
  • Loading branch information
jhoeller committed Mar 29, 2023
1 parent 66cdf43 commit 491ae1e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,9 @@ private static <T> T initProxy(

else if (controllerType.isInterface()) {
ClassLoader classLoader = controllerType.getClassLoader();
if (classLoader == null) { // JDK interface type from bootstrap loader
if (classLoader == null || classLoader.getParent() == null) {
// JDK interface type from bootstrap loader or platform loader ->
// use higher-level loader which can see Spring infrastructure classes
classLoader = MethodInvocationInfo.class.getClassLoader();
}
Class<?>[] ifcs = new Class<?>[] {controllerType, MethodInvocationInfo.class};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.sql.Savepoint;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -384,14 +385,6 @@ public void fromMethodCallWithObjectReturnType() {
assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
}

@Test // SPR-16710
public void fromMethodCallWithCharSequenceReturnType() {
UriComponents uriComponents = fromMethodCall(
on(BookingControllerWithCharSequence.class).getBooking(21L)).buildAndExpand(42);

assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
}

@Test // SPR-16710
public void fromMethodCallWithStringReturnType() {
assertThatIllegalStateException().isThrownBy(() -> {
Expand All @@ -409,6 +402,22 @@ public void fromMethodNameWithStringReturnType() {
assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
}

@Test // gh-30210
public void fromMethodCallWithCharSequenceReturnType() {
UriComponents uriComponents = fromMethodCall(
on(BookingControllerWithCharSequence.class).getBooking(21L)).buildAndExpand(42);

assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
}

@Test // gh-30210
public void fromMethodCallWithJdbc30115ReturnType() {
UriComponents uriComponents = fromMethodCall(
on(BookingControllerWithJdbcSavepoint.class).getBooking(21L)).buildAndExpand(42);

assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
}

@Test
public void fromMappingNamePlain() {
initWebApplicationContext(WebConfig.class);
Expand Down Expand Up @@ -716,6 +725,17 @@ public Object getBooking(@PathVariable Long booking) {
}


@Controller
@RequestMapping("/hotels/{hotel}")
static class BookingControllerWithString {

@GetMapping("/bookings/{booking}")
public String getBooking(@PathVariable Long booking) {
return "url";
}
}


@Controller
@RequestMapping("/hotels/{hotel}")
static class BookingControllerWithCharSequence {
Expand All @@ -729,11 +749,11 @@ public CharSequence getBooking(@PathVariable Long booking) {

@Controller
@RequestMapping("/hotels/{hotel}")
static class BookingControllerWithString {
static class BookingControllerWithJdbcSavepoint {

@GetMapping("/bookings/{booking}")
public String getBooking(@PathVariable Long booking) {
return "url";
public Savepoint getBooking(@PathVariable Long booking) {
return null;
}
}

Expand Down

0 comments on commit 491ae1e

Please sign in to comment.