Search in sources :

Example 1 with ReflectionException

use of com.qiuyj.commons.bean.exception.ReflectionException in project qiuyj-code by qiuyuanjun.

the class AbstractNestedPropertyAccessor method parseProperty.

private static SinglePropertyHolder parseProperty(String property) {
    Objects.requireNonNull(property);
    int flagIdx = property.indexOf(PropertyAccessor.NESTED_PROPERTY_SEPARATOR);
    String currentProperty = property, nestedPropertyPath = null, indexedProperty = null;
    if (flagIdx > 0) {
        currentProperty = property.substring(0, flagIdx);
        nestedPropertyPath = property.substring(flagIdx + 1);
    } else if (flagIdx == 0) {
        throw new ReflectionException("Nested property separator '.' can not be located at the first location of this property expression: " + property);
    }
    flagIdx = currentProperty.indexOf(PropertyAccessor.INDEXED_PROPERTY_PREFIX);
    if (flagIdx > 0) {
        if (currentProperty.charAt(currentProperty.length() - 1) != PropertyAccessor.INDEXED_PROPERTY_SUFFIX) {
            throw new ReflectionException("Indexed property's last character must be ']'");
        } else {
            indexedProperty = currentProperty.substring(flagIdx + 1, currentProperty.length() - 1);
            currentProperty = currentProperty.substring(0, flagIdx);
        }
    } else if (flagIdx == 0) {
        throw new ReflectionException("Indexed property prefix '[' can not be located at the first location of this property: " + currentProperty);
    }
    return new SinglePropertyHolder(currentProperty, nestedPropertyPath, indexedProperty);
}
Also used : ReflectionException(com.qiuyj.commons.bean.exception.ReflectionException)

Example 2 with ReflectionException

use of com.qiuyj.commons.bean.exception.ReflectionException in project qiuyj-code by qiuyuanjun.

the class BeanWrapperImpl method doSetPropertyValue.

@Override
protected void doSetPropertyValue(String property, Object value) {
    PropertyDescriptor pd = getPropertyDescriptor(property);
    if (Objects.isNull(pd)) {
        throw new ReflectionException("Can not found property: " + property + " in class: " + wrappedClass);
    } else {
        if (Objects.nonNull(value)) {
            validateType(pd.getPropertyType(), value.getClass());
        }
        if (pd instanceof NoGetterSetterPropertyDescriptor) {
            Field propertyField = ((NoGetterSetterPropertyDescriptor) pd).getPropertyField();
            ReflectionUtils.makeAccessible(propertyField);
            try {
                propertyField.set(wrappedInstance, value);
            } catch (IllegalAccessException e) {
            // ignore
            }
        } else {
            Method writeMethod = pd.getWriteMethod();
            if (Objects.isNull(writeMethod)) {
                if (fieldOperationSupport) {
                    Field propertyField = ReflectionUtils.getDeclaredField(wrappedClass, property);
                    ReflectionUtils.makeAccessible(propertyField);
                    try {
                        propertyField.set(wrappedInstance, value);
                    } catch (IllegalAccessException e) {
                    // ingore
                    }
                } else {
                    throw new IllegalStateException("Property '" + property + "' is an readonly property.");
                }
            } else {
                ReflectionUtils.makeAccessible(writeMethod);
                ReflectionUtils.invokeMethod(wrappedInstance, writeMethod, value);
            }
        }
    }
}
Also used : ReflectionException(com.qiuyj.commons.bean.exception.ReflectionException) Field(java.lang.reflect.Field) PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method)

Example 3 with ReflectionException

use of com.qiuyj.commons.bean.exception.ReflectionException in project qiuyj-code by qiuyuanjun.

the class BeanWrapperImpl method doGetPropertyValue.

@Override
protected Object doGetPropertyValue(String property) {
    PropertyDescriptor pd = getPropertyDescriptor(property);
    Object getterValue = null;
    if (Objects.isNull(pd)) {
        throw new ReflectionException("Can not found property: " + property + " in class: " + wrappedClass);
    } else if (pd instanceof NoGetterSetterPropertyDescriptor) {
        Field propertyField = ((NoGetterSetterPropertyDescriptor) pd).getPropertyField();
        ReflectionUtils.makeAccessible(propertyField);
        try {
            getterValue = propertyField.get(wrappedInstance);
        } catch (IllegalAccessException e) {
        // ignore
        }
    } else {
        Method readMethod = pd.getReadMethod();
        if (Objects.isNull(readMethod)) {
            if (fieldOperationSupport) {
                Field propertyField = ReflectionUtils.getDeclaredField(wrappedClass, property);
                ReflectionUtils.makeAccessible(propertyField);
                try {
                    getterValue = propertyField.get(wrappedInstance);
                } catch (IllegalAccessException e) {
                // ignore
                }
            } else {
                throw new ReflectionException("Property '" + property + "' is an reading invisible property.");
            }
        } else {
            ReflectionUtils.makeAccessible(readMethod);
            getterValue = ReflectionUtils.invokeMethod(wrappedInstance, readMethod);
        }
    }
    return getterValue;
}
Also used : ReflectionException(com.qiuyj.commons.bean.exception.ReflectionException) Field(java.lang.reflect.Field) PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method)

Aggregations

ReflectionException (com.qiuyj.commons.bean.exception.ReflectionException)3 PropertyDescriptor (java.beans.PropertyDescriptor)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2