Search in sources :

Example 1 with DfBeanIllegalPropertyException

use of org.dbflute.helper.beans.exception.DfBeanIllegalPropertyException in project dbflute-core by dbflute.

the class IfCommentEvaluator method processOneProperty.

protected Object processOneProperty(Object baseObject, String firstProperty, String property) {
    if (baseObject == null) {
        throwIfCommentNullPointerException(firstProperty);
    }
    final DfBeanDesc beanDesc = DfBeanDescFactory.getBeanDesc(baseObject.getClass());
    if (beanDesc.hasPropertyDesc(property)) {
        // main case
        final DfPropertyDesc propertyDesc = beanDesc.getPropertyDesc(property);
        try {
            return propertyDesc.getValue(baseObject);
        } catch (DfBeanIllegalPropertyException e) {
            throwIfCommentPropertyReadFailureException(baseObject, propertyDesc.getPropertyName(), e);
            // unreachable
            return null;
        }
    }
    if (property.endsWith(METHOD_SUFFIX)) {
        // sub-main case
        final String methodName = property.substring(0, property.length() - METHOD_SUFFIX.length());
        try {
            final Method method = beanDesc.getMethod(methodName);
            return DfReflectionUtil.invoke(method, baseObject, (Object[]) null);
        } catch (DfBeanMethodNotFoundException e) {
            throwIfCommentNotFoundMethodException(baseObject, methodName);
            // unreachable
            return null;
        } catch (ReflectionFailureException e) {
            throwIfCommentMethodInvocationFailureException(baseObject, methodName, e);
            // unreachable
            return null;
        }
    }
    if (MapParameterBean.class.isInstance(baseObject)) {
        // used by union-query internally
        // if the key does not exist, it does not process
        // (different specification with Map)
        final Map<?, ?> map = ((MapParameterBean<?>) baseObject).getParameterMap();
        if (map.containsKey(property)) {
            return map.get(property);
        }
    }
    if (Map.class.isInstance(baseObject)) {
        // if the key does not exist, treated same as a null value
        final Map<?, ?> map = (Map<?, ?>) baseObject;
        return map.get(property);
    }
    if (List.class.isInstance(baseObject)) {
        if (property.startsWith("get(") && property.endsWith(")")) {
            final List<?> list = (List<?>) baseObject;
            final String exp = Srl.extractScopeFirst(property, "get(", ")").getContent();
            try {
                final Integer index = DfTypeUtil.toInteger(exp);
                return list.get(index);
            } catch (NumberFormatException e) {
                throwIfCommentListIndexNotNumberException(list, exp, e);
                // unreachable
                return null;
            } catch (IndexOutOfBoundsException e) {
                throwIfCommentListIndexOutOfBoundsException(list, exp, e);
                // unreachable
                return null;
            }
        }
    }
    throwIfCommentNotFoundPropertyException(baseObject, property);
    // unreachable
    return null;
}
Also used : IfCommentListIndexOutOfBoundsException(org.dbflute.twowaysql.exception.IfCommentListIndexOutOfBoundsException) Method(java.lang.reflect.Method) DfBeanMethodNotFoundException(org.dbflute.helper.beans.exception.DfBeanMethodNotFoundException) ReflectionFailureException(org.dbflute.util.DfReflectionUtil.ReflectionFailureException) DfPropertyDesc(org.dbflute.helper.beans.DfPropertyDesc) DfBeanIllegalPropertyException(org.dbflute.helper.beans.exception.DfBeanIllegalPropertyException) MapParameterBean(org.dbflute.twowaysql.pmbean.MapParameterBean) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) DfBeanDesc(org.dbflute.helper.beans.DfBeanDesc)

Example 2 with DfBeanIllegalPropertyException

use of org.dbflute.helper.beans.exception.DfBeanIllegalPropertyException in project dbflute-core by dbflute.

the class DfPropertyDescImpl method throwPropertyNotReadableException.

protected void throwPropertyNotReadableException(Object target) {
    final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
    br.addNotice("The property is not readable.");
    setupExceptionBasicInfo(br);
    br.addItem("Target Object");
    br.addElement(target != null ? target.getClass() : null);
    br.addElement(target);
    final String msg = br.buildExceptionMessage();
    throw new DfBeanIllegalPropertyException(msg);
}
Also used : ExceptionMessageBuilder(org.dbflute.helper.message.ExceptionMessageBuilder) DfBeanIllegalPropertyException(org.dbflute.helper.beans.exception.DfBeanIllegalPropertyException)

Example 3 with DfBeanIllegalPropertyException

use of org.dbflute.helper.beans.exception.DfBeanIllegalPropertyException in project dbflute-core by dbflute.

the class DfPropertyDescImpl method throwPropertyNotWritableException.

protected void throwPropertyNotWritableException(Object target, Object value) {
    final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
    br.addNotice("The property is not writable.");
    setupExceptionBasicInfo(br);
    br.addItem("Target Object");
    br.addElement(target != null ? target.getClass() : null);
    br.addElement(target);
    br.addItem("Wrote Object");
    br.addElement(value != null ? value.getClass() : null);
    br.addElement(value);
    final String msg = br.buildExceptionMessage();
    throw new DfBeanIllegalPropertyException(msg);
}
Also used : ExceptionMessageBuilder(org.dbflute.helper.message.ExceptionMessageBuilder) DfBeanIllegalPropertyException(org.dbflute.helper.beans.exception.DfBeanIllegalPropertyException)

Example 4 with DfBeanIllegalPropertyException

use of org.dbflute.helper.beans.exception.DfBeanIllegalPropertyException in project dbflute-core by dbflute.

the class DfPropertyDescImplTest method test_setValue_illegalProperty.

public void test_setValue_illegalProperty() throws Exception {
    // ## Arrange ##
    DfBeanDesc beanDesc = DfBeanDescFactory.getBeanDesc(MockBean.class);
    DfPropertyDesc pd = beanDesc.getPropertyDesc("readOnlyName");
    MockBean bean = new MockBean();
    // ## Act ##
    try {
        pd.setValue(bean, "foo");
        // ## Assert ##
        fail();
    } catch (DfBeanIllegalPropertyException e) {
        // OK
        log(e.getMessage());
    }
}
Also used : DfPropertyDesc(org.dbflute.helper.beans.DfPropertyDesc) DfBeanIllegalPropertyException(org.dbflute.helper.beans.exception.DfBeanIllegalPropertyException) DfBeanDesc(org.dbflute.helper.beans.DfBeanDesc)

Example 5 with DfBeanIllegalPropertyException

use of org.dbflute.helper.beans.exception.DfBeanIllegalPropertyException in project dbflute-core by dbflute.

the class DfPropertyDescImplTest method test_getValue_illegalProperty.

public void test_getValue_illegalProperty() throws Exception {
    // ## Arrange ##
    DfBeanDesc beanDesc = DfBeanDescFactory.getBeanDesc(MockBean.class);
    DfPropertyDesc pd = beanDesc.getPropertyDesc("writeOnlyName");
    MockBean bean = new MockBean();
    bean.setWriteOnlyName("foo");
    // ## Act ##
    try {
        pd.getValue(bean);
        // ## Assert ##
        fail();
    } catch (DfBeanIllegalPropertyException e) {
        // OK
        log(e.getMessage());
    }
}
Also used : DfPropertyDesc(org.dbflute.helper.beans.DfPropertyDesc) DfBeanIllegalPropertyException(org.dbflute.helper.beans.exception.DfBeanIllegalPropertyException) DfBeanDesc(org.dbflute.helper.beans.DfBeanDesc)

Aggregations

DfBeanIllegalPropertyException (org.dbflute.helper.beans.exception.DfBeanIllegalPropertyException)7 ExceptionMessageBuilder (org.dbflute.helper.message.ExceptionMessageBuilder)4 DfBeanDesc (org.dbflute.helper.beans.DfBeanDesc)3 DfPropertyDesc (org.dbflute.helper.beans.DfPropertyDesc)3 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 DfBeanMethodNotFoundException (org.dbflute.helper.beans.exception.DfBeanMethodNotFoundException)1 IfCommentListIndexOutOfBoundsException (org.dbflute.twowaysql.exception.IfCommentListIndexOutOfBoundsException)1 MapParameterBean (org.dbflute.twowaysql.pmbean.MapParameterBean)1 ReflectionFailureException (org.dbflute.util.DfReflectionUtil.ReflectionFailureException)1