Search in sources :

Example 1 with ConstructorInstance

use of lucee.runtime.reflection.pairs.ConstructorInstance in project Lucee by lucee.

the class Reflector method getMethodInstance.

/**
 * gets the MethodInstance matching given Parameter
 * @param clazz Class Of the Method to get
 * @param methodName Name of the Method to get
 * @param args Arguments of the Method to get
 * @return return Matching Method
 * @throws NoSuchMethodException
 * @throws PageException
 */
public static MethodInstance getMethodInstance(Object obj, Class clazz, String methodName, Object[] args) throws NoSuchMethodException {
    MethodInstance mi = getMethodInstanceEL(obj, clazz, KeyImpl.getInstance(methodName), args);
    if (mi != null)
        return mi;
    Class[] classes = getClasses(args);
    // StringBuilder sb=null;
    JavaObject jo;
    Class c;
    ConstructorInstance ci;
    for (int i = 0; i < classes.length; i++) {
        if (args[i] instanceof JavaObject) {
            jo = (JavaObject) args[i];
            c = jo.getClazz();
            ci = Reflector.getConstructorInstance(c, new Object[0], null);
            if (ci == null) {
                throw new NoSuchMethodException("The " + pos(i + 1) + " parameter of " + methodName + "(" + getDspMethods(classes) + ") ia a object created " + "by the createObject function (JavaObject/JavaProxy). This object has not been instantiated because it does not have a constructor " + "that takes zero arguments. " + Constants.NAME + " cannot instantiate it for you, please use the .init(...) method to instantiate it with the correct parameters first");
            }
        }
    }
    /*
        the argument list contains objects created by createObject, 
        that are no instantiated (first,third,10th) and because this object have no constructor taking no arguments, Lucee cannot instantiate them.
        you need first to instantiate this objects. 
        */
    throw new NoSuchMethodException("No matching Method for " + methodName + "(" + getDspMethods(classes) + ") found for " + Caster.toTypeName(clazz));
}
Also used : JavaObject(lucee.runtime.java.JavaObject) MethodInstance(lucee.runtime.reflection.pairs.MethodInstance) JavaObject(lucee.runtime.java.JavaObject) ConstructorInstance(lucee.runtime.reflection.pairs.ConstructorInstance)

Example 2 with ConstructorInstance

use of lucee.runtime.reflection.pairs.ConstructorInstance in project Lucee by lucee.

the class Reflector method getConstructorInstance.

public static ConstructorInstance getConstructorInstance(Class clazz, Object[] args, ConstructorInstance defaultValue) {
    args = cleanArgs(args);
    // getConstructors(clazz);
    Constructor[] constructors = cStorage.getConstructors(clazz, args.length);
    if (constructors != null) {
        Class[] clazzArgs = getClasses(args);
        // exact comparsion
        outer: for (int i = 0; i < constructors.length; i++) {
            if (constructors[i] != null) {
                Class[] parameterTypes = constructors[i].getParameterTypes();
                for (int y = 0; y < parameterTypes.length; y++) {
                    if (toReferenceClass(parameterTypes[y]) != clazzArgs[y])
                        continue outer;
                }
                return new ConstructorInstance(constructors[i], args);
            }
        }
        // like comparsion
        outer: for (int i = 0; i < constructors.length; i++) {
            if (constructors[i] != null) {
                Class[] parameterTypes = constructors[i].getParameterTypes();
                for (int y = 0; y < parameterTypes.length; y++) {
                    if (!like(clazzArgs[y], toReferenceClass(parameterTypes[y])))
                        continue outer;
                }
                return new ConstructorInstance(constructors[i], args);
            }
        }
        // convert comparsion
        ConstructorInstance ci = null;
        int _rating = 0;
        outer: for (int i = 0; i < constructors.length; i++) {
            if (constructors[i] != null) {
                RefInteger rating = (constructors.length > 1) ? new RefIntegerImpl(0) : null;
                Class[] parameterTypes = constructors[i].getParameterTypes();
                Object[] newArgs = new Object[args.length];
                for (int y = 0; y < parameterTypes.length; y++) {
                    try {
                        newArgs[y] = convert(args[y], toReferenceClass(parameterTypes[y]), rating);
                    } catch (PageException e) {
                        continue outer;
                    }
                }
                if (ci == null || rating.toInt() > _rating) {
                    if (rating != null)
                        _rating = rating.toInt();
                    ci = new ConstructorInstance(constructors[i], newArgs);
                }
            // return new ConstructorInstance(constructors[i],newArgs);
            }
        }
        return ci;
    }
    return defaultValue;
// throw new NoSuchMethodException("No matching Constructor for "+clazz.getName()+"("+getDspMethods(getClasses(args))+") found");
}
Also used : PageException(lucee.runtime.exp.PageException) Constructor(java.lang.reflect.Constructor) RefInteger(lucee.commons.lang.types.RefInteger) ConstructorInstance(lucee.runtime.reflection.pairs.ConstructorInstance) RefIntegerImpl(lucee.commons.lang.types.RefIntegerImpl)

Example 3 with ConstructorInstance

use of lucee.runtime.reflection.pairs.ConstructorInstance in project Lucee by lucee.

the class XMLConfigWebFactory method loadMonitors.

private static void loadMonitors(ConfigServerImpl configServer, ConfigImpl config, Document doc) throws IOException {
    // only load in server context
    if (configServer != null)
        return;
    configServer = (ConfigServerImpl) config;
    Element parent = getChildByName(doc.getDocumentElement(), "monitoring");
    boolean enabled = Caster.toBooleanValue(getAttr(parent, "enabled"), false);
    configServer.setMonitoringEnabled(enabled);
    SystemOut.printDate(config.getOutWriter(), "monitoring is " + (enabled ? "enabled" : "disabled"));
    Element[] children = getChildren(parent, "monitor");
    java.util.List<IntervallMonitor> intervalls = new ArrayList<IntervallMonitor>();
    java.util.List<RequestMonitor> requests = new ArrayList<RequestMonitor>();
    java.util.List<MonitorTemp> actions = new ArrayList<MonitorTemp>();
    String strType, name;
    ClassDefinition cd;
    boolean log, async;
    short type;
    for (int i = 0; i < children.length; i++) {
        Element el = children[i];
        cd = getClassDefinition(el, "", config.getIdentification());
        strType = getAttr(el, "type");
        name = getAttr(el, "name");
        async = Caster.toBooleanValue(getAttr(el, "async"), false);
        log = Caster.toBooleanValue(getAttr(el, "log"), true);
        if ("request".equalsIgnoreCase(strType))
            type = IntervallMonitor.TYPE_REQUEST;
        else if ("action".equalsIgnoreCase(strType))
            type = Monitor.TYPE_ACTION;
        else
            type = IntervallMonitor.TYPE_INTERVAL;
        if (cd.hasClass() && !StringUtil.isEmpty(name)) {
            name = name.trim();
            try {
                Class clazz = cd.getClazz();
                Object obj;
                ConstructorInstance constr = Reflector.getConstructorInstance(clazz, new Object[] { configServer }, null);
                if (constr != null)
                    obj = constr.invoke();
                else
                    obj = clazz.newInstance();
                SystemOut.printDate(config.getOutWriter(), "loaded " + (strType) + " monitor [" + clazz.getName() + "]");
                if (type == IntervallMonitor.TYPE_INTERVAL) {
                    IntervallMonitor m = obj instanceof IntervallMonitor ? (IntervallMonitor) obj : new IntervallMonitorWrap(obj);
                    m.init(configServer, name, log);
                    intervalls.add(m);
                } else if (type == Monitor.TYPE_ACTION) {
                    ActionMonitor am = obj instanceof ActionMonitor ? (ActionMonitor) obj : new ActionMonitorWrap(obj);
                    actions.add(new MonitorTemp(am, name, log));
                } else {
                    RequestMonitorPro m = new RequestMonitorProImpl(obj instanceof RequestMonitor ? (RequestMonitor) obj : new RequestMonitorWrap(obj));
                    if (async)
                        m = new AsyncRequestMonitor(m);
                    m.init(configServer, name, log);
                    SystemOut.printDate(config.getOutWriter(), "initialize " + (strType) + " monitor [" + clazz.getName() + "]");
                    requests.add(m);
                }
            } catch (Throwable t) {
                ExceptionUtil.rethrowIfNecessary(t);
                SystemOut.printDate(config.getErrWriter(), ExceptionUtil.getStacktrace(t, true));
            }
        }
    }
    configServer.setRequestMonitors(requests.toArray(new RequestMonitor[requests.size()]));
    configServer.setIntervallMonitors(intervalls.toArray(new IntervallMonitor[intervalls.size()]));
    ActionMonitorCollector actionMonitorCollector = ActionMonitorFatory.getActionMonitorCollector(configServer, actions.toArray(new MonitorTemp[actions.size()]));
    configServer.setActionMonitorCollector(actionMonitorCollector);
    ((CFMLEngineImpl) configServer.getCFMLEngine()).touchMonitor(configServer);
}
Also used : RequestMonitorPro(lucee.runtime.monitor.RequestMonitorPro) IntervallMonitor(lucee.runtime.monitor.IntervallMonitor) RequestMonitorWrap(lucee.runtime.monitor.RequestMonitorWrap) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ConstructorInstance(lucee.runtime.reflection.pairs.ConstructorInstance) ClassDefinition(lucee.runtime.db.ClassDefinition) RequestMonitorProImpl(lucee.runtime.monitor.RequestMonitorProImpl) ActionMonitor(lucee.runtime.monitor.ActionMonitor) ActionMonitorWrap(lucee.runtime.monitor.ActionMonitorWrap) lucee.aprint(lucee.aprint) CFMLEngineImpl(lucee.runtime.engine.CFMLEngineImpl) AsyncRequestMonitor(lucee.runtime.monitor.AsyncRequestMonitor) ActionMonitorCollector(lucee.runtime.monitor.ActionMonitorCollector) CFXTagClass(lucee.runtime.cfx.customtag.CFXTagClass) CPPCFXTagClass(lucee.runtime.cfx.customtag.CPPCFXTagClass) JavaCFXTagClass(lucee.runtime.cfx.customtag.JavaCFXTagClass) IntervallMonitorWrap(lucee.runtime.monitor.IntervallMonitorWrap) RequestMonitor(lucee.runtime.monitor.RequestMonitor) AsyncRequestMonitor(lucee.runtime.monitor.AsyncRequestMonitor)

Aggregations

ConstructorInstance (lucee.runtime.reflection.pairs.ConstructorInstance)3 Constructor (java.lang.reflect.Constructor)1 ArrayList (java.util.ArrayList)1 lucee.aprint (lucee.aprint)1 RefInteger (lucee.commons.lang.types.RefInteger)1 RefIntegerImpl (lucee.commons.lang.types.RefIntegerImpl)1 CFXTagClass (lucee.runtime.cfx.customtag.CFXTagClass)1 CPPCFXTagClass (lucee.runtime.cfx.customtag.CPPCFXTagClass)1 JavaCFXTagClass (lucee.runtime.cfx.customtag.JavaCFXTagClass)1 ClassDefinition (lucee.runtime.db.ClassDefinition)1 CFMLEngineImpl (lucee.runtime.engine.CFMLEngineImpl)1 PageException (lucee.runtime.exp.PageException)1 JavaObject (lucee.runtime.java.JavaObject)1 ActionMonitor (lucee.runtime.monitor.ActionMonitor)1 ActionMonitorCollector (lucee.runtime.monitor.ActionMonitorCollector)1 ActionMonitorWrap (lucee.runtime.monitor.ActionMonitorWrap)1 AsyncRequestMonitor (lucee.runtime.monitor.AsyncRequestMonitor)1 IntervallMonitor (lucee.runtime.monitor.IntervallMonitor)1 IntervallMonitorWrap (lucee.runtime.monitor.IntervallMonitorWrap)1 RequestMonitor (lucee.runtime.monitor.RequestMonitor)1