Search in sources :

Example 1 with UDFPlus

use of lucee.runtime.type.UDFPlus in project Lucee by lucee.

the class Caster method toArray.

/**
 * cast a Object to a Array Object
 * @param o Object to cast
 * @return casted Array
 * @throws PageException
 */
public static Array toArray(Object o) throws PageException {
    if (o instanceof Array)
        return (Array) o;
    else if (o instanceof Object[]) {
        return new ArrayImpl((Object[]) o);
    } else if (o instanceof List) {
        // new ArrayImpl(((List) o).toArray());
        return ListAsArray.toArray((List) o);
    } else if (o instanceof Set) {
        // new ArrayImpl(((List) o).toArray());
        return toArray(((Set) o).toArray());
    } else if (o instanceof XMLStruct) {
        XMLMultiElementStruct xmes;
        if (o instanceof XMLMultiElementStruct) {
            xmes = (XMLMultiElementStruct) o;
        } else {
            XMLStruct sct = (XMLStruct) o;
            Array a = new ArrayImpl();
            a.append(o);
            xmes = new XMLMultiElementStruct(a, sct.getCaseSensitive());
        }
        return new XMLMultiElementArray(xmes);
    } else if (o instanceof ObjectWrap) {
        return toArray(((ObjectWrap) o).getEmbededObject());
    } else if (o instanceof Struct) {
        // function _toArray
        if (o instanceof Component) {
            Component c = (Component) o;
            PageContext pc = ThreadLocalPageContext.get();
            if (pc != null) {
                Member member = c.getMember(Component.ACCESS_PRIVATE, KeyConstants.__toArray, false, false);
                // Object o = get(pc,"_toString",null);
                if (member instanceof UDFPlus) {
                    UDFPlus udf = (UDFPlus) member;
                    if (udf.getReturnType() == CFTypes.TYPE_ARRAY && udf.getFunctionArguments().length == 0) {
                        return Caster.toArray(c.call(pc, KeyConstants.__toArray, new Object[0]));
                    }
                }
            }
        }
        Struct sct = (Struct) o;
        Array arr = new ArrayImpl();
        Iterator<Entry<Key, Object>> it = sct.entryIterator();
        Entry<Key, Object> e = null;
        try {
            while (it.hasNext()) {
                e = it.next();
                arr.setE(toIntValue(e.getKey().getString()), e.getValue());
            }
        } catch (ExpressionException ee) {
            throw new ExpressionException("can't cast struct to a array, key [" + e.getKey().getString() + "] is not a number");
        }
        return arr;
    } else if (o instanceof boolean[])
        return new ArrayImpl(ArrayUtil.toReferenceType((boolean[]) o));
    else if (o instanceof byte[])
        return new ArrayImpl(ArrayUtil.toReferenceType((byte[]) o));
    else if (o instanceof char[])
        return new ArrayImpl(ArrayUtil.toReferenceType((char[]) o));
    else if (o instanceof short[])
        return new ArrayImpl(ArrayUtil.toReferenceType((short[]) o));
    else if (o instanceof int[])
        return new ArrayImpl(ArrayUtil.toReferenceType((int[]) o));
    else if (o instanceof long[])
        return new ArrayImpl(ArrayUtil.toReferenceType((long[]) o));
    else if (o instanceof float[])
        return new ArrayImpl(ArrayUtil.toReferenceType((float[]) o));
    else if (o instanceof double[])
        return new ArrayImpl(ArrayUtil.toReferenceType((double[]) o));
    throw new CasterException(o, "Array");
}
Also used : ResultSet(java.sql.ResultSet) Set(java.util.Set) ArrayImpl(lucee.runtime.type.ArrayImpl) ExpressionException(lucee.runtime.exp.ExpressionException) XMLMultiElementStruct(lucee.runtime.text.xml.struct.XMLMultiElementStruct) XMLStruct(lucee.runtime.text.xml.struct.XMLStruct) ObjectStruct(lucee.runtime.type.scope.ObjectStruct) MapAsStruct(lucee.runtime.type.wrap.MapAsStruct) Struct(lucee.runtime.type.Struct) CollectionStruct(lucee.runtime.type.CollectionStruct) Entry(java.util.Map.Entry) XMLMultiElementStruct(lucee.runtime.text.xml.struct.XMLMultiElementStruct) ArrayList(java.util.ArrayList) ArrayAsList(lucee.runtime.type.wrap.ArrayAsList) List(java.util.List) NodeList(org.w3c.dom.NodeList) ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext) PageContext(lucee.runtime.PageContext) Component(lucee.runtime.Component) Member(lucee.runtime.component.Member) CasterException(lucee.runtime.exp.CasterException) ObjectWrap(lucee.runtime.type.ObjectWrap) XMLMultiElementArray(lucee.runtime.text.xml.struct.XMLMultiElementArray) UDFPlus(lucee.runtime.type.UDFPlus) Array(lucee.runtime.type.Array) XMLMultiElementArray(lucee.runtime.text.xml.struct.XMLMultiElementArray) ListAsArray(lucee.runtime.type.wrap.ListAsArray) XMLStruct(lucee.runtime.text.xml.struct.XMLStruct) JavaObject(lucee.runtime.java.JavaObject) Key(lucee.runtime.type.Collection.Key)

Example 2 with UDFPlus

use of lucee.runtime.type.UDFPlus in project Lucee by lucee.

the class ComponentImpl method setOwner.

private void setOwner(Map<Key, ? extends Member> data) {
    Member m;
    Iterator<? extends Member> it = data.values().iterator();
    while (it.hasNext()) {
        m = it.next();
        if (m instanceof UDFPlus) {
            ((UDFPlus) m).setOwnerComponent(this);
        }
    }
}
Also used : UDFPlus(lucee.runtime.type.UDFPlus) Member(lucee.runtime.component.Member) DataMember(lucee.runtime.component.DataMember)

Example 3 with UDFPlus

use of lucee.runtime.type.UDFPlus in project Lucee by lucee.

the class ComponentImpl method callGetter.

private Object callGetter(PageContext pc, Collection.Key key) throws PageException {
    Key getterName = KeyImpl.getInstance("get" + key.getLowerString());
    Member member = getMember(pc, getterName, false, false);
    if (member instanceof UDFPlus) {
        UDFPlus udf = (UDFPlus) member;
        if (udf.getFunctionArguments().length == 0 && udf.getReturnType() != CFTypes.TYPE_VOID) {
            return _call(pc, getterName, udf, null, ArrayUtil.OBJECT_EMPTY);
        }
    }
    throw new ExpressionException("Component [" + getCallName() + "] has no accessible Member with name [" + key + "]");
}
Also used : UDFPlus(lucee.runtime.type.UDFPlus) Member(lucee.runtime.component.Member) DataMember(lucee.runtime.component.DataMember) ArgumentIntKey(lucee.runtime.type.scope.ArgumentIntKey) ExpressionException(lucee.runtime.exp.ExpressionException)

Example 4 with UDFPlus

use of lucee.runtime.type.UDFPlus in project Lucee by lucee.

the class ComponentImpl method callSetter.

private Object callSetter(PageContext pc, Collection.Key key, Object value) throws PageException {
    Collection.Key setterName = KeyImpl.getInstance("set" + key.getLowerString());
    Member member = getMember(pc, setterName, false, false);
    if (member instanceof UDFPlus) {
        UDFPlus udf = (UDFPlus) member;
        if (udf.getFunctionArguments().length == 1 && (udf.getReturnType() == CFTypes.TYPE_VOID) || udf.getReturnType() == CFTypes.TYPE_ANY) {
            // TDOO support int return type
            return _call(pc, setterName, udf, null, new Object[] { value });
        }
    }
    return _set(pc, key, value);
}
Also used : Collection(lucee.runtime.type.Collection) UDFPlus(lucee.runtime.type.UDFPlus) Member(lucee.runtime.component.Member) DataMember(lucee.runtime.component.DataMember)

Example 5 with UDFPlus

use of lucee.runtime.type.UDFPlus in project Lucee by lucee.

the class ComponentImpl method _set.

/**
 * sets a value to the current Component, dont to base Component
 * @param key
 * @param value
 * @return value set
 * @throws ExpressionException
 */
private Object _set(PageContext pc, Collection.Key key, Object value) throws ExpressionException {
    if (value instanceof Member) {
        if (value instanceof UDFPlus) {
            UDFPlus udf = (UDFPlus) ((UDFPlus) value).duplicate();
            // udf.isComponentMember(true);///+++
            udf.setOwnerComponent(this);
            if (udf.getAccess() > Component.ACCESS_PUBLIC)
                udf.setAccess(Component.ACCESS_PUBLIC);
            _data.put(key, udf);
            _udfs.put(key, udf);
            hasInjectedFunctions = true;
        } else
            _data.put(key, (Member) value);
    } else {
        Member existing = _data.get(key);
        if (loaded && !isAccessible(pc, existing != null ? existing.getAccess() : dataMemberDefaultAccess))
            throw new ExpressionException("Component [" + getCallName() + "] has no accessible Member with name [" + key + "]", "enable [trigger data member] in admininistrator to also invoke getters and setters");
        _data.put(key, new DataMember(existing != null ? existing.getAccess() : dataMemberDefaultAccess, existing != null ? existing.getModifier() : Member.MODIFIER_NONE, value));
    }
    return value;
}
Also used : DataMember(lucee.runtime.component.DataMember) UDFPlus(lucee.runtime.type.UDFPlus) Member(lucee.runtime.component.Member) DataMember(lucee.runtime.component.DataMember) ExpressionException(lucee.runtime.exp.ExpressionException)

Aggregations

UDFPlus (lucee.runtime.type.UDFPlus)12 Member (lucee.runtime.component.Member)9 DataMember (lucee.runtime.component.DataMember)5 ArgumentIntKey (lucee.runtime.type.scope.ArgumentIntKey)4 ExpressionException (lucee.runtime.exp.ExpressionException)3 Component (lucee.runtime.Component)2 UDF (lucee.runtime.type.UDF)2 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 PageContext (lucee.runtime.PageContext)1 ThreadLocalPageContext (lucee.runtime.engine.ThreadLocalPageContext)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 CasterException (lucee.runtime.exp.CasterException)1 PageException (lucee.runtime.exp.PageException)1 JavaObject (lucee.runtime.java.JavaObject)1 XMLMultiElementArray (lucee.runtime.text.xml.struct.XMLMultiElementArray)1 XMLMultiElementStruct (lucee.runtime.text.xml.struct.XMLMultiElementStruct)1