Skip to content

Commit

Permalink
MethodValidationInterceptor excludes FactoryBean metadata methods
Browse files Browse the repository at this point in the history
Issue: SPR-17374
  • Loading branch information
jhoeller committed Oct 14, 2018
1 parent 2255d22 commit 5f2d47a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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 @@ -27,6 +27,7 @@

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -122,8 +123,8 @@ private void doTestProxyValidation(MyValidInterface proxy) {
@Test
public void testLazyValidatorForMethodValidation() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
LazyMethodValidationConfig.class, CustomValidatorBean.class, MyValidBean.class);
ctx.getBean(MyValidInterface.class).myValidMethod("value", 5);
LazyMethodValidationConfig.class, CustomValidatorBean.class, MyValidBean.class, MyValidFactoryBean.class);
ctx.getBeansOfType(MyValidInterface.class).values().forEach(bean -> bean.myValidMethod("value", 5));
}


Expand All @@ -146,6 +147,35 @@ public String myGenericMethod(String value) {
}


@MyStereotype
public static class MyValidFactoryBean implements FactoryBean<String>, MyValidInterface<String> {

@Override
public String getObject() {
return null;
}

@Override
public Class<?> getObjectType() {
return String.class;
}

@Override
public Object myValidMethod(String arg1, int arg2) {
return (arg2 == 0 ? null : "value");
}

@Override
public void myValidAsyncMethod(String arg1, int arg2) {
}

@Override
public String myGenericMethod(String value) {
return value;
}
}


public interface MyValidInterface<T> {

@NotNull Object myValidMethod(@NotNull(groups = MyGroup.class) String arg1, @Max(10) int arg2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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 @@ -28,6 +28,8 @@
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.SmartFactoryBean;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ClassUtils;
Expand Down Expand Up @@ -86,6 +88,11 @@ public MethodValidationInterceptor(Validator validator) {
@Override
@SuppressWarnings("unchecked")
public Object invoke(MethodInvocation invocation) throws Throwable {
// Avoid Validator invocation on FactoryBean.getObjectType/isSingleton
if (isFactoryBeanMetadataMethod(invocation.getMethod())) {
return invocation.proceed();
}

Class<?>[] groups = determineValidationGroups(invocation);

// Standard Bean Validation 1.1 API
Expand Down Expand Up @@ -119,6 +126,12 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
return returnValue;
}

private boolean isFactoryBeanMetadataMethod(Method method) {
Class<?> clazz = method.getDeclaringClass();
return ((clazz == FactoryBean.class || clazz == SmartFactoryBean.class) &&
!method.getName().equals("getObject"));
}

/**
* Determine the validation groups to validate against for the given method invocation.
* <p>Default are the validation groups as specified in the {@link Validated} annotation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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 @@ -27,6 +27,7 @@

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -119,8 +120,8 @@ private void doTestProxyValidation(MyValidInterface proxy) {
@Test
public void testLazyValidatorForMethodValidation() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
LazyMethodValidationConfig.class, CustomValidatorBean.class, MyValidBean.class);
ctx.getBean(MyValidInterface.class).myValidMethod("value", 5);
LazyMethodValidationConfig.class, CustomValidatorBean.class, MyValidBean.class, MyValidFactoryBean.class);
ctx.getBeansOfType(MyValidInterface.class).values().forEach(bean -> bean.myValidMethod("value", 5));
}


Expand All @@ -143,6 +144,35 @@ public String myGenericMethod(String value) {
}


@MyStereotype
public static class MyValidFactoryBean implements FactoryBean<String>, MyValidInterface<String> {

@Override
public String getObject() {
return null;
}

@Override
public Class<?> getObjectType() {
return String.class;
}

@Override
public Object myValidMethod(String arg1, int arg2) {
return (arg2 == 0 ? null : "value");
}

@Override
public void myValidAsyncMethod(String arg1, int arg2) {
}

@Override
public String myGenericMethod(String value) {
return value;
}
}


public interface MyValidInterface<T> {

@NotNull Object myValidMethod(@NotNull(groups = MyGroup.class) String arg1, @Max(10) int arg2);
Expand Down

0 comments on commit 5f2d47a

Please sign in to comment.