de.knightsoftnet.validators.shared.util.BeanPropertyReaderUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mt-bean-validators Show documentation
Show all versions of mt-bean-validators Show documentation
The MT Bean Validators is a collection of JSR-303/JSR-349/JSR-380 bean validators. It's extracted from
GWT Bean Validators and contains no dependencies to GWT.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package de.knightsoftnet.validators.shared.util;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.NestedNullException;
import org.apache.commons.beanutils.PropertyUtils;
import java.lang.reflect.InvocationTargetException;
/**
* Bean Property Util, read bean property.
*
* @author Manfred Tremmel
*
*/
public final class BeanPropertyReaderUtil {
/**
* private constructor for final utility class.
*/
private BeanPropertyReaderUtil() {
super();
}
/**
*
* Return the value of the specified property of the specified bean, no matter which property
* reference format is used, as a String.
*
*
* If there is a null value in path hierarchy, exception is cached and null returned.
*
*
* @param bean Bean whose property is to be extracted
* @param name Possibly indexed and/or nested name of the property to be extracted
* @return The property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have access to the property accessor
* method
* @exception InvocationTargetException if the property accessor method throws an exception
* @exception NoSuchMethodException if an accessor method for this property cannot be found
* @see BeanUtilsBean#getProperty
*/
public static String getNullSaveStringProperty(final Object bean, final String name)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
String property;
try {
property = BeanUtils.getProperty(bean, name);
} catch (final NestedNullException | NoSuchMethodException pexception) {
property = null;
}
return property;
}
/**
*
* Return the value of the specified property of the specified bean, no matter which property
* reference format is used, as a String.
*
*
* If there is a null value in path hierarchy, exception is cached and null returned.
*
*
* @param bean Bean whose property is to be extracted
* @param name Possibly indexed and/or nested name of the property to be extracted
* @return The property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have access to the property accessor
* method
* @exception InvocationTargetException if the property accessor method throws an exception
* @exception NoSuchMethodException if an accessor method for this property cannot be found
* @see BeanUtilsBean#getProperty
*/
public static Object getNullSaveProperty(final Object bean, final String name)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Object property;
try {
property = PropertyUtils.getProperty(bean, name);
} catch (final NestedNullException | NoSuchMethodException pexception) {
property = null;
}
return property;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy