public Object callMethod()

in core/src/main/java/org/apache/struts2/ognl/accessor/CompoundRootAccessor.java [191:281]


    public Object callMethod(Map context, Object target, String name, Object[] objects) throws MethodFailedException {
        CompoundRoot root = (CompoundRoot) target;

        if ("describe".equals(name)) {
            Object v;
            if (objects != null && objects.length == 1) {
                v = objects[0];
            } else {
                v = root.get(0);
            }


            if (v instanceof Collection || v instanceof Map || v.getClass().isArray()) {
                return v.toString();
            }

            try {
                Map<String, PropertyDescriptor> descriptors = OgnlRuntime.getPropertyDescriptors(v.getClass());

                int maxSize = 0;
                for (String pdName : descriptors.keySet()) {
                    if (pdName.length() > maxSize) {
                        maxSize = pdName.length();
                    }
                }

                SortedSet<String> set = new TreeSet<>();

                for (PropertyDescriptor pd : descriptors.values()) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(pd.getName()).append(": ");

                    int padding = maxSize - pd.getName().length();
                    for (int i = 0; i < padding; i++) {
                        sb.append(" ");
                    }
                    sb.append(pd.getPropertyType().getName());
                    set.add(sb.toString());
                }

                StringBuilder sb = new StringBuilder();
                for (String aSet : set) {
                    sb.append(aSet).append("\n");
                }
                return sb.toString();
            } catch (IntrospectionException | OgnlException e) {
                LOG.debug("Got exception in callMethod", e);
            }
            return null;
        }

        Throwable reason = null;
        Class[] argTypes = getArgTypes(objects);
        for (Object o : root) {
            if (o == null) {
                continue;
            }

            Class clazz = o.getClass();

            MethodCall mc = null;

            if (argTypes != null) {
                mc = new MethodCall(clazz, name, argTypes);
            }

            if ((argTypes == null) || !invalidMethods.containsKey(mc)) {
                try {
                    return OgnlRuntime.callMethod((OgnlContext) context, o, name, objects);
                } catch (OgnlException e) {
                    reason = e.getReason();

                    if (reason != null && !(reason instanceof NoSuchMethodException)) {
                        // method has found but thrown an exception
                        break;
                    }

                    if ((mc != null) && (reason != null)) {
                        invalidMethods.put(mc, Boolean.TRUE);
                    }
                    // continue and try the next one
                }
            }
        }

        if (context.containsKey(OgnlValueStack.THROW_EXCEPTION_ON_FAILURE)) {
            throw new MethodFailedException(target, name, reason);
        }

        return null;
    }