Skip to content

Commit

Permalink
LDEV-5075 - add support for new operator loads java
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Aug 20, 2024
1 parent fe35e10 commit 132f8b2
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 28 deletions.
54 changes: 38 additions & 16 deletions core/src/main/java/lucee/runtime/component/ComponentLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,22 @@ public class ComponentLoader {
*/
public static ComponentImpl searchComponent(PageContext pc, PageSource loadingLocation, String rawPath, Boolean searchLocal, Boolean searchRoot, boolean isExtendedComponent)
throws PageException {
return (ComponentImpl) _search(pc, loadingLocation, rawPath, searchLocal, searchRoot, true, RETURN_TYPE_COMPONENT, isExtendedComponent, true);
return (ComponentImpl) _search(pc, loadingLocation, rawPath, searchLocal, searchRoot, true, RETURN_TYPE_COMPONENT, isExtendedComponent, true, true);
}

public static ComponentImpl searchComponent(PageContext pc, PageSource loadingLocation, String rawPath, Boolean searchLocal, Boolean searchRoot,
final boolean isExtendedComponent, boolean executeConstr) throws PageException {
return (ComponentImpl) _search(pc, loadingLocation, rawPath, searchLocal, searchRoot, executeConstr, RETURN_TYPE_COMPONENT, isExtendedComponent, true);
public static ComponentImpl searchComponent(PageContext pc, PageSource loadingLocation, String rawPath, Boolean searchLocal, Boolean searchRoot, boolean isExtendedComponent,
boolean executeConstr) throws PageException {
return (ComponentImpl) _search(pc, loadingLocation, rawPath, searchLocal, searchRoot, executeConstr, RETURN_TYPE_COMPONENT, isExtendedComponent, true, true);
}

public static ComponentImpl searchComponent(PageContext pc, PageSource loadingLocation, String rawPath, Boolean searchLocal, Boolean searchRoot,
final boolean isExtendedComponent, boolean executeConstr, boolean validate) throws PageException {
return (ComponentImpl) _search(pc, loadingLocation, rawPath, searchLocal, searchRoot, executeConstr, RETURN_TYPE_COMPONENT, isExtendedComponent, validate);
public static ComponentImpl searchComponent(PageContext pc, PageSource loadingLocation, String rawPath, Boolean searchLocal, Boolean searchRoot, boolean isExtendedComponent,
boolean executeConstr, boolean validate) throws PageException {
return (ComponentImpl) _search(pc, loadingLocation, rawPath, searchLocal, searchRoot, executeConstr, RETURN_TYPE_COMPONENT, isExtendedComponent, validate, true);
}

public static ComponentImpl searchComponent(PageContext pc, PageSource loadingLocation, String rawPath, Boolean searchLocal, Boolean searchRoot, boolean isExtendedComponent,
boolean executeConstr, boolean validate, boolean throwOnMissing) throws PageException {
return (ComponentImpl) _search(pc, loadingLocation, rawPath, searchLocal, searchRoot, executeConstr, RETURN_TYPE_COMPONENT, isExtendedComponent, validate, throwOnMissing);
}

public static StaticScope getStaticScope(PageContext pc, PageSource loadingLocation, String rawPath, Boolean searchLocal, Boolean searchRoot) throws PageException {
Expand Down Expand Up @@ -138,39 +143,54 @@ public static StaticScope getStaticScope(PageContext pc, PageSource loadingLocat
}

public static ComponentPageImpl searchComponentPage(PageContext pc, PageSource loadingLocation, String rawPath, Boolean searchLocal, Boolean searchRoot) throws PageException {
return searchComponentPage(pc, loadingLocation, rawPath, searchLocal, searchRoot, true);
return searchComponentPage(pc, loadingLocation, rawPath, searchLocal, searchRoot, true, true);
}

public static ComponentPageImpl searchComponentPage(PageContext pc, PageSource loadingLocation, String rawPath, Boolean searchLocal, Boolean searchRoot, boolean validate)
throws PageException {
Object obj = _search(pc, loadingLocation, rawPath, searchLocal, searchRoot, false, RETURN_TYPE_PAGE, false, validate);
return searchComponentPage(pc, loadingLocation, rawPath, searchLocal, searchRoot, validate, true);
}

public static ComponentPageImpl searchComponentPage(PageContext pc, PageSource loadingLocation, String rawPath, Boolean searchLocal, Boolean searchRoot, boolean validate,
boolean throwOnMissing) throws PageException {
Object obj = _search(pc, loadingLocation, rawPath, searchLocal, searchRoot, false, RETURN_TYPE_PAGE, false, validate, throwOnMissing);

if (obj instanceof ComponentPageImpl) return (ComponentPageImpl) obj;
throw new ExpressionException("invalid " + toStringType(RETURN_TYPE_PAGE) + " definition, can't find " + toStringType(RETURN_TYPE_PAGE) + " [" + rawPath + "]");
}

public static InterfaceImpl searchInterface(PageContext pc, PageSource loadingLocation, String rawPath) throws PageException {
return (InterfaceImpl) _search(pc, loadingLocation, rawPath, Boolean.TRUE, Boolean.TRUE, true, RETURN_TYPE_INTERFACE, false, true);
return (InterfaceImpl) _search(pc, loadingLocation, rawPath, Boolean.TRUE, Boolean.TRUE, true, RETURN_TYPE_INTERFACE, false, true, true);
}

public static InterfaceImpl searchInterface(PageContext pc, PageSource loadingLocation, String rawPath, boolean executeConstr) throws PageException {
return (InterfaceImpl) _search(pc, loadingLocation, rawPath, Boolean.TRUE, Boolean.TRUE, executeConstr, RETURN_TYPE_INTERFACE, false, true);
return (InterfaceImpl) _search(pc, loadingLocation, rawPath, Boolean.TRUE, Boolean.TRUE, executeConstr, RETURN_TYPE_INTERFACE, false, true, true);
}

public static InterfaceImpl searchInterface(PageContext pc, PageSource loadingLocation, String rawPath, boolean executeConstr, boolean validate) throws PageException {
return (InterfaceImpl) _search(pc, loadingLocation, rawPath, Boolean.TRUE, Boolean.TRUE, executeConstr, RETURN_TYPE_INTERFACE, false, validate);
return (InterfaceImpl) _search(pc, loadingLocation, rawPath, Boolean.TRUE, Boolean.TRUE, executeConstr, RETURN_TYPE_INTERFACE, false, validate, true);
}

public static InterfaceImpl searchInterface(PageContext pc, PageSource loadingLocation, String rawPath, boolean executeConstr, boolean validate, boolean throwOnMissing)
throws PageException {
return (InterfaceImpl) _search(pc, loadingLocation, rawPath, Boolean.TRUE, Boolean.TRUE, executeConstr, RETURN_TYPE_INTERFACE, false, validate, throwOnMissing);
}

public static Page searchPage(PageContext pc, PageSource child, String rawPath, Boolean searchLocal, Boolean searchRoot) throws PageException {
return (Page) _search(pc, child, rawPath, searchLocal, searchRoot, false, RETURN_TYPE_PAGE, false, true);
return (Page) _search(pc, child, rawPath, searchLocal, searchRoot, false, RETURN_TYPE_PAGE, false, true, true);
}

public static Page searchPage(PageContext pc, PageSource child, String rawPath, Boolean searchLocal, Boolean searchRoot, boolean validate) throws PageException {
return (Page) _search(pc, child, rawPath, searchLocal, searchRoot, false, RETURN_TYPE_PAGE, false, validate);
return (Page) _search(pc, child, rawPath, searchLocal, searchRoot, false, RETURN_TYPE_PAGE, false, validate, true);
}

public static Page searchPage(PageContext pc, PageSource child, String rawPath, Boolean searchLocal, Boolean searchRoot, boolean validate, boolean throwOnMissing)
throws PageException {
return (Page) _search(pc, child, rawPath, searchLocal, searchRoot, false, RETURN_TYPE_PAGE, false, validate, throwOnMissing);
}

private static Object _search(PageContext pc, PageSource loadingLocation, String rawPath, Boolean searchLocal, Boolean searchRoot, boolean executeConstr, short returnType,
final boolean isExtendedComponent, boolean validate) throws PageException {
final boolean isExtendedComponent, boolean validate, boolean throwOnMissing) throws PageException {
PageSource currPS = pc.getCurrentPageSource(null);

ImportDefintion[] importDefintions = null;
Expand All @@ -187,7 +207,9 @@ else if ((currP = currPS.loadPage(pc, false, null)) != null) {
// first try for the current dialect
Object obj = _search(pc, loadingLocation, rawPath, searchLocal, searchRoot, executeConstr, returnType, currPS, importDefintions, isExtendedComponent, validate);

if (obj == null) throw new ExpressionException("invalid " + toStringType(returnType) + " definition, can't find " + toStringType(returnType) + " [" + rawPath + "]");
if (obj == null && throwOnMissing) {
throw new ExpressionException("invalid " + toStringType(returnType) + " definition, can't find " + toStringType(returnType) + " [" + rawPath + "]");
}
return obj;
}

Expand Down
101 changes: 91 additions & 10 deletions core/src/main/java/lucee/runtime/functions/other/_CreateComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@
*/
package lucee.runtime.functions.other;

import lucee.commons.lang.ClassException;
import lucee.commons.lang.ClassUtil;
import lucee.commons.lang.ExceptionUtil;
import lucee.runtime.Component;
import lucee.runtime.ComponentImpl;
import lucee.runtime.Page;
import lucee.runtime.PageContext;
import lucee.runtime.PageSource;
import lucee.runtime.component.ComponentLoader;
import lucee.runtime.component.ImportDefintion;
import lucee.runtime.component.ImportDefintionImpl;
import lucee.runtime.exp.ApplicationException;
import lucee.runtime.exp.ExpressionException;
import lucee.runtime.exp.PageException;
import lucee.runtime.functions.orm.EntityNew;
Expand All @@ -33,41 +43,83 @@
public class _CreateComponent {

private static final Object[] EMPTY = new Object[0];
private static final ImportDefintion JAVA_LANG = new ImportDefintionImpl("java.lang", "*");
private static ImportDefintion[] EMPTY_ID = new ImportDefintion[0];

public static Object call(PageContext pc, Object[] objArr) throws PageException {
String path = Caster.toString(objArr[objArr.length - 1]);
// not store the index to make it faster
Component cfc = ComponentLoader.searchComponent(pc, null, path, null, null, false, true, true, false);
Class cls = null;
if (cfc == null) {
// no package
if (path.indexOf('.') == -1) {
ImportDefintion[] imports = getImportDefintions(pc);
ImportDefintion id;
for (int i = 0; i <= imports.length; i++) {
id = i == imports.length ? JAVA_LANG : imports[i];
if ("*".equals(id.getName()) || path.equals(id.getName())) {
try {// TODO do method with defaultValue
cls = ClassUtil.loadClass(pc, id.getPackage() + "." + path);
break;
}
catch (Exception e) {

Component c = CreateObject.doComponent(pc, path);
}
}
}

}
if (cls == null) {
try {
cls = ClassUtil.loadClass(pc, path);
}
catch (Exception e) {
ApplicationException ae = new ApplicationException("could not find component or class with name [" + path + "]");
ExceptionUtil.initCauseEL(ae, e);
throw ae;
}
}

}

// no init method
if (!(c.get(pc, KeyConstants._init, null) instanceof UDF)) {
if (cfc != null && !(cfc.get(pc, KeyConstants._init, null) instanceof UDF)) {

if (objArr.length > 1) { // we have arguments passed in
Object arg1 = objArr[0];
if (arg1 instanceof FunctionValue) {
Struct args = Caster.toFunctionValues(objArr, 0, objArr.length - 1);
EntityNew.setPropeties(pc, c, args, true);
EntityNew.setPropeties(pc, cfc, args, true);
}
else if (Decision.isStruct(arg1) && !Decision.isComponent(arg1) && objArr.length == 2) { // we only do this in case there is only argument set, otherwise we assume
// that this is simply a missuse of the new operator
Struct args = Caster.toStruct(arg1);
EntityNew.setPropeties(pc, c, args, true);
EntityNew.setPropeties(pc, cfc, args, true);
}
}

return c;
return cfc;
}

Object rtn;
// no arguments
if (objArr.length == 1) {// no args
rtn = c.call(pc, KeyConstants._init, EMPTY);
if (objArr.length == 1) {
if (cfc != null) rtn = cfc.call(pc, KeyConstants._init, EMPTY);
else {
try {
rtn = ClassUtil.loadInstance(cls);
}
catch (ClassException e) {
throw Caster.toPageException(e);
}
}
}
// named arguments
else if (objArr[0] instanceof FunctionValue) {
if (cfc == null) throw new ApplicationException("named arguments are not supported with classes.");
Struct args = Caster.toFunctionValues(objArr, 0, objArr.length - 1);
rtn = c.callWithNamedValues(pc, KeyConstants._init, args);
rtn = cfc.callWithNamedValues(pc, KeyConstants._init, args);
}
// no name arguments
else {
Expand All @@ -77,11 +129,40 @@ else if (objArr[0] instanceof FunctionValue) {
if (args[i] instanceof FunctionValue)
throw new ExpressionException("invalid argument definition, when using named parameters to a function, every parameter must have a name.");
}
rtn = c.call(pc, KeyConstants._init, args);
if (cfc != null) rtn = cfc.call(pc, KeyConstants._init, args);
else {
try {
rtn = ClassUtil.loadInstance(cls, args);
}
catch (Exception e) {
throw Caster.toPageException(e);
}
}

}
if (rtn == null && cfc != null) {
return cfc;
}
if (rtn == null) return c;

return rtn;
}

private static ImportDefintion[] getImportDefintions(PageContext pc) {
PageSource currPS = pc.getCurrentPageSource(null);

ImportDefintion[] importDefintions = null;
if (currPS != null) {
Page currP;
Component cfc = pc.getActiveComponent();
if (cfc instanceof ComponentImpl && currPS.equals(cfc.getPageSource())) {
importDefintions = ((ComponentImpl) cfc)._getImportDefintions();
}
else if ((currP = currPS.loadPage(pc, false, null)) != null) {
importDefintions = currP.getImportDefintions();
}
}
if (importDefintions == null) return EMPTY_ID;
return importDefintions;
}

}
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.2.0.51-SNAPSHOT"/>
<property name="version" value="6.2.0.52-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.2.0.51-SNAPSHOT</version>
<version>6.2.0.52-SNAPSHOT</version>
<packaging>jar</packaging>

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

0 comments on commit 132f8b2

Please sign in to comment.