Example usage for org.apache.commons.jxpath.util MethodLookupUtils lookupConstructor

List of usage examples for org.apache.commons.jxpath.util MethodLookupUtils lookupConstructor

Introduction

In this page you can find the example usage for org.apache.commons.jxpath.util MethodLookupUtils lookupConstructor.

Prototype

public static Constructor lookupConstructor(Class targetClass, Object[] parameters) 

Source Link

Document

Look up a constructor.

Usage

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;
    }//from  www . j  av  a 2  s .c  o m

    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;
}