Skip to content

Commit

Permalink
fix default method for java 8+ (fixes #104, via #112)
Browse files Browse the repository at this point in the history
  • Loading branch information
bogachevdmitry authored May 31, 2023
1 parent 639708f commit b38decf
Showing 1 changed file with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

import io.qameta.atlas.core.api.MethodExtension;
import io.qameta.atlas.core.util.MethodInfo;
import org.apache.commons.lang3.JavaVersion;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost;

/**
* Default method extension.
*/
Expand All @@ -20,11 +25,28 @@ public boolean test(final Method method) {
@Override
public Object invoke(final Object proxy, final MethodInfo methodInfo, final Configuration config) throws Throwable {
final Class<?> declaringClass = methodInfo.getMethod().getDeclaringClass();
final Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class
.getDeclaredConstructor(Class.class, int.class);
constructor.setAccessible(true);
return constructor.newInstance(declaringClass, MethodHandles.Lookup.PRIVATE)
.unreflectSpecial(methodInfo.getMethod(), declaringClass)

if (isJavaVersionAtMost(JavaVersion.JAVA_1_8)) {
final Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class
.getDeclaredConstructor(Class.class, int.class);
constructor.setAccessible(true);
return constructor.newInstance(declaringClass, MethodHandles.Lookup.PRIVATE)
.unreflectSpecial(methodInfo.getMethod(), declaringClass)
.bindTo(proxy)
.invokeWithArguments(methodInfo.getArgs());
}

final MethodHandle methodHandle = MethodHandles.lookup().findSpecial(
declaringClass,
methodInfo.getMethod().getName(),
MethodType.methodType(
methodInfo.getMethod().getReturnType(),
methodInfo.getMethod().getParameterTypes()
),
declaringClass
);

return methodHandle
.bindTo(proxy)
.invokeWithArguments(methodInfo.getArgs());
}
Expand Down

0 comments on commit b38decf

Please sign in to comment.