Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: [Hib Validator] NPE w/ overloaded methods on validated class
PostPosted: Wed Jun 27, 2012 4:01 pm 
Newbie

Joined: Wed Jun 27, 2012 3:37 pm
Posts: 3
I've been debugging the following NPE found using Hibernate Validator 4.3.0.Final. It is failing to get method metadata from the bean metadata for the method being invoked:

java.lang.NullPointerException
at org.hibernate.validator.internal.engine.ValidatorImpl.validateParametersInContext(ValidatorImpl.java:846)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateAllParameters(ValidatorImpl.java:218)
at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:88)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

My class implements an Interface annotated with @Validated, such that all methods can potentially be validated without adding this annotation in many locations. It has two methods with the same name, taking a different number of properties. These two methods don't actually have any constraints on them, but I don't think this is relevant.
The return type of my method is also a class that happens to have validation annotations within it on its properties.

This NPE occurs when one of the overloaded methods is invoked.

Upon debugging I notice the following:
When adding a BeanConfiguration for the class, we try to create a set of builder delegates based off of the constrained elements (org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataImpl.BeanMetaDataBuilder#addMetaDataToBuilder).

In doing so, we try to find an existing builder appropriate for the element. The first method has a builder delegate generated. The ConstrainedElement representing the second method finds this builder delegate and checks it. This delegate's method builder does not accept the element as it recognises the number of arguments are different for the method. This is great. However the property builder accepts this method!

public ValidatedObject getMyValidObject(arg1);
public ValidatedObject getMyValidObject(arg1, arg2);


The property builder sees the second method as a getter, finds it to be an accessor for property 'myValidObject' which matches the property it has. The propertyType is ValidatedObject. (org.hibernate.validator.internal.metadata.aggregated.PropertyMetaData.Builder#accepts)


Is this a bug? Or am I missing something? I'm currently going to try renaming the methods to not look like getters, or not use overloading in order to avoid this.
Any help is much appreciated!

Thanks,

Louis


Top
 Profile  
 
 Post subject: Re: [Hib Validator] NPE w/ overloaded methods on validated class
PostPosted: Thu Jun 28, 2012 4:40 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi Louis,

to be honest I cannot quite follow, especially not where and how @Validated comes into play? Is this a custom annotation of yours? Could you post the annotated entities? I seems you are using method validation in this case. How is the method validation triggered?

--Hardy


Top
 Profile  
 
 Post subject: Re: [Hib Validator] NPE w/ overloaded methods on validated class
PostPosted: Thu Jun 28, 2012 5:25 am 
Newbie

Joined: Wed Jun 27, 2012 3:37 pm
Posts: 3
Hi Hardy,

Thanks for trying to follow. Apologies for referencing the org.springframework.validation.annotation.Validated annotation, this is simply a variant of JSR-303's javax.validation.Valid annotation. It helps Spring know to use Hibernate's MethodValidationInterceptor which I believe is supplementary to the JSR-303 spec, this is how it knows to trigger. I don't believe this is related to the issue, you simply need a bean that has method validation across its methods and you will reproduce this issue. Have two methods with the same name that are prefixed with get/is/has (org.hibernate.validator.internal.util.ReflectionHelper#isGetterMethod), you will reproduce this issue.

I would not expect to get a NullPointerException trying to call a method for which hibernate is applying the MethodValidationInterceptor, yet has built no method metadata for such that it gets an NPE.

I have confirmed that changing:
ValidatedObject getMyValidObject(arg1);
ValidatedObject getMyValidObject(arg1, arg2);

to:
ValidatedObject retrieveMyValidObject(arg1);
ValidatedObject retrieveMyValidObject(arg1, arg2);

or:
ValidatedObject getMyValidObject(arg1);
ValidatedObject getMyValidObjectDifferentMethodName(arg1, arg2);

resolves/avoids the issue.

This is what I've done for now, but this behaviour seems like a bug and I was hoping to get it raised in JIRA so we can one day take it in an update.
org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataImpl.BuilderDelegate#add is the key method to look at to understand how my constrained element of type method incorrectly gets added to an existing propertyBuilder (org.hibernate.validator.internal.metadata.aggregated.PropertyMetaData), resulting in an appropriate BuilderDelegate not being created (NPE!).

Thanks again,

Louis


Last edited by louis.burton on Thu Jun 28, 2012 5:31 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: [Hib Validator] NPE w/ overloaded methods on validated class
PostPosted: Thu Jun 28, 2012 5:30 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

thanks for your help and debugging :-) Just create an issue in https://hibernate.onjira.com/browse/HV and if possible attach a test case (some example code in the issue or even better an actual runnable testcase). We will look at it asap.

Thanks,
Hardy


Top
 Profile  
 
 Post subject: Re: [Hib Validator] NPE w/ overloaded methods on validated class
PostPosted: Thu Jun 28, 2012 6:43 am 
Newbie

Joined: Wed Jun 27, 2012 3:37 pm
Posts: 3
Thanks for the quick responses.

I have raised https://hibernate.onjira.com/browse/HV-601

If I get some spare capacity I'll try and get a runnable test case together and attach it, but this issue set me back a little already! Thanks,

Louis


Top
 Profile  
 
 Post subject: Re: [Hib Validator] NPE w/ overloaded methods on validated class
PostPosted: Thu Jun 28, 2012 6:46 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Thanks :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.