com.envimate.mapmate.reflections.DynamicMethod Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mapmate Show documentation
Show all versions of mapmate Show documentation
MapMate is a modern mapping framework in the scope of mapping data in
Json, XML, or YAML format into DTOs composed and vice versa.
/*
* Copyright (C) 2017 [Richard Hauswald, Nune Isabekyan] (envimate GmbH - https://envimate.com/)
*/
package com.envimate.mapmate.reflections;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
public final class DynamicMethod {
public final Class> type;
public final MethodHandle methodHandle;
private DynamicMethod(final Class> type, final MethodHandle methodHandle) {
this.type = type;
this.methodHandle = methodHandle;
}
public static DynamicMethod fromMethodName(final Class> type, final MethodName methodName, final MethodType methodType) {
try {
final MethodHandle methodHandle = MethodHandles.publicLookup()
.findVirtual(type, methodName.internalValueForMapping(), methodType);
return new DynamicMethod(type, methodHandle);
} catch (NoSuchMethodException | IllegalAccessException e) {
throw DynamicMethodNotFoundException.dynamicMethodNotFoundException(methodName, type, e);
}
}
public static DynamicMethod fromStaticMethodName(
final Class> type,
final MethodName methodName,
final MethodType methodType) {
try {
final MethodHandle methodHandle = MethodHandles.publicLookup()
.findStatic(type, methodName.internalValueForMapping(), methodType);
return new DynamicMethod(type, methodHandle);
} catch (NoSuchMethodException | IllegalAccessException e) {
throw DynamicMethodNotFoundException.dynamicMethodNotFoundException(methodName, type, e);
}
}
}