Skip to content

Commit 1eb61de

Browse files
committed
Unwrap CREs from the reflection exception
1 parent b846e13 commit 1eb61de

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

src/main/java/com/laytonsmith/core/functions/Function.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.laytonsmith.core.exceptions.ConfigCompileException;
1919
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
2020
import com.laytonsmith.core.natives.interfaces.Mixed;
21+
import java.lang.reflect.InvocationTargetException;
2122
import java.util.Collections;
2223
import java.util.List;
2324
import java.util.Set;
@@ -248,17 +249,31 @@ public interface CodeBranch {
248249
*/
249250
static Mixed ExecuteFunction(Function f, Target t, Environment env, Mixed[] args) throws ConfigRuntimeException {
250251
if(OLD_EXEC_CACHE.contains(f.getClass())) {
251-
return ReflectionUtils.invokeMethod(f.getClass(), f, "exec",
252-
new Class[]{Target.class, Environment.class, Mixed[].class},
253-
new Object[]{t, env, args});
252+
return invokeOldExec(f, t, env, args);
254253
}
255254
try {
256255
return f.exec(t, env, null, args);
257256
} catch(AbstractMethodError e) {
258257
OLD_EXEC_CACHE.add(f.getClass());
258+
return invokeOldExec(f, t, env, args);
259+
}
260+
}
261+
262+
private static Mixed invokeOldExec(Function f, Target t, Environment env, Mixed[] args)
263+
throws ConfigRuntimeException {
264+
try {
259265
return ReflectionUtils.invokeMethod(f.getClass(), f, "exec",
260266
new Class[]{Target.class, Environment.class, Mixed[].class},
261267
new Object[]{t, env, args});
268+
} catch(ReflectionUtils.ReflectionException e) {
269+
Throwable cause = e.getCause();
270+
if(cause instanceof InvocationTargetException) {
271+
cause = cause.getCause();
272+
}
273+
if(cause instanceof ConfigRuntimeException) {
274+
throw (ConfigRuntimeException) cause;
275+
}
276+
throw e;
262277
}
263278
}
264279

@@ -269,17 +284,30 @@ static Mixed ExecuteFunction(Function f, Target t, Environment env, Mixed[] args
269284

270285
static String ExecuteProfileMessage(Function f, Environment env, Mixed[] args) {
271286
if(OLD_PROFILE_MESSAGE_CACHE.contains(f.getClass())) {
272-
return (String) ReflectionUtils.invokeMethod(f.getClass(), f, "profileMessage",
273-
new Class[]{Mixed[].class},
274-
new Object[]{args});
287+
return invokeOldProfileMessage(f, args);
275288
}
276289
try {
277290
return f.profileMessage(env, args);
278291
} catch(AbstractMethodError e) {
279292
OLD_PROFILE_MESSAGE_CACHE.add(f.getClass());
293+
return invokeOldProfileMessage(f, args);
294+
}
295+
}
296+
297+
private static String invokeOldProfileMessage(Function f, Mixed[] args) {
298+
try {
280299
return (String) ReflectionUtils.invokeMethod(f.getClass(), f, "profileMessage",
281300
new Class[]{Mixed[].class},
282301
new Object[]{args});
302+
} catch(ReflectionUtils.ReflectionException e) {
303+
Throwable cause = e.getCause();
304+
if(cause instanceof InvocationTargetException) {
305+
cause = cause.getCause();
306+
}
307+
if(cause instanceof ConfigRuntimeException) {
308+
throw (ConfigRuntimeException) cause;
309+
}
310+
throw e;
283311
}
284312
}
285313
}

0 commit comments

Comments
 (0)