Search in sources :

Example 1 with ReflectionFailureException

use of org.dbflute.util.DfReflectionUtil.ReflectionFailureException in project dbflute-core by dbflute.

the class DfRelativeDateResolver method invokeMethod.

protected HandyDate invokeMethod(String tableName, String columnName, String relativeDate, HandyDate handyDate, String methodCall) {
    if (!methodCall.contains("(") || !methodCall.endsWith(")")) {
        throwLoadDataRelativeDateMethodArgPartNotFoundException(tableName, columnName, relativeDate);
    }
    final String methodName = Srl.substringFirstFront(methodCall, "(");
    final String methodArgsPart = Srl.substringFirstFront(Srl.substringFirstRear(methodCall, "("), ")");
    final List<String> argElementList;
    if (Srl.is_NotNull_and_NotTrimmedEmpty(methodArgsPart)) {
        argElementList = Srl.splitListTrimmed(methodArgsPart, ",");
    } else {
        argElementList = DfCollectionUtil.emptyList();
    }
    final List<Object> argValueList = DfCollectionUtil.newArrayList();
    for (String arg : argElementList) {
        if (isNumber(arg)) {
            // int only supported (cannot use long)
            argValueList.add(DfTypeUtil.toInteger(arg));
        } else {
            argValueList.add(arg);
        }
    }
    final List<Class<?>> argTypeList = DfCollectionUtil.newArrayList();
    for (Object argValue : argValueList) {
        final Class<? extends Object> argType = argValue.getClass();
        if (Integer.class.equals(argType)) {
            // even if the argument value is int type, getClass() returns Integer type
            argTypeList.add(int.class);
        } else {
            argTypeList.add(argType);
        }
    }
    final Class<?>[] argTypes = argTypeList.toArray(new Class<?>[argTypeList.size()]);
    final Class<HandyDate> handyDateType = HandyDate.class;
    final Method method = DfReflectionUtil.getPublicMethod(handyDateType, methodName, argTypes);
    if (method == null) {
        throwLoadDataRelativeDateMethodNotFoundException(tableName, columnName, relativeDate, handyDateType, methodName, argTypes);
    }
    try {
        handyDate = (HandyDate) DfReflectionUtil.invoke(method, handyDate, argValueList.toArray());
    } catch (ReflectionFailureException e) {
        throwLoadDataRelativeDateInvokeFailureException(tableName, columnName, relativeDate, handyDateType, methodName, e);
    }
    return handyDate;
}
Also used : HandyDate(org.dbflute.helper.HandyDate) Method(java.lang.reflect.Method) ReflectionFailureException(org.dbflute.util.DfReflectionUtil.ReflectionFailureException)

Example 2 with ReflectionFailureException

use of org.dbflute.util.DfReflectionUtil.ReflectionFailureException in project dbflute-core by dbflute.

the class AbstractConditionQuery method invokeOrderBy.

/**
 * {@inheritDoc}
 */
public void invokeOrderBy(String columnFlexibleName, boolean isAsc) {
    assertStringNotNullAndNotTrimmedEmpty("columnFlexibleName", columnFlexibleName);
    final PropertyNameCQContainer container = xhelpExtractingPropertyNameCQContainer(columnFlexibleName);
    final String flexibleName = container.getFlexibleName();
    final ConditionQuery cq = container.getConditionQuery();
    final String ascDesc = isAsc ? "Asc" : "Desc";
    final DBMeta dbmeta = findDBMeta(cq.asTableDbName());
    final String columnCapPropName = initCap(dbmeta.findColumnInfo(flexibleName).getPropertyName());
    final String methodName = "addOrderBy_" + columnCapPropName + "_" + ascDesc;
    final Method method = xhelpGettingCQMethod(cq, methodName, (Class<?>[]) null);
    if (method == null) {
        throwConditionInvokingOrderMethodNotFoundException(columnFlexibleName, isAsc, methodName);
    }
    try {
        xhelpInvokingCQMethod(cq, method, (Object[]) null);
    } catch (ReflectionFailureException e) {
        throwConditionInvokingOrderReflectionFailureException(columnFlexibleName, isAsc, methodName, e);
    }
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta) Method(java.lang.reflect.Method) ReflectionFailureException(org.dbflute.util.DfReflectionUtil.ReflectionFailureException)

Example 3 with ReflectionFailureException

use of org.dbflute.util.DfReflectionUtil.ReflectionFailureException in project dbflute-core by dbflute.

the class AbstractConditionQuery method doInvokeQuery.

protected void doInvokeQuery(String colName, String ckey, Object value, ConditionOption option) {
    assertStringNotNullAndNotTrimmedEmpty("columnFlexibleName", colName);
    assertStringNotNullAndNotTrimmedEmpty("conditionKeyName", ckey);
    final boolean noArg = Srl.equalsIgnoreCase(ckey, "IsNull", "IsNotNull", "IsNullOrEmpty", "EmptyString");
    if (!noArg && (value == null || "".equals(value))) {
        if (xgetSqlClause().isNullOrEmptyQueryChecked()) {
            // as default
            String msg = "The conditionValue is required but null or empty: column=" + colName + " value=" + value;
            throw new IllegalConditionBeanOperationException(msg);
        } else {
            // e.g. when cb.ignoreNullOrEmptyQuery()
            return;
        }
    }
    final PropertyNameCQContainer container = xhelpExtractingPropertyNameCQContainer(colName);
    final String flexibleName = container.getFlexibleName();
    final ConditionQuery cq = container.getConditionQuery();
    final DBMeta dbmeta = findDBMeta(cq.asTableDbName());
    final ColumnInfo columnInfo;
    try {
        columnInfo = dbmeta.findColumnInfo(flexibleName);
    } catch (RuntimeException e) {
        throwConditionInvokingColumnFindFailureException(colName, ckey, value, option, e);
        // unreachable (to avoid compile error)
        return;
    }
    final String columnCapPropName = initCap(columnInfo.getPropertyName());
    final boolean rangeOf = Srl.equalsIgnoreCase(ckey, "RangeOf");
    final boolean fromTo = Srl.equalsIgnoreCase(ckey, "FromTo", "DateFromTo");
    final boolean inScope = Srl.equalsIgnoreCase(ckey, "InScope");
    if (!noArg) {
        try {
            // convert type
            value = columnInfo.convertToObjectNativeType(value);
        } catch (RuntimeException e) {
            throwConditionInvokingValueConvertFailureException(colName, ckey, value, option, e);
        }
    }
    final String methodName = xbuildQuerySetMethodName(ckey, columnCapPropName);
    final List<Class<?>> typeList = newArrayListSized(4);
    final Class<?> propertyType = columnInfo.getObjectNativeType();
    if (fromTo) {
        if (LocalDate.class.isAssignableFrom(propertyType)) {
            // #date_parade
            typeList.add(propertyType);
            typeList.add(propertyType);
        } else if (LocalDateTime.class.isAssignableFrom(propertyType)) {
            typeList.add(propertyType);
            typeList.add(propertyType);
        } else {
            // fixedly util.Date
            typeList.add(Date.class);
            typeList.add(Date.class);
        }
    } else if (rangeOf) {
        typeList.add(propertyType);
        typeList.add(propertyType);
    } else {
        if (!noArg) {
            final Class<?> instanceType = value.getClass();
            if (inScope && Collection.class.isAssignableFrom(instanceType)) {
                // double check just in case
                // inScope's argument is fixed type
                typeList.add(Collection.class);
            } else {
                typeList.add(instanceType);
            }
        }
    }
    if (option != null) {
        typeList.add(option.getClass());
    }
    final List<Class<?>> filteredTypeList = newArrayListSized(typeList.size());
    for (Class<?> parameterType : typeList) {
        filteredTypeList.add(xfilterInvokeQueryParameterType(colName, ckey, parameterType));
    }
    final Class<?>[] parameterTypes = filteredTypeList.toArray(new Class<?>[filteredTypeList.size()]);
    final Method method = xhelpGettingCQMethod(cq, methodName, parameterTypes);
    if (method == null) {
        throwConditionInvokingSetMethodNotFoundException(colName, ckey, value, option, methodName, parameterTypes);
    }
    try {
        final List<Object> argList = newArrayList();
        if (fromTo || rangeOf) {
            if (!(value instanceof List<?>)) {
                // check type
                throwConditionInvokingDateFromToValueInvalidException(colName, ckey, value, option, methodName, parameterTypes);
            }
            argList.addAll((List<?>) value);
        } else {
            if (!noArg) {
                argList.add(value);
            }
        }
        if (option != null) {
            argList.add(option);
        }
        final List<Object> filteredArgList = newArrayListSized(argList.size());
        for (Object arg : argList) {
            filteredArgList.add(xfilterInvokeQueryParameterValue(colName, ckey, arg));
        }
        xhelpInvokingCQMethod(cq, method, filteredArgList.toArray());
    } catch (ReflectionFailureException e) {
        throwConditionInvokingSetReflectionFailureException(colName, ckey, value, option, methodName, parameterTypes, e);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) Method(java.lang.reflect.Method) ReflectionFailureException(org.dbflute.util.DfReflectionUtil.ReflectionFailureException) Date(java.util.Date) LocalDate(java.time.LocalDate) DBMeta(org.dbflute.dbmeta.DBMeta) IllegalConditionBeanOperationException(org.dbflute.exception.IllegalConditionBeanOperationException) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with ReflectionFailureException

use of org.dbflute.util.DfReflectionUtil.ReflectionFailureException 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 5 with ReflectionFailureException

use of org.dbflute.util.DfReflectionUtil.ReflectionFailureException in project lastaflute by lastaflute.

the class RelativeDateScript method invokeMethod.

protected HandyDate invokeMethod(String relativeDate, HandyDate handyDate, String methodCall) {
    if (!methodCall.contains("(") || !methodCall.endsWith(")")) {
        throwRelativeDateMethodArgPartNotFoundException(relativeDate);
    }
    final String methodName = Srl.substringFirstFront(methodCall, "(");
    final String methodArgsPart = Srl.substringFirstFront(Srl.substringFirstRear(methodCall, "("), ")");
    final List<String> argElementList;
    if (Srl.is_NotNull_and_NotTrimmedEmpty(methodArgsPart)) {
        argElementList = Srl.splitListTrimmed(methodArgsPart, ",");
    } else {
        argElementList = DfCollectionUtil.emptyList();
    }
    final List<Object> argValueList = DfCollectionUtil.newArrayList();
    for (String arg : argElementList) {
        if (isNumber(arg)) {
            // int only supported (cannot use long)
            argValueList.add(DfTypeUtil.toInteger(arg));
        } else {
            argValueList.add(arg);
        }
    }
    final List<Class<?>> argTypeList = DfCollectionUtil.newArrayList();
    for (Object argValue : argValueList) {
        final Class<? extends Object> argType = argValue.getClass();
        if (Integer.class.equals(argType)) {
            // even if the argument value is int type, getClass() returns Integer type
            argTypeList.add(int.class);
        } else {
            argTypeList.add(argType);
        }
    }
    final Class<?>[] argTypes = argTypeList.toArray(new Class<?>[argTypeList.size()]);
    final Class<HandyDate> handyDateType = HandyDate.class;
    final Method method = DfReflectionUtil.getPublicMethod(handyDateType, methodName, argTypes);
    if (method == null) {
        throwRelativeDateMethodNotFoundException(relativeDate, handyDateType, methodName, argTypes);
    }
    try {
        handyDate = (HandyDate) DfReflectionUtil.invoke(method, handyDate, argValueList.toArray());
    } catch (ReflectionFailureException e) {
        throwRelativeDateInvokeFailureException(relativeDate, handyDateType, methodName, e);
    }
    return handyDate;
}
Also used : HandyDate(org.dbflute.helper.HandyDate) Method(java.lang.reflect.Method) ReflectionFailureException(org.dbflute.util.DfReflectionUtil.ReflectionFailureException)

Aggregations

Method (java.lang.reflect.Method)10 ReflectionFailureException (org.dbflute.util.DfReflectionUtil.ReflectionFailureException)10 DBMeta (org.dbflute.dbmeta.DBMeta)3 ConditionInvokingFailureException (org.dbflute.exception.ConditionInvokingFailureException)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 HandyDate (org.dbflute.helper.HandyDate)2 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Map (java.util.Map)1 ConditionValue (org.dbflute.cbean.cvalue.ConditionValue)1 SpecifiedColumn (org.dbflute.cbean.dream.SpecifiedColumn)1 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)1 IllegalConditionBeanOperationException (org.dbflute.exception.IllegalConditionBeanOperationException)1 DfBeanDesc (org.dbflute.helper.beans.DfBeanDesc)1 DfPropertyDesc (org.dbflute.helper.beans.DfPropertyDesc)1 DfBeanIllegalPropertyException (org.dbflute.helper.beans.exception.DfBeanIllegalPropertyException)1 DfBeanMethodNotFoundException (org.dbflute.helper.beans.exception.DfBeanMethodNotFoundException)1