Search in sources :

Example 1 with JavaObject

use of lucee.runtime.java.JavaObject 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 JavaObject

use of lucee.runtime.java.JavaObject in project Lucee by lucee.

the class BundleInfo method call.

public static Struct call(PageContext pc, Object obj) throws PageException {
    if (obj == null)
        throw new FunctionException(pc, "bundleInfo", 1, "object", "value is null");
    Class<?> clazz;
    if (obj instanceof JavaObject)
        clazz = ((JavaObject) obj).getClazz();
    else if (obj instanceof ObjectWrap)
        clazz = ((ObjectWrap) obj).getEmbededObject().getClass();
    else
        clazz = obj.getClass();
    ClassLoader cl = clazz.getClassLoader();
    if (cl instanceof BundleClassLoader) {
        BundleClassLoader bcl = (BundleClassLoader) cl;
        Bundle b = bcl.getBundle();
        Struct sct = new StructImpl();
        sct.setEL(KeyConstants._id, b.getBundleId());
        sct.setEL(KeyConstants._name, b.getSymbolicName());
        sct.setEL("location", b.getLocation());
        sct.setEL(KeyConstants._version, b.getVersion().toString());
        sct.setEL(KeyConstants._state, OSGiUtil.toState(b.getState(), null));
        try {
            sct.setEL("requiredBundles", toArray1(OSGiUtil.getRequiredBundles(b)));
            sct.setEL("requiredPackages", toArray2(OSGiUtil.getRequiredPackages(b)));
        } catch (BundleException be) {
            throw Caster.toPageException(be);
        }
        return sct;
    }
    throw new ApplicationException(obj + "given object is not from a OSGi bundle");
}
Also used : ObjectWrap(lucee.runtime.type.ObjectWrap) StructImpl(lucee.runtime.type.StructImpl) ApplicationException(lucee.runtime.exp.ApplicationException) JavaObject(lucee.runtime.java.JavaObject) BundleClassLoader(org.apache.felix.framework.BundleWiringImpl.BundleClassLoader) Bundle(org.osgi.framework.Bundle) FunctionException(lucee.runtime.exp.FunctionException) BundleClassLoader(org.apache.felix.framework.BundleWiringImpl.BundleClassLoader) BundleException(org.osgi.framework.BundleException) Struct(lucee.runtime.type.Struct)

Example 3 with JavaObject

use of lucee.runtime.java.JavaObject in project Lucee by lucee.

the class CreateDynamicProxy method _call.

public static Object _call(PageContext pc, Object oCFC, Object oInterfaces) throws PageException, IOException {
    if (SystemUtil.getLoaderVersion() < 5.9D)
        throw new ApplicationException("You need to update your lucee.jar to execute the function [createDynamicProxy], you can download the latest jar from http://download.lucee.org.");
    // Component
    Component cfc;
    if (oCFC instanceof Component)
        cfc = (Component) oCFC;
    else
        cfc = pc.loadComponent(Caster.toString(oCFC));
    // string list to array
    if (Decision.isString(oInterfaces)) {
        String list = Caster.toString(oInterfaces);
        oInterfaces = ListUtil.listToStringArray(list, ',');
    }
    Class[] interfaces = null;
    if (Decision.isArray(oInterfaces)) {
        Object[] arr = Caster.toNativeArray(oInterfaces);
        ClassLoader cl = ((PageContextImpl) pc).getClassLoader();
        interfaces = new Class[arr.length];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] instanceof JavaObject)
                interfaces[i] = ((JavaObject) arr[i]).getClazz();
            else
                interfaces[i] = ClassUtil.loadClass(cl, Caster.toString(arr[i]));
        }
    // strInterfaces=ListUtil.toStringArray(Caster.toArray(oInterfaces));
    } else if (oInterfaces instanceof JavaObject) {
        interfaces = new Class[] { ((JavaObject) oInterfaces).getClazz() };
    } else
        throw new FunctionException(pc, "CreateDynamicProxy", 2, "interfaces", "invalid type [" + Caster.toClassName(oInterfaces) + "] for class defintion");
    // check if all classes are interfaces
    for (int i = 0; i < interfaces.length; i++) {
        if (!interfaces[i].isInterface())
            throw new FunctionException(pc, "CreateDynamicProxy", 2, "interfaces", "definition [" + interfaces[i].getClass() + "] is a class and not a interface");
    }
    return JavaProxyFactory.createProxy(pc, cfc, null, interfaces);
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) JavaObject(lucee.runtime.java.JavaObject) FunctionException(lucee.runtime.exp.FunctionException) JavaObject(lucee.runtime.java.JavaObject) PageContextImpl(lucee.runtime.PageContextImpl) Component(lucee.runtime.Component)

Example 4 with JavaObject

use of lucee.runtime.java.JavaObject in project Lucee by lucee.

the class ForEachUtil method _toIterator.

private static Iterator _toIterator(Object o) {
    if (o instanceof Iteratorable) {
        return ((Iteratorable) o).keysAsStringIterator();
    }
    if (o instanceof Iterator) {
        return (Iterator) o;
    }
    if (o instanceof Enumeration) {
        return new EnumAsIt((Enumeration) o);
    }
    if (o instanceof JavaObject) {
        Collection coll = Caster.toCollection(((JavaObject) o).getEmbededObject(null), null);
        if (coll != null)
            return coll.getIterator();
        String[] names = ClassUtil.getFieldNames(((JavaObject) o).getClazz());
        return new ArrayIterator(names);
    } else if (o instanceof CharSequence) {
        return ListUtil.listToArray(o.toString(), ',').getIterator();
    }
    return null;
}
Also used : Enumeration(java.util.Enumeration) JavaObject(lucee.runtime.java.JavaObject) ForEachIteratorable(lucee.runtime.type.ForEachIteratorable) Iteratorable(lucee.runtime.type.Iteratorable) Iterator(java.util.Iterator) Collection(lucee.runtime.type.Collection) EnumAsIt(lucee.runtime.type.it.EnumAsIt)

Example 5 with JavaObject

use of lucee.runtime.java.JavaObject in project Lucee by lucee.

the class JSONConverter method _serialize.

/**
 * serialize a Object to his xml Format represenation
 * @param object Object to serialize
 * @param sb StringBuilder to write data
 * @param serializeQueryByColumns
 * @param done
 * @throws ConverterException
 */
private void _serialize(PageContext pc, Set test, Object object, StringBuilder sb, boolean serializeQueryByColumns, Set done) throws ConverterException {
    // NULL
    if (object == null || object == NULL) {
        sb.append(goIn());
        sb.append("null");
        return;
    }
    // String
    if (object instanceof String || object instanceof StringBuilder) {
        sb.append(goIn());
        sb.append(StringUtil.escapeJS(object.toString(), '"', charsetEncoder));
        return;
    }
    // Character
    if (object instanceof Character) {
        sb.append(goIn());
        sb.append(StringUtil.escapeJS(String.valueOf(((Character) object).charValue()), '"', charsetEncoder));
        return;
    }
    // Number
    if (object instanceof Number) {
        sb.append(goIn());
        sb.append(Caster.toString(((Number) object)));
        return;
    }
    // Boolean
    if (object instanceof Boolean) {
        sb.append(goIn());
        sb.append(Caster.toString(((Boolean) object).booleanValue()));
        return;
    }
    // DateTime
    if (object instanceof DateTime) {
        _serializeDateTime((DateTime) object, sb);
        return;
    }
    // Date
    if (object instanceof Date) {
        _serializeDate((Date) object, sb);
        return;
    }
    // XML
    if (object instanceof Node) {
        _serializeXML((Node) object, sb);
        return;
    }
    // Timespan
    if (object instanceof TimeSpan) {
        _serializeTimeSpan((TimeSpan) object, sb);
        return;
    }
    // File
    if (object instanceof File) {
        _serialize(pc, test, ((File) object).getAbsolutePath(), sb, serializeQueryByColumns, done);
        return;
    }
    // String Converter
    if (object instanceof ScriptConvertable) {
        sb.append(((ScriptConvertable) object).serialize());
        return;
    }
    // byte[]
    if (object instanceof byte[]) {
        sb.append("\"" + Base64Coder.encode((byte[]) object) + "\"");
        return;
    }
    Object raw = LazyConverter.toRaw(object);
    if (done.contains(raw)) {
        sb.append(goIn());
        sb.append("null");
        return;
    }
    done.add(raw);
    try {
        // Component
        if (object instanceof Component) {
            _serializeComponent(pc, test, (Component) object, sb, serializeQueryByColumns, done);
            return;
        }
        // UDF
        if (object instanceof UDF) {
            _serializeUDF(pc, test, (UDF) object, sb, serializeQueryByColumns, done);
            return;
        }
        // Struct
        if (object instanceof Struct) {
            _serializeStruct(pc, test, (Struct) object, sb, serializeQueryByColumns, true, done);
            return;
        }
        // Map
        if (object instanceof Map) {
            _serializeMap(pc, test, (Map) object, sb, serializeQueryByColumns, done);
            return;
        }
        // Array
        if (object instanceof Array) {
            _serializeArray(pc, test, (Array) object, sb, serializeQueryByColumns, done);
            return;
        }
        // List
        if (object instanceof List) {
            _serializeList(pc, test, (List) object, sb, serializeQueryByColumns, done);
            return;
        }
        // Query
        if (object instanceof Query) {
            _serializeQuery(pc, test, (Query) object, sb, serializeQueryByColumns, done);
            return;
        }
        // Native Array
        if (Decision.isNativeArray(object)) {
            if (object instanceof char[])
                _serialize(pc, test, new String((char[]) object), sb, serializeQueryByColumns, done);
            else {
                _serializeArray(pc, test, ArrayUtil.toReferenceType(object, ArrayUtil.OBJECT_EMPTY), sb, serializeQueryByColumns, done);
            }
            return;
        }
        // ObjectWrap
        if (object instanceof ObjectWrap) {
            try {
                _serialize(pc, test, ((ObjectWrap) object).getEmbededObject(), sb, serializeQueryByColumns, done);
            } catch (PageException e) {
                if (object instanceof JavaObject) {
                    _serializeClass(pc, test, ((JavaObject) object).getClazz(), null, sb, serializeQueryByColumns, done);
                } else
                    throw new ConverterException("can't serialize Object of type [ " + Caster.toClassName(object) + " ]");
            }
            return;
        }
        _serializeClass(pc, test, object.getClass(), object, sb, serializeQueryByColumns, done);
    } finally {
        done.remove(raw);
    }
}
Also used : PageException(lucee.runtime.exp.PageException) ObjectWrap(lucee.runtime.type.ObjectWrap) Query(lucee.runtime.type.Query) Node(org.w3c.dom.Node) DateTime(lucee.runtime.type.dt.DateTime) Date(java.util.Date) Struct(lucee.runtime.type.Struct) TimeSpan(lucee.runtime.type.dt.TimeSpan) Array(lucee.runtime.type.Array) JavaObject(lucee.runtime.java.JavaObject) UDF(lucee.runtime.type.UDF) JavaObject(lucee.runtime.java.JavaObject) List(java.util.List) Component(lucee.runtime.Component) File(java.io.File) Map(java.util.Map)

Aggregations

JavaObject (lucee.runtime.java.JavaObject)5 Component (lucee.runtime.Component)2 ApplicationException (lucee.runtime.exp.ApplicationException)2 FunctionException (lucee.runtime.exp.FunctionException)2 ObjectWrap (lucee.runtime.type.ObjectWrap)2 Struct (lucee.runtime.type.Struct)2 File (java.io.File)1 Date (java.util.Date)1 Enumeration (java.util.Enumeration)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 PageContextImpl (lucee.runtime.PageContextImpl)1 PageException (lucee.runtime.exp.PageException)1 ConstructorInstance (lucee.runtime.reflection.pairs.ConstructorInstance)1 MethodInstance (lucee.runtime.reflection.pairs.MethodInstance)1 Array (lucee.runtime.type.Array)1 Collection (lucee.runtime.type.Collection)1 ForEachIteratorable (lucee.runtime.type.ForEachIteratorable)1 Iteratorable (lucee.runtime.type.Iteratorable)1