Example usage for org.springframework.beans BeanUtils findDeclaredMethodWithMinimalParameters

List of usage examples for org.springframework.beans BeanUtils findDeclaredMethodWithMinimalParameters

Introduction

In this page you can find the example usage for org.springframework.beans BeanUtils findDeclaredMethodWithMinimalParameters.

Prototype

@Nullable
public static Method findDeclaredMethodWithMinimalParameters(Class<?> clazz, String methodName)
        throws IllegalArgumentException 

Source Link

Document

Find a method with the given method name and minimal parameters (best case: none), declared on the given class or one of its superclasses.

Usage

From source file:org.apache.syncope.core.persistence.dao.impl.SubjectSearchDAOImpl.java

@SuppressWarnings("rawtypes")
private String getQuery(final SubjectCond cond, final boolean not, final List<Object> parameters,
        final SubjectType type, final SearchSupport svs) {

    final AttributableUtil attrUtil = AttributableUtil.getInstance(type.asAttributableType());

    Field subjectField = ReflectionUtils.findField(attrUtil.attributableClass(), cond.getSchema());
    if (subjectField == null) {
        LOG.warn("Ignoring invalid schema '{}'", cond.getSchema());
        return EMPTY_ATTR_QUERY;
    }/*from ww  w.  ja va2 s. c  om*/

    AbstractNormalSchema schema = attrUtil.newSchema();
    schema.setName(subjectField.getName());
    for (AttributeSchemaType attrSchemaType : AttributeSchemaType.values()) {
        if (subjectField.getType().isAssignableFrom(attrSchemaType.getType())) {
            schema.setType(attrSchemaType);
        }
    }

    // Deal with subject Integer fields logically mapping to boolean values
    // (SyncopeRole.inheritAttributes, for example)
    boolean foundBooleanMin = false;
    boolean foundBooleanMax = false;
    if (Integer.class.equals(subjectField.getType())) {
        for (Annotation annotation : subjectField.getAnnotations()) {
            if (Min.class.equals(annotation.annotationType())) {
                foundBooleanMin = ((Min) annotation).value() == 0;
            } else if (Max.class.equals(annotation.annotationType())) {
                foundBooleanMax = ((Max) annotation).value() == 1;
            }
        }
    }
    if (foundBooleanMin && foundBooleanMax) {
        if ("true".equalsIgnoreCase(cond.getExpression())) {
            cond.setExpression("1");
            schema.setType(AttributeSchemaType.Long);
        } else if ("false".equalsIgnoreCase(cond.getExpression())) {
            cond.setExpression("0");
            schema.setType(AttributeSchemaType.Long);
        }
    }

    // Deal with subject fields representing relationships to other entities
    // Only _id and _name are suppored
    if (subjectField.getType().getAnnotation(Entity.class) != null) {
        if (BeanUtils.findDeclaredMethodWithMinimalParameters(subjectField.getType(), "getId") != null) {
            cond.setSchema(cond.getSchema() + "_id");
            schema.setType(AttributeSchemaType.Long);
        }
        if (BeanUtils.findDeclaredMethodWithMinimalParameters(subjectField.getType(), "getName") != null) {
            cond.setSchema(cond.getSchema() + "_name");
            schema.setType(AttributeSchemaType.String);
        }
    }

    AbstractAttrValue attrValue = attrUtil.newAttrValue();
    try {
        if (cond.getType() != AttributeCond.Type.LIKE && cond.getType() != AttributeCond.Type.ISNULL
                && cond.getType() != AttributeCond.Type.ISNOTNULL) {

            schema.getValidator().validate(cond.getExpression(), attrValue);
        }
    } catch (ValidationException e) {
        LOG.error("Could not validate expression '" + cond.getExpression() + "'", e);
        return EMPTY_ATTR_QUERY;
    }

    final StringBuilder query = new StringBuilder("SELECT DISTINCT subject_id FROM ").append(svs.field().name)
            .append(" WHERE ");

    fillAttributeQuery(query, attrValue, schema, cond, not, parameters, svs);

    return query.toString();
}

From source file:org.springframework.beans.factory.support.AbstractBeanFactory.java

/**
 * Invoke the specified custom destroy method on the given bean.
 * <p>This implementation invokes a no-arg method if found, else checking
 * for a method with a single boolean argument (passing in "true",
 * assuming a "force" parameter), else logging an error.
 * <p>Can be overridden in subclasses for custom resolution of destroy
 * methods with arguments.//from   www  .  j a v a 2  s  .  c  om
 * @param beanName the bean has in the factory. Used for debug output.
 * @param bean new bean instance we may need to notify of destruction
 * @param destroyMethodName the name of the custom destroy method
 * @param enforceDestroyMethod indicates whether the defined destroy method needs to exist
 */
protected void invokeCustomDestroyMethod(String beanName, Object bean, String destroyMethodName,
        boolean enforceDestroyMethod) {

    Method destroyMethod = BeanUtils.findDeclaredMethodWithMinimalParameters(bean.getClass(),
            destroyMethodName);
    if (destroyMethod == null) {
        if (enforceDestroyMethod) {
            logger.error("Couldn't find a destroy method named '" + destroyMethodName + "' on bean with name '"
                    + beanName + "'");
        }
    } else {
        Class[] paramTypes = destroyMethod.getParameterTypes();
        if (paramTypes.length > 1) {
            logger.error("Method '" + destroyMethodName + "' of bean '" + beanName
                    + "' has more than one parameter - not supported as destroy method");
        } else if (paramTypes.length == 1 && !paramTypes[0].equals(boolean.class)) {
            logger.error("Method '" + destroyMethodName + "' of bean '" + beanName
                    + "' has a non-boolean parameter - not supported as destroy method");
        } else {
            Object[] args = new Object[paramTypes.length];
            if (paramTypes.length == 1) {
                args[0] = Boolean.TRUE;
            }
            if (!Modifier.isPublic(destroyMethod.getModifiers())) {
                destroyMethod.setAccessible(true);
            }
            try {
                destroyMethod.invoke(bean, args);
            } catch (InvocationTargetException ex) {
                logger.error("Couldn't invoke destroy method '" + destroyMethodName + "' of bean with name '"
                        + beanName + "'", ex.getTargetException());
            } catch (Throwable ex) {
                logger.error("Couldn't invoke destroy method '" + destroyMethodName + "' of bean with name '"
                        + beanName + "'", ex);
            }
        }
    }
}