Skip to content

Commit

Permalink
improve exception message when no method match
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Sep 4, 2024
1 parent 1db15ce commit f11f535
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public final class PhysicalClassLoader extends URLClassLoader implements Extenda
static {
boolean res = registerAsParallelCapable();
}

private static RC rc = new RC();

private static Map<String, PhysicalClassLoader> classLoaders = new ConcurrentHashMap<>();
Expand All @@ -82,6 +83,8 @@ public final class PhysicalClassLoader extends URLClassLoader implements Extenda

private boolean rpc;

private String birthplace;

private static long counter = 0L;
private static long _start = 0L;
private static String start = Long.toString(_start, Character.MAX_RADIX);
Expand Down Expand Up @@ -163,7 +166,7 @@ private PhysicalClassLoader(Config c, List<Resource> resources, Resource directo
this.resources = resources;
config = (ConfigPro) c;
this.addionalClassLoader = addionalClassLoader;

this.birthplace = ExceptionUtil.getStacktrace(new Throwable(), false);
this.pageSourcePool = pageSourcePool;
// ClassLoader resCL = parent!=null?parent:config.getResourceClassLoader(null);

Expand All @@ -175,6 +178,10 @@ private PhysicalClassLoader(Config c, List<Resource> resources, Resource directo
this.rpc = rpc;
}

public String getBirthplace() {
return birthplace;
}

public boolean isRPC() {
return rpc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public FunctionException(PageContext pc, String functionName, int badArgumentPos
this(pc, functionName, toStringBadArgumentPosition(badArgumentPosition), badArgumentName, message, detail);
}

private static String toStringBadArgumentPosition(int pos) {
public static String toStringBadArgumentPosition(int pos) {
switch (pos) {
case 1:
return "first";
Expand Down
48 changes: 45 additions & 3 deletions core/src/main/java/lucee/transformer/dynamic/meta/Clazz.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import lucee.commons.lang.ClassException;
import lucee.commons.lang.ClassUtil;
import lucee.commons.lang.Pair;
import lucee.commons.lang.PhysicalClassLoader;
import lucee.commons.lang.types.RefInteger;
import lucee.commons.lang.types.RefIntegerImpl;
import lucee.runtime.config.Constants;
import lucee.runtime.exp.FunctionException;
import lucee.runtime.exp.PageException;
import lucee.runtime.java.JavaObject;
import lucee.runtime.op.Caster;
Expand Down Expand Up @@ -212,13 +214,53 @@ public static Method getMethodMatch(Clazz clazz, final Collection.Key methodName
* (first,third,10th) and because this object have no constructor taking no arguments, Lucee cannot
* instantiate them. you need first to instantiate this objects.
*/

Class[] trgArgs = Reflector.getClasses(args);
String strTrgArgs = Reflector.getDspMethods(trgArgs);
StringBuilder msg = new StringBuilder();
msg.append("No matching method for ").append(lucee.runtime.type.util.Type.getName(clazz.getDeclaringClass())).append(".").append(methodName).append("(")
.append(Reflector.getDspMethods(Reflector.getClasses(args))).append(") found. ");
msg.append("No matching method for ").append(lucee.runtime.type.util.Type.getName(clazz.getDeclaringClass())).append(".").append(methodName).append("(").append(strTrgArgs)
.append(") found. ");
if (methods.size() > 0) {
msg.append("there are similar methods with the same name, but diferent arguments:\n ");
Class[] srcArgs;
String strSrcArgs;
for (Method m: methods) {
msg.append(methodName).append('(').append(Reflector.getDspMethods(m.getArgumentClasses())).append(");\n");
srcArgs = m.getArgumentClasses();
strSrcArgs = Reflector.getDspMethods(srcArgs);
if (strSrcArgs.equals(strTrgArgs)) {
ClassLoader srcClassLoader = null;
ClassLoader trgClassLoader = null;
int index = -1;
for (int i = 0; i < srcArgs.length; i++) {
if (srcArgs[i].getClassLoader() == trgArgs[i].getClassLoader()) continue;
index = i;
srcClassLoader = srcArgs[i].getClassLoader();
trgClassLoader = trgArgs[i].getClassLoader();
break;
}
String srcClassLoaderName = "Bootstrap ClassLoader";
if (srcClassLoader instanceof PhysicalClassLoader) {
srcClassLoaderName = "PhysicalClassLoader loaded at " + ((PhysicalClassLoader) srcClassLoader).getBirthplace();
}
else if (srcClassLoader != null) {
srcClassLoaderName = srcClassLoader.toString();
}
String trgClassLoaderName = "Bootstrap ClassLoader";
if (trgClassLoader instanceof PhysicalClassLoader) {
trgClassLoaderName = "PhysicalClassLoader loaded at " + ((PhysicalClassLoader) trgClassLoader).getBirthplace();
}
else if (trgClassLoader != null) {
trgClassLoaderName = srcClassLoader.toString();
}

if (index != -1) throw new NoSuchMethodException("Found a matching method for [" + lucee.runtime.type.util.Type.getName(clazz.getDeclaringClass()) + "("
+ strSrcArgs + ")], but the classes were loaded by different class loaders. \n" + "The " + FunctionException.toStringBadArgumentPosition(index + 1)
+ " argument [" + lucee.runtime.type.util.Type.getName(srcArgs[index]) + "] was loaded by the class loader [" + trgClassLoaderName
+ "], but the provided argument is from the class loader [" + srcClassLoaderName + "]."
+ " Ensure that both classes are loaded by the same class loader to avoid conflicts.");

}
msg.append(methodName).append('(').append(strSrcArgs).append(");\n");
}

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

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

0 comments on commit f11f535

Please sign in to comment.