From 77840a5a8dc0203571d4386e2b922bd41775ce76 Mon Sep 17 00:00:00 2001 From: michaeloffner Date: Mon, 1 Jul 2024 17:22:36 +0200 Subject: [PATCH] LDEV-4975 - allow class as second argument --- .../runtime/functions/other/CreateObject.java | 52 ++++++++++++++----- loader/build.xml | 2 +- loader/pom.xml | 2 +- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/lucee/runtime/functions/other/CreateObject.java b/core/src/main/java/lucee/runtime/functions/other/CreateObject.java index 741debeb38..06dab32b2a 100755 --- a/core/src/main/java/lucee/runtime/functions/other/CreateObject.java +++ b/core/src/main/java/lucee/runtime/functions/other/CreateObject.java @@ -29,10 +29,12 @@ import lucee.runtime.config.ConfigWebPro; import lucee.runtime.engine.ThreadLocalPageContext; import lucee.runtime.exp.ExpressionException; +import lucee.runtime.exp.FunctionException; import lucee.runtime.exp.FunctionNotSupported; import lucee.runtime.exp.PageException; import lucee.runtime.exp.SecurityException; -import lucee.runtime.ext.function.Function; +import lucee.runtime.ext.function.BIF; +import lucee.runtime.java.JavaObject; import lucee.runtime.net.http.HTTPClient; import lucee.runtime.net.proxy.ProxyData; import lucee.runtime.net.proxy.ProxyDataImpl; @@ -40,34 +42,52 @@ import lucee.runtime.security.SecurityManager; import lucee.runtime.type.Struct; -public final class CreateObject implements Function { +public final class CreateObject extends BIF { + + private static final long serialVersionUID = -3975902435778397677L; + + @Override + public Object invoke(PageContext pc, Object[] args) throws PageException { + if (args.length < 1 || args.length > 4) throw new FunctionException(pc, "CreateObject", 1, 4, args.length); + + // type + String type = Caster.toString(args[0], null); + if (StringUtil.isEmpty(type)) throw new FunctionException(pc, "CreateObject", 1, "type", "must be (com, java, webservice or component) other types are not supported"); + + return call(pc, type, args.length > 1 ? args[1] : null, args.length > 2 ? args[2] : null, args.length > 3 ? args[3] : null); + } + public static Object call(PageContext pc, String cfcName) throws PageException { return call(pc, "component", cfcName, null, null); } - public static Object call(PageContext pc, String type, String className) throws PageException { - return call(pc, type, className, null, null); + public static Object call(PageContext pc, String type, Object objClass) throws PageException { + return call(pc, type, objClass, null, null); } - public static Object call(PageContext pc, String type, String className, Object context) throws PageException { - return call(pc, type, className, context, null); + public static Object call(PageContext pc, String type, Object objClass, Object context) throws PageException { + return call(pc, type, objClass, context, null); } - public static Object call(PageContext pc, String type, String className, Object context, Object serverName) throws PageException { + public static Object call(PageContext pc, String type, Object objClass, Object context, Object serverName) throws PageException { type = StringUtil.toLowerCase(type); // JAVA if (type.equals("java")) { checkAccess(pc, type); - return doJava(pc, className, context, Caster.toString(serverName)); + if (objClass instanceof Class) { + return new JavaObject((pc).getVariableUtil(), (Class) objClass); + } + + return doJava(pc, toClassName(pc, objClass), context, Caster.toString(serverName)); } // COM if (type.equals("com")) { - return doCOM(pc, className); + return doCOM(pc, toClassName(pc, objClass)); } // Component if (type.equals("component") || type.equals("cfc")) { - return doComponent(pc, className); + return doComponent(pc, toClassName(pc, objClass)); } // Webservice if (type.equals("webservice") || type.equals("wsdl")) { @@ -92,7 +112,7 @@ public static Object call(PageContext pc, String type, String className, Object } } - return doWebService(pc, className, user, pass, proxy); + return doWebService(pc, toClassName(pc, objClass), user, pass, proxy); } if (type.equals("http")) { String user = null; @@ -116,17 +136,23 @@ public static Object call(PageContext pc, String type, String className, Object } } - return doHTTP(pc, className, user, pass, proxy); + return doHTTP(pc, toClassName(pc, objClass), user, pass, proxy); } // .net if (type.equals(".net") || type.equals("dotnet")) { - return doDotNet(pc, className); + return doDotNet(pc, toClassName(pc, objClass)); } throw new ExpressionException( "Invalid argument for function createObject, first argument (type), " + "must be (com, java, webservice or component) other types are not supported"); } + private static String toClassName(PageContext pc, Object objClass) throws FunctionException { + String str = Caster.toString(objClass, null); + if (StringUtil.isEmpty(str, true)) throw new FunctionException(pc, "CreateObject", 2, "className", "value must be a string"); + return str; + } + private static Object doDotNet(PageContext pc, String className) throws FunctionNotSupported { throw new FunctionNotSupported("CreateObject", "type .net"); } diff --git a/loader/build.xml b/loader/build.xml index 42d7322415..aee7e3393b 100644 --- a/loader/build.xml +++ b/loader/build.xml @@ -2,7 +2,7 @@ - + diff --git a/loader/pom.xml b/loader/pom.xml index ae65a5d418..e830b6f506 100644 --- a/loader/pom.xml +++ b/loader/pom.xml @@ -3,7 +3,7 @@ org.lucee lucee - 6.1.1.13-SNAPSHOT + 6.1.1.14-SNAPSHOT jar Lucee Loader Build