Discussion:
Exception in thread “main” java.lang.NoSuchMethodException: Unknown property X_XXX on class
Priyam Srivastava
2018-07-19 02:51:00 UTC
Permalink
Hi,

I know commons-beanutils expect that Java classes should ideally conform
to Java Beans naming pattern. But when they don't adhere to the naming
convention, I see library results vary for different ways of field
declaration.

So, when I declare something like F_NAME or MiddleName, while setting
property value using BeanUtils.setProperty(), it sets the value as null
and when reading the same property using
PropertyUtils.getSimpleProperty() it throws Exception:

|Exceptionin thread "main"java.lang.NoSuchMethodException:Unknownproperty
'F_NAME'on class...|

On the other hand when I declare field as LNAME, both
BeanUtils.setProperty() and PropertyUtils.getSimpleProperty() works fine.

Sample Code:

|publicfinalclassPropertyUtilsTest{privateStringF_NAME;privateStringMiddleName;privateStringLNAME;/**
* @return the F_NAME */publicStringgetF_NAME(){returnF_NAME;}/** *
@param F_NAME the f_NAME to set
*/publicvoidsetF_NAME(Stringf_NAME){F_NAME =f_NAME;}/** * @return the
middleName */publicStringgetMiddleName(){returnMiddleName;}/** * @param
middleName the middleName to set
*/publicvoidsetMiddleName(StringmiddleName){MiddleName=middleName;}/** *
@return the LNAME */publicStringgetLNAME(){returnLNAME;}/** * @param
lNAME the LNAME to set */publicvoidsetLNAME(StringlNAME){LNAME
=lNAME;}publicstaticvoidmain(String[]args)throwsException{PropertyUtilsTestl_obj
=newPropertyUtilsTest();BeanUtils.setProperty(l_obj,"LNAME","Future");BeanUtils.setProperty(l_obj,"F_NAME","Wambling");BeanUtils.setProperty(l_obj,"MiddleName","Uknown");System.out.println("F_NAME::
"+l_obj.getF_NAME());System.out.println("MiddleName::
"+l_obj.getMiddleName());System.out.println("LNAME::
"+l_obj.getLNAME());try{System.out.println("MiddleName::
"+PropertyUtils.getSimpleProperty(l_obj,"MiddleName"));}catch(Exceptione){e.printStackTrace();}System.out.println("\n");try{System.out.println("F_NAME::
"+PropertyUtils.getSimpleProperty(l_obj,"F_NAME"));}catch(Exceptione){e.printStackTrace();}System.out.println("LNAME::
"+PropertyUtils.getSimpleProperty(l_obj,"LNAME"));System.out.println("\n");}}|

Output:

|F_NAME::nullMiddleName::nullLNAME::Futurejava.lang.NoSuchMethodException:Unknownproperty
'MiddleName'on class'class com.wambling.stubs.PropertyUtilsTest'at
org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1269)at
org.apache.commons.beanutils.PropertyUtils.getSimpleProperty(PropertyUtils.java:649)at
com.wambling.stubs.PropertyUtilsTest.main(PropertyUtilsTest.java:72)java.lang.NoSuchMethodException:Unknownproperty
'F_NAME'on class'class com.wambling.stubs.PropertyUtilsTest'at
org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1269)at
org.apache.commons.beanutils.PropertyUtils.getSimpleProperty(PropertyUtils.java:649)at
com.wambling.stubs.PropertyUtilsTest.main(PropertyUtilsTest.java:78)LNAME::Future|

So, my question is why such different behavior for these fields as all
of them does not follow Java Beans Naming convention?

commons-beanutils Version used: 1.9.3
--
Regards,
Priyam
Loading...