From ccf613c29ce8ece7620fe5311c0a972d0dceadf6 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 11 Oct 2017 08:09:06 -0400 Subject: [PATCH] Make method accessible in GenericScope proxy target Otherwise app crashes on first access of scoped proxy. Fixes gh-261 --- .../cloud/context/scope/GenericScope.java | 11 +++++++---- .../scope/refresh/RefreshScopeConfigurationTests.java | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/scope/GenericScope.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/scope/GenericScope.java index 57c3b87f..372bbf3c 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/scope/GenericScope.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/scope/GenericScope.java @@ -13,6 +13,7 @@ package org.springframework.cloud.context.scope; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -453,9 +454,10 @@ public class GenericScope implements Scope, BeanFactoryPostProcessor, @Override public Object invoke(MethodInvocation invocation) throws Throwable { - if (AopUtils.isEqualsMethod(invocation.getMethod()) - || AopUtils.isToStringMethod(invocation.getMethod()) - || AopUtils.isHashCodeMethod(invocation.getMethod())) { + Method method = invocation.getMethod(); + if (AopUtils.isEqualsMethod(method) + || AopUtils.isToStringMethod(method) + || AopUtils.isHashCodeMethod(method)) { return invocation.proceed(); } Object proxy = getObject(); @@ -464,7 +466,8 @@ public class GenericScope implements Scope, BeanFactoryPostProcessor, try { if (proxy instanceof Advised) { Advised advised = (Advised) proxy; - return ReflectionUtils.invokeMethod(invocation.getMethod(), + ReflectionUtils.makeAccessible(method); + return ReflectionUtils.invokeMethod(method, advised.getTargetSource().getTarget(), invocation.getArguments()); } diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeConfigurationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeConfigurationTests.java index 09badd73..c03f7aef 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeConfigurationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeConfigurationTests.java @@ -160,7 +160,8 @@ public class RefreshScopeConfigurationTests { String message; @RequestMapping("/") - public String hello() { + // Deliberately use package scope + String hello() { return message; }