Example usage for org.apache.commons.beanutils MethodUtils getAccessibleMethod

List of usage examples for org.apache.commons.beanutils MethodUtils getAccessibleMethod

Introduction

In this page you can find the example usage for org.apache.commons.beanutils MethodUtils getAccessibleMethod.

Prototype

public static Method getAccessibleMethod(Method method) 

Source Link

Document

Return an accessible method (that is, one that can be invoked via reflection) that implements the specified Method.

Usage

From source file:org.apache.myfaces.el.MethodBindingImpl.java

public Object invoke(FacesContext facesContext, Object[] args)
        throws EvaluationException, MethodNotFoundException {
    if (facesContext == null) {
        throw new NullPointerException("facesContext");
    }//from w  ww. j ava  2  s  . c o  m
    try {
        Object[] baseAndProperty = resolveToBaseAndProperty(facesContext);
        Object base = baseAndProperty[0];
        Object property = baseAndProperty[1];

        Method m = base.getClass().getMethod(property.toString(), _argClasses);

        // Check if the concrete class of this method is accessible and if not
        // search for a public interface that declares this method
        m = MethodUtils.getAccessibleMethod(m);
        if (m == null) {
            throw new MethodNotFoundException(getExpressionString() + " (not accessible!)");
        }

        return m.invoke(base, args);
    } catch (ReferenceSyntaxException e) {
        throw e;
    } catch (IndexOutOfBoundsException e) {
        // ArrayIndexOutOfBoundsException also here
        throw new PropertyNotFoundException("Expression: " + getExpressionString(), e);
    } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();
        if (cause != null) {
            if (cause instanceof ValidatorException || cause instanceof AbortProcessingException) {
                throw new EvaluationException(cause);
            } else {
                throw new EvaluationException("Exception while invoking expression " + getExpressionString(),
                        cause);
            }
        } else {
            throw new EvaluationException("Exception while invoking expression " + getExpressionString(), e);
        }
    } catch (Exception e) {
        throw new EvaluationException("Exception while invoking expression " + getExpressionString(), e);
    }
}

From source file:org.apache.myfaces.el.PropertyResolverImpl.java

public static void setProperty(Object base, String name, Object newValue) {
    PropertyDescriptor propertyDescriptor = getPropertyDescriptor(base, name);

    Method m = propertyDescriptor.getWriteMethod();
    if (m == null) {
        throw new PropertyNotFoundException(getMessage(base, name) + " (no write method for property!)");
    }/* w  ww  . jav  a  2s. c  o  m*/

    // Check if the concrete class of this method is accessible and if not
    // search for a public interface that declares this method
    m = MethodUtils.getAccessibleMethod(m);
    if (m == null) {
        throw new PropertyNotFoundException(getMessage(base, name) + " (not accessible!)");
    }

    try {
        m.invoke(base, new Object[] { newValue });
    } catch (Throwable t) {
        log.debug("Exception while invoking setter method.", t);
        throw new EvaluationException(getMessage(base, name, newValue, m), t);
    }
}

From source file:org.apache.myfaces.el.PropertyResolverImpl.java

public static Object getProperty(Object base, String name) {
    PropertyDescriptor propertyDescriptor = getPropertyDescriptor(base, name);

    Method m = propertyDescriptor.getReadMethod();
    if (m == null) {
        throw new PropertyNotFoundException(getMessage(base, name));
    }/*from w  w  w.jav a  2  s .c om*/

    // Check if the concrete class of this method is accessible and if not
    // search for a public interface that declares this method
    m = MethodUtils.getAccessibleMethod(m);
    if (m == null) {
        throw new PropertyNotFoundException(getMessage(base, name) + " (not accessible!)");
    }

    try {
        return m.invoke(base, NO_ARGS);
    } catch (Throwable t) {
        throw new EvaluationException(getMessage(base, name), t);
    }
}

From source file:org.oddjob.framework.WrapDynaClass.java

/**
 * Introspect our bean class to identify the supported properties.
 */// www  .j a v  a  2s  . c  om
protected void introspect(Class<?> beanClass) {
    Set<String> mismatched = new HashSet<String>();

    // first find simple and indexed properties via usual means.
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass);
    for (int i = 0; i < descriptors.length; ++i) {
        PropertyDescriptor descriptor = descriptors[i];

        String propertyName = descriptor.getName();

        DynaProperty dynaProperty;
        // indexed property?
        if (descriptor instanceof IndexedPropertyDescriptor) {
            dynaProperty = new DynaProperty(propertyName, descriptor.getPropertyType(),
                    ((IndexedPropertyDescriptor) descriptor).getIndexedPropertyType());
        }
        // else a simple property.
        else {
            dynaProperty = new DynaProperty(propertyName, descriptor.getPropertyType());
        }

        propertiesMap.put(propertyName, dynaProperty);

        // update readable writable
        if (MethodUtils.getAccessibleMethod(descriptor.getReadMethod()) != null) {
            readableProperties.add(propertyName);
        }
        if (MethodUtils.getAccessibleMethod(descriptor.getWriteMethod()) != null) {
            writableProperties.add(propertyName);
        }
    }

    // now find mapped properties.
    Method[] methods = beanClass.getMethods();
    for (int i = 0; i < methods.length; ++i) {
        Method method = methods[i];

        // methods beginning with get could be properties.          
        if (!method.getName().startsWith("get") && !method.getName().startsWith("set")) {
            continue;
        }

        String propertyName = method.getName().substring(3);
        // get on it's own is not a property
        if (propertyName.length() == 0) {
            continue;
        }

        // lowercase first letter
        propertyName = propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1);

        Class<?>[] args = method.getParameterTypes();

        DynaProperty dynaProperty = null;

        boolean readable = false;
        boolean writable = false;

        // is mapped property?
        if (method.getName().startsWith("get") && Void.TYPE != method.getReturnType() && args.length == 1
                && args[0] == String.class) {
            DynaProperty existing = (DynaProperty) propertiesMap.get(propertyName);
            if (existing != null && !existing.isMapped()) {
                mismatched.add(propertyName);
                continue;
            }
            dynaProperty = new DynaProperty(propertyName, Map.class, method.getReturnType());
            readable = true;
        } else if (args.length == 2 && args[0] == String.class && Void.TYPE == method.getReturnType()) {
            DynaProperty existing = (DynaProperty) propertiesMap.get(propertyName);
            if (existing != null && !existing.isMapped()) {
                mismatched.add(propertyName);
                continue;
            }
            dynaProperty = new DynaProperty(propertyName, Map.class, args[1]);
            writable = true;
        } else {
            continue;
        }
        propertiesMap.put(propertyName, dynaProperty);

        // update readable writable
        if (readable) {
            readableProperties.add(propertyName);
        }
        if (writable) {
            writableProperties.add(propertyName);
        }
    }

    for (String element : mismatched) {
        propertiesMap.remove(element);
        readableProperties.remove(element);
        writableProperties.remove(element);
    }

    properties = (DynaProperty[]) propertiesMap.values().toArray(new DynaProperty[0]);

}