All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.envimate.mapmate.reflections.DynamicMethod Maven / Gradle / Ivy

Go to download

MapMate is a modern mapping framework in the scope of mapping data in Json, XML, or YAML format into DTOs composed and vice versa.

There is a newer version: 1.6.8
Show newest version
/*
 * 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);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy