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));
}
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");
}
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);
}
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;
}
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);
}
}
Aggregations