Skip to content

Commit

Permalink
integrate method filtering directly into ClazzDynamic to avoid creati…
Browse files Browse the repository at this point in the history
…ng a List and make cache more local
  • Loading branch information
michaeloffner committed Dec 19, 2024
1 parent 79038e6 commit ff5e463
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 262 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/lucee/commons/lang/ClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1032,8 +1032,8 @@ public static ClassLoader getClassLoader(PageContext pc, Class clazz) throws IOE
return new lucee.commons.lang.ClassLoaderHelper().getClass().getClassLoader();
}

public static Object newInstance(Class clazz) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
SecurityException, PageException, IOException {
public static Object newInstance(Class clazz)
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, PageException {
return Reflector.getConstructorInstance(clazz, EMPTY_OBJ, true).invoke();
}
}
9 changes: 9 additions & 0 deletions core/src/main/java/lucee/runtime/reflection/Reflector.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,15 @@ public static boolean like(Class src, Class trg) {
return isInstaneOf(src, trg, true);
}

public static Object convert(Object src, Class trgClass, RefInteger rating, Object defaultValue) {
try {
return convert(src, trgClass, rating);
}
catch (PageException e) {// MUST handle this better
return defaultValue;
}
}

/**
* convert Object from src to trg Type, if possible
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
**/
package lucee.runtime.reflection.pairs;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.function.BiFunction;

Expand Down Expand Up @@ -57,8 +56,8 @@ public ConstructorInstance(Class clazz, Object[] args, boolean convertComparsion
this.convertComparsion = convertComparsion;
}

public Object invoke() throws PageException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
SecurityException, IOException {
public Object invoke()
throws PageException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {

try {
return ((BiFunction<Object, Object, Object>) getResult().getValue()).apply(null, args);
Expand All @@ -70,7 +69,7 @@ public Object invoke() throws PageException, InstantiationException, IllegalAcce
boolean failed = false;
try {
DynamicInvoker di = DynamicInvoker.getExistingInstance();
lucee.transformer.dynamic.meta.Constructor constr = Clazz.getConstructorMatch(di.getClazz(clazz, true), args, true, convertComparsion);
lucee.transformer.dynamic.meta.Constructor constr = di.getClazz(clazz, true).getConstructor(args, true, convertComparsion);
return ((LegacyConstuctor) constr).getConstructor().newInstance(args);
}
catch (IncompatibleClassChangeError | ClassFormatError | IllegalStateException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Object invoke(Object o) throws PageException {
LogUtil.log("dynamic", e);
DynamicInvoker di = DynamicInvoker.getExistingInstance();
try {
lucee.transformer.dynamic.meta.Method method = Clazz.getMethodMatch(di.getClazz(clazz, true), methodName, args, nameCaseSensitive, true, true);
lucee.transformer.dynamic.meta.Method method = di.getClazz(clazz, true).getMethod(methodName.getString(), args, nameCaseSensitive, true, true);
return ((LegacyMethod) method).getMethod().invoke(o, args);
}
catch (Exception e1) {
Expand All @@ -98,7 +98,7 @@ public static Object invoke(Object obj, Key methodName, Object[] args, boolean n
LogUtil.log("dynamic", e);
DynamicInvoker di = DynamicInvoker.getExistingInstance();
try {
lucee.transformer.dynamic.meta.Method method = Clazz.getMethodMatch(di.getClazz(obj.getClass(), true), methodName, args, nameCaseSensitive, true, true);
lucee.transformer.dynamic.meta.Method method = di.getClazz(obj.getClass(), true).getMethod(methodName.getString(), args, nameCaseSensitive, true, true);
return ((LegacyMethod) method).getMethod().invoke(obj, args);
}
catch (Exception e1) {
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/lucee/transformer/dynamic/DynamicInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import lucee.commons.lang.SerializableObject;
import lucee.commons.lang.SystemOut;
import lucee.loader.engine.CFMLEngineFactory;
import lucee.runtime.exp.PageException;
import lucee.runtime.op.Caster;
import lucee.runtime.reflection.Reflector;
import lucee.runtime.type.Collection.Key;
Expand Down Expand Up @@ -135,7 +134,7 @@ private Object invoke(Object objMaybeNull, Class<?> objClass, Key methodName, Ob
catch (IncompatibleClassChangeError | IllegalStateException e) {
if (log != null) log.error("dynamic", e);
if (!Clazz.allowReflection()) throw e;
lucee.transformer.dynamic.meta.Method method = Clazz.getMethodMatch(getClazz(objClass, true), methodName, arguments, nameCaseSensitive, true, convertComparsion);
lucee.transformer.dynamic.meta.Method method = getClazz(objClass, true).getMethod(methodName.getString(), arguments, nameCaseSensitive, true, convertComparsion);
return ((LegacyMethod) method).getMethod().invoke(objClass, arguments);
}
}
Expand All @@ -157,7 +156,7 @@ public Clazz getClazz(Class<?> clazz, boolean useReflection) {

public Pair<FunctionMember, Object> getInstance(Class<?> clazz, Key methodName, Object[] arguments, boolean nameCaseSensitive, boolean convertComparsion)
throws NoSuchMethodException, IOException, UnmodifiableClassException, InstantiationException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException, SecurityException, PageException {
InvocationTargetException, SecurityException {

// double start = SystemUtil.millis();
boolean isConstr = methodName == null;
Expand All @@ -167,8 +166,9 @@ public Pair<FunctionMember, Object> getInstance(Class<?> clazz, Key methodName,
// getClass -= start;
// start = SystemUtil.millis();
// getClass += start;
lucee.transformer.dynamic.meta.FunctionMember fm = isConstr ? Clazz.getConstructorMatch(clazzz, arguments, true, convertComparsion)
: Clazz.getMethodMatch(clazzz, methodName, arguments, nameCaseSensitive, true, convertComparsion);

lucee.transformer.dynamic.meta.FunctionMember fm = isConstr ? clazzz.getConstructor(arguments, true, convertComparsion)
: clazzz.getMethod(methodName.getString(), arguments, nameCaseSensitive, true, convertComparsion);

// match -= start;
// start = SystemUtil.millis();
Expand Down Expand Up @@ -736,7 +736,7 @@ public static void main(String[] argsw) throws Throwable {

aprint.e(e.invokeInstanceMethod(new SystemOut(), "setOut", new Object[] { null }, true, true));
System.setProperty("a.b.c", "- value -");
aprint.e(e.invokeInstanceMethod(sb, "toSTring", new Object[] {}, true, false));
aprint.e(e.invokeInstanceMethod(sb, "toSTring", new Object[] {}, false, false));
aprint.e(e.invokeStaticMethod(SystemUtil.class, "getSystemPropOrEnvVar", new Object[] { "a.b.c", "default-value" }, true, true));
aprint.e(e.invokeStaticMethod(ListUtil.class, "arrayToList", new Object[] { new String[] { "a", "b" }, "," }, true, true));
aprint.e("done");
Expand Down
Loading

0 comments on commit ff5e463

Please sign in to comment.