Fix calling default methods on Event interfaces (#1386)

This commit is contained in:
Luck 2019-01-16 14:43:55 +00:00
parent 3c0d1ba7a1
commit 1d9fc568e4
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 66 additions and 2 deletions

View File

@ -32,6 +32,7 @@ import me.lucko.luckperms.api.event.LuckPermsEvent;
import me.lucko.luckperms.api.event.Param;
import me.lucko.luckperms.common.util.ImmutableCollectors;
import me.lucko.luckperms.common.util.LoadingMap;
import me.lucko.luckperms.common.util.PrivateMethodHandles;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationHandler;
@ -148,8 +149,7 @@ public class GeneratedEventSpec {
}
if (method.getDeclaringClass() == Object.class || method.isDefault()) {
return MethodHandles.lookup()
.in(method.getDeclaringClass())
return PrivateMethodHandles.privateLookup(method.getDeclaringClass())
.unreflectSpecial(method, method.getDeclaringClass())
.bindTo(proxy)
.invokeWithArguments(args);

View File

@ -0,0 +1,64 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.util;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public final class PrivateMethodHandles {
private PrivateMethodHandles() {}
private static final Constructor<MethodHandles.Lookup> LOOKUP_CONSTRUCTOR;
static {
try {
LOOKUP_CONSTRUCTOR = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, int.class);
LOOKUP_CONSTRUCTOR.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new ExceptionInInitializerError(e);
}
}
// MethodHandles.Lookup.PUBLIC | MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED | MethodHandles.Lookup.PACKAGE
private static final int TRUSTED_FLAG = -1;
/**
* Returns a {@link MethodHandles.Lookup lookup object} with full capabilities to emulate all
* supported bytecode behaviors, including private access, on a target class.
*
* @param targetClass the target class
* @return a lookup object for the target class, with private access
*/
public static MethodHandles.Lookup privateLookup(Class<?> targetClass) {
try {
return LOOKUP_CONSTRUCTOR.newInstance(targetClass, TRUSTED_FLAG);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}