Skip to content

Commit

Permalink
Merge branch '6.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Jun 17, 2024
2 parents 1582f5f + 9a56a88 commit 6d5c312
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -29,7 +29,7 @@
public abstract class ApplicationContextEvent extends ApplicationEvent {

/**
* Create a new ContextStartedEvent.
* Create a new {@code ApplicationContextEvent}.
* @param source the {@code ApplicationContext} that the event is raised for
* (must not be {@code null})
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -29,7 +29,7 @@
public class ContextClosedEvent extends ApplicationContextEvent {

/**
* Creates a new ContextClosedEvent.
* Create a new {@code ContextClosedEvent}.
* @param source the {@code ApplicationContext} that has been closed
* (must not be {@code null})
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -29,7 +29,7 @@
public class ContextRefreshedEvent extends ApplicationContextEvent {

/**
* Create a new ContextRefreshedEvent.
* Create a new {@code ContextRefreshedEvent}.
* @param source the {@code ApplicationContext} that has been initialized
* or refreshed (must not be {@code null})
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -30,7 +30,7 @@
public class ContextStartedEvent extends ApplicationContextEvent {

/**
* Create a new ContextStartedEvent.
* Create a new {@code ContextStartedEvent}.
* @param source the {@code ApplicationContext} that has been started
* (must not be {@code null})
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -30,7 +30,7 @@
public class ContextStoppedEvent extends ApplicationContextEvent {

/**
* Create a new ContextStoppedEvent.
* Create a new {@code ContextStoppedEvent}.
* @param source the {@code ApplicationContext} that has been stopped
* (must not be {@code null})
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ public Class<?>[] determineValidationGroups(Object target, Method method) {

@Override
public final MethodValidationResult validateArguments(
Object target, Method method, @Nullable MethodParameter[] parameters, Object[] arguments,
Class<?>[] groups) {
Object target, Method method, @Nullable MethodParameter[] parameters,
Object[] arguments, Class<?>[] groups) {

Set<ConstraintViolation<Object>> violations =
invokeValidatorForArguments(target, method, arguments, groups);
Expand All @@ -256,23 +256,21 @@ public final Set<ConstraintViolation<Object>> invokeValidatorForArguments(
Object target, Method method, Object[] arguments, Class<?>[] groups) {

ExecutableValidator execVal = this.validator.get().forExecutables();
Set<ConstraintViolation<Object>> violations;
try {
violations = execVal.validateParameters(target, method, arguments, groups);
return execVal.validateParameters(target, method, arguments, groups);
}
catch (IllegalArgumentException ex) {
// Probably a generic type mismatch between interface and impl as reported in SPR-12237 / HV-1011
// Let's try to find the bridged method on the implementation class...
Method bridgedMethod = BridgeMethodResolver.getMostSpecificMethod(method, target.getClass());
violations = execVal.validateParameters(target, bridgedMethod, arguments, groups);
return execVal.validateParameters(target, bridgedMethod, arguments, groups);
}
return violations;
}

@Override
public final MethodValidationResult validateReturnValue(
Object target, Method method, @Nullable MethodParameter returnType, @Nullable Object returnValue,
Class<?>[] groups) {
Object target, Method method, @Nullable MethodParameter returnType,
@Nullable Object returnValue, Class<?>[] groups) {

Set<ConstraintViolation<Object>> violations =
invokeValidatorForReturnValue(target, method, returnValue, groups);
Expand Down Expand Up @@ -305,9 +303,9 @@ private MethodValidationResult adaptViolations(
Map<Path.Node, ParamErrorsBuilder> nestedViolations = new LinkedHashMap<>();

for (ConstraintViolation<Object> violation : violations) {
Iterator<Path.Node> itr = violation.getPropertyPath().iterator();
while (itr.hasNext()) {
Path.Node node = itr.next();
Iterator<Path.Node> nodes = violation.getPropertyPath().iterator();
while (nodes.hasNext()) {
Path.Node node = nodes.next();

MethodParameter parameter;
if (node.getKind().equals(ElementKind.PARAMETER)) {
Expand All @@ -328,8 +326,8 @@ else if (node.getKind().equals(ElementKind.RETURN_VALUE)) {
// https://github.com/jakartaee/validation/issues/194

Path.Node parameterNode = node;
if (itr.hasNext()) {
node = itr.next();
if (nodes.hasNext()) {
node = nodes.next();
}

Object value;
Expand Down Expand Up @@ -425,7 +423,6 @@ public interface ObjectNameResolver {
* @return the name to use
*/
String resolveName(MethodParameter parameter, @Nullable Object value);

}


Expand Down Expand Up @@ -456,6 +453,7 @@ private final class ParamValidationResultBuilder {
public ParamValidationResultBuilder(
Object target, MethodParameter parameter, @Nullable Object value, @Nullable Object container,
@Nullable Integer containerIndex, @Nullable Object containerKey) {

this.target = target;
this.parameter = parameter;
this.value = value;
Expand All @@ -473,7 +471,6 @@ public ParameterValidationResult build() {
this.parameter, this.value, this.resolvableErrors, this.container,
this.containerIndex, this.containerKey);
}

}


Expand Down Expand Up @@ -527,8 +524,7 @@ public ParameterErrors build() {


/**
* Default algorithm to select an object name, as described in
* {@link #setObjectNameResolver(ObjectNameResolver)}.
* Default algorithm to select an object name, as described in {@link #setObjectNameResolver}.
*/
private static class DefaultObjectNameResolver implements ObjectNameResolver {

Expand Down
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 6d5c312

Please sign in to comment.