Skip to content

Commit

Permalink
Consistently ignore bridge method on generated subclass for visibilit…
Browse files Browse the repository at this point in the history
…y purposes

Closes gh-33030
  • Loading branch information
jhoeller committed Jun 17, 2024
1 parent c38e989 commit 2c3c383
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -43,6 +44,7 @@
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

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

Expand Down Expand Up @@ -183,6 +185,14 @@ void testValueInjectionWithProviderMethodArguments() {
context.close();
}

@Test
void testValueInjectionWithAccidentalAutowiredAnnotations() {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ValueConfigWithAccidentalAutowiredAnnotations.class);
doTestValueInjection(context);
context.close();
}

private void doTestValueInjection(BeanFactory context) {
System.clearProperty("myProp");

Expand Down Expand Up @@ -494,6 +504,32 @@ public TestBean testBean2(@Value("#{systemProperties[myProp]}") Provider<String>
}


@Configuration
static class ValueConfigWithAccidentalAutowiredAnnotations implements InitializingBean {

boolean invoked;

@Override
public void afterPropertiesSet() {
Assert.state(!invoked, "Factory method must not get invoked on startup");
}

@Bean @Scope("prototype")
@Autowired
public TestBean testBean(@Value("#{systemProperties[myProp]}") Provider<String> name) {
invoked = true;
return new TestBean(name.get());
}

@Bean @Scope("prototype")
@Autowired
public TestBean testBean2(@Value("#{systemProperties[myProp]}") Provider<String> name2) {
invoked = true;
return new TestBean(name2.get());
}
}


@Configuration
static class PropertiesConfig {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,13 @@ private static Method searchForMatch(Class<?> type, Method bridgeMethod) {
*/
public static boolean isVisibilityBridgeMethodPair(Method bridgeMethod, Method bridgedMethod) {
if (bridgeMethod == bridgedMethod) {
// Same method: for common purposes, return true to proceed as if it was a visibility bridge.
return true;
}
if (ClassUtils.getUserClass(bridgeMethod.getDeclaringClass()) != bridgeMethod.getDeclaringClass()) {
// Method on generated subclass: return false to consistently ignore it for visibility purposes.
return false;
}
return (bridgeMethod.getReturnType().equals(bridgedMethod.getReturnType()) &&
bridgeMethod.getParameterCount() == bridgedMethod.getParameterCount() &&
Arrays.equals(bridgeMethod.getParameterTypes(), bridgedMethod.getParameterTypes()));
Expand Down

0 comments on commit 2c3c383

Please sign in to comment.