Skip to content

Commit

Permalink
bypass MethodInstance creation
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Dec 17, 2024
1 parent 2f84ac1 commit 79038e6
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/lucee/runtime/reflection/Reflector.java
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ public static Object callMethod(Object obj, String methodName, Object[] args, bo
}

public static Object callMethod(final Object obj, Collection.Key methodName, Object[] args, boolean nameCaseSensitive) throws PageException {
return getMethodInstance(obj.getClass(), methodName, args, nameCaseSensitive, false).invoke(obj);
return MethodInstance.invoke(obj, methodName, args, nameCaseSensitive, true);
}

public static boolean hasMethod(Class<?> clazz, String methodName, Object[] args, boolean nameCaseSensitive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,35 @@ public Object invoke(Object o) throws PageException {
}
}

public static Object invoke(Object obj, Key methodName, Object[] args, boolean nameCaseSensitive, boolean convertComparsion) throws PageException {
// if (Clazz.allowReflection()) print.e(Clazz.allowReflection());
try {
return ((BiFunction<Object, Object, Object>) DynamicInvoker.getExistingInstance().getInstance(obj.getClass(), methodName, args, nameCaseSensitive, convertComparsion)
.getValue()).apply(obj, args);
}
catch (IncompatibleClassChangeError | ClassFormatError | ClassCastException e) { // java.lang.ClassCastException
if (!Clazz.allowReflection()) throw e;
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);
return ((LegacyMethod) method).getMethod().invoke(obj, args);
}
catch (Exception e1) {
if (e1 instanceof InvocationTargetException) {
Throwable t = ((InvocationTargetException) e1).getTargetException();
ExceptionUtil.initCauseEL(e, t);
throw e;
}
ExceptionUtil.initCauseEL(e, e1);
throw e;
}
}
catch (Exception e) {
throw Caster.toPageException(e);
}
}

/**
* @return Returns the args.
*/
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/lucee/runtime/util/VariableUtilImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import lucee.runtime.op.Caster;
import lucee.runtime.op.Decision;
import lucee.runtime.reflection.Reflector;
import lucee.runtime.reflection.pairs.MethodInstance;
import lucee.runtime.security.SecurityManager;
import lucee.runtime.text.xml.XMLUtil;
import lucee.runtime.text.xml.struct.XMLStructFactory;
Expand Down Expand Up @@ -838,8 +839,9 @@ public Object callFunctionWithoutNamedValues(PageContext pc, Object coll, Collec
if (pc.getConfig().getSecurityManager().getAccess(SecurityManager.TYPE_DIRECT_JAVA_ACCESS) == SecurityManager.VALUE_YES) {
if (doLogReflectionCalls()) LogUtil.log(pc, Log.LEVEL_INFO, "reflection", "call-method:" + key + " from class " + Caster.toTypeName(coll));
if (!(coll instanceof Undefined)) {
return Reflector.getMethodInstance(coll.getClass(), key, args, false, false).invoke(coll);
// return Reflector.callMethod(coll, key, args);
// return Reflector.getMethodInstance(coll.getClass(), key, args, false, false).invoke(coll);
return MethodInstance.invoke(coll, key, args, false, true);
// return Reflector.callMethod(coll, key, args, false);
}
}
throw new ExpressionException("No matching Method/Function for " + key + "(" + Reflector.getDspMethods(Reflector.getClasses(args)) + ")");
Expand Down
11 changes: 6 additions & 5 deletions core/src/main/java/lucee/transformer/dynamic/DynamicInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public Pair<FunctionMember, Object> createInstance2(Class<?> clazz, Key methodNa
return null;
}

public static void main(String[] args) throws Throwable {
public static void main(String[] argsw) throws Throwable {
System.setProperty("lucee.allow.reflection", "false");
Resource classes = ResourcesImpl.getFileResourceProvider().getResource("/Users/mic/tmp8/classes/");
ResourceUtil.deleteContent(classes, null);
Expand All @@ -485,6 +485,7 @@ public static void main(String[] args) throws Throwable {
TestMule tm = new TestMule();
Class<? extends TestMule> clazz = tm.getClass();
Class[] cargs = new Class[] { int.class };
Object[] args = new Object[] { 1 };
Key methodName = new KeyImpl("Test");
e.invokeConstructor(clazz, new Object[] { 1 }, false);
e.invokeConstructor(String.class, new Object[] { "" }, false);
Expand All @@ -493,7 +494,7 @@ public static void main(String[] args) throws Throwable {
for (int i = 0; i < rounds; i++) {
long start = System.currentTimeMillis();
for (int y = 0; y < max; y++) {
clazz.getMethod("test", cargs).invoke(tm, new Object[] { 1 });
clazz.getMethod("test", cargs).invoke(tm, args);
}
tmp = System.currentTimeMillis() - start;
if (tmp < reflection) reflection = tmp;
Expand All @@ -503,7 +504,7 @@ public static void main(String[] args) throws Throwable {
for (int i = 0; i < rounds; i++) {
long start = System.currentTimeMillis();
for (int y = 0; y < max; y++) {
e.invokeInstanceMethod(tm, "test", new Object[] { 1 }, false, false);
e.invokeInstanceMethod(tm, methodName, args, false, false);
}
tmp = System.currentTimeMillis() - start;
if (tmp < dynamicInvoker) dynamicInvoker = tmp;
Expand All @@ -514,7 +515,7 @@ public static void main(String[] args) throws Throwable {
long start = System.currentTimeMillis();
for (int y = 0; y < max; y++) {
// Reflector.getMethodInstance(clazz, methodName, new Object[] { 1 }, false, false).invoke(tm);
Reflector.getMethod(clazz, "test", cargs, true).invoke(tm, new Object[] { 1 });
Reflector.getMethod(clazz, "test", cargs, true).invoke(tm, args);
}
tmp = System.currentTimeMillis() - start;
if (tmp < dynamicInvoker2) dynamicInvoker2 = tmp;
Expand All @@ -524,7 +525,7 @@ public static void main(String[] args) throws Throwable {
for (int i = 0; i < rounds; i++) {
long start = System.currentTimeMillis();
for (int y = 0; y < max; y++) {
Reflector.callMethod(tm, methodName, new Object[] { 1 }, false);
Reflector.callMethod(tm, methodName, args, false);
}
tmp = System.currentTimeMillis() - start;
if (tmp < dynamicInvoker3) dynamicInvoker3 = tmp;
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.2.0.228-SNAPSHOT"/>
<property name="version" value="6.2.0.229-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.228-SNAPSHOT</version>
<version>6.2.0.229-SNAPSHOT</version>
<packaging>jar</packaging>

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

0 comments on commit 79038e6

Please sign in to comment.