Example usage for org.apache.commons.jxpath.functions ConstructorFunction ConstructorFunction

List of usage examples for org.apache.commons.jxpath.functions ConstructorFunction ConstructorFunction

Introduction

In this page you can find the example usage for org.apache.commons.jxpath.functions ConstructorFunction ConstructorFunction.

Prototype

public ConstructorFunction(Constructor constructor) 

Source Link

Document

Create a new ConstructorFunction.

Usage

From source file:org.xchain.framework.jxpath.GenericsWisePackageFunctions.java

public Function getFunction(String namespace, String name, Object[] parameters) {
    if ((namespace == null && this.namespace != null) //NOPMD
            || (namespace != null && !namespace.equals(this.namespace))) {
        return null;
    }/* ww  w  .j  a  va  2 s .  c om*/

    if (parameters == null) {
        parameters = EMPTY_ARRAY;
    }

    if (parameters.length >= 1) {
        Object target = TypeUtils.convert(parameters[0], Object.class);
        if (target != null) {
            Method method = MethodLookupUtils.lookupMethod(target.getClass(), name, parameters);
            if (method != null) {
                return new MethodFunction(method);
            }

            if (target instanceof NodeSet) {
                target = ((NodeSet) target).getPointers();
            }

            method = MethodLookupUtils.lookupMethod(target.getClass(), name, parameters);
            if (method != null) {
                return new MethodFunction(method);
            }

            if (target instanceof Collection) {
                Iterator iter = ((Collection) target).iterator();
                if (iter.hasNext()) {
                    target = iter.next();
                    if (target instanceof Pointer) {
                        target = ((Pointer) target).getValue();
                    }
                } else {
                    target = null;
                }
            }
        }
        if (target != null) {
            Method method = MethodLookupUtils.lookupMethod(target.getClass(), name, parameters);
            if (method != null) {
                return new MethodFunction(method);
            }
        }
    }

    String fullName = classPrefix + name;
    int inx = fullName.lastIndexOf('.');
    if (inx == -1) {
        return null;
    }

    String className = fullName.substring(0, inx);
    String methodName = fullName.substring(inx + 1);

    Class functionClass;
    try {
        functionClass = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
    } catch (ClassNotFoundException ex) {
        throw new JXPathException(
                "Cannot invoke extension function " + (namespace != null ? namespace + ":" + name : name), ex);
    }

    if (methodName.equals("new")) {
        Constructor constructor = MethodLookupUtils.lookupConstructor(functionClass, parameters);
        if (constructor != null) {
            return new ConstructorFunction(constructor);
        }
    } else {
        Method method = MethodLookupUtils.lookupStaticMethod(functionClass, methodName, parameters);
        if (method != null) {
            return new MethodFunction(method);
        }
    }
    return null;
}

From source file:org.xchain.framework.lifecycle.NamespaceFunctionLibrary.java

public Function getFunction(String namespace, String name, Object[] parameters) {
    // make sure that we are requesting the correct namespace.
    if (namespace == null && this.namespace != null) {
        return null;
    } else if (!namespace.equals(this.namespace)) {
        return null;
    }/*ww  w. jav a 2s. c  om*/

    if (parameters == null) {
        parameters = EMPTY_ARRAY;
    }

    Set<Class> constructorClassSet = constructorClassMap.get(name);
    Constructor constructor;
    if (constructorClassSet != null) {
        for (Class constructorClass : constructorClassSet) {
            constructor = MethodLookupUtils.lookupConstructor(constructorClass, parameters);
            if (constructor != null) {
                return new ConstructorFunction(constructor);
            }
        }
    }

    Set<MethodInfo> methodInfoSet = staticMethodInfoMap.get(name);
    Method method;

    if (methodInfoSet != null) {
        for (MethodInfo methodInfo : methodInfoSet) {
            method = MethodLookupUtils.lookupStaticMethod(methodInfo.getMethodClass(),
                    methodInfo.getMethodName(), parameters);
            if (method != null) {
                return new MethodFunction(method);
            }
        }
    }

    methodInfoSet = instanceMethodInfoMap.get(name);

    if (methodInfoSet != null) {
        for (MethodInfo methodInfo : methodInfoSet) {
            Object[] instanceParameters = parameters;
            Method singletonAccessor = methodInfo.getSingletonAccessor();
            if (singletonAccessor != null) {
                try {
                    Object singleton = singletonAccessor.invoke(null, EMPTY_ARRAY);
                    instanceParameters = new Object[parameters.length + 1];
                    instanceParameters[0] = singleton;
                    for (int i = 0; i < parameters.length; i++) {
                        instanceParameters[i + 1] = parameters[i];
                    }
                    method = MethodLookupUtils.lookupMethod(methodInfo.getMethodClass(),
                            methodInfo.getMethodName(), instanceParameters);
                    if (method != null) {
                        return new SingletonMethodFunction(method, singleton);
                    }
                } catch (Exception e) {
                    if (log.isDebugEnabled()) {
                        log.debug("Could not find singleton for class '" + methodInfo.getMethodClass().getName()
                                + "'.", e);
                    }
                }
            } else {
                method = MethodLookupUtils.lookupMethod(methodInfo.getMethodClass(), methodInfo.getMethodName(),
                        instanceParameters);
                if (method != null) {
                    return new MethodFunction(method);
                }
            }
        }
    }

    return null;
}