1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-08-28 16:22:18 +00:00

Prevent reflection errors crashing the game

.getMethods() may throw if a method references classes which don't exist
(such as client-only classes on a server). This is an Error, and so is
unchecked - hence us not handling it before.

Fixes #645
This commit is contained in:
Jonathan Coates 2021-01-06 18:21:03 +00:00 committed by Jummit
parent 95794fdaf3
commit eb2d9482a2

View File

@ -64,9 +64,9 @@ public final class Generator<T> {
private final Function<T, T> wrap;
private final LoadingCache<Method, Optional<T>> methodCache = CacheBuilder.newBuilder()
.build(CacheLoader.from(this::build));
.build(CacheLoader.from(catching(this::build, Optional.empty())));
private final LoadingCache<Class<?>, List<NamedMethod<T>>> classCache = CacheBuilder.newBuilder()
.build(CacheLoader.from(this::build));
.build(CacheLoader.from(catching(this::build, Collections.emptyList())));
Generator(Class<T> base, List<Class<?>> context, Function<T, T> wrap) {
this.base = base;
@ -374,4 +374,22 @@ public final class Generator<T> {
method.getName());
return null;
}
@SuppressWarnings( "Guava" )
private static <T, U> com.google.common.base.Function<T, U> catching( Function<T, U> function, U def )
{
return x -> {
try
{
return function.apply( x );
}
catch( Exception | LinkageError e )
{
// LinkageError due to possible codegen bugs and NoClassDefFoundError. The latter occurs when fetching
// methods on a class which references non-existent (i.e. client-only) types.
ComputerCraft.log.error( "Error generating @LuaFunctions", e );
return def;
}
};
}
}