Skip to content

Commit

Permalink
LDEV-4975 - allow class as second argument
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Jul 1, 2024
1 parent 2cff958 commit 77840a5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
52 changes: 39 additions & 13 deletions core/src/main/java/lucee/runtime/functions/other/CreateObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,45 +29,65 @@
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;
import lucee.runtime.op.Caster;
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")) {
Expand All @@ -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;
Expand All @@ -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");
}
Expand Down
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.1.1.13-SNAPSHOT"/>
<property name="version" value="6.1.1.14-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.1.1.13-SNAPSHOT</version>
<version>6.1.1.14-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit 77840a5

Please sign in to comment.