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

org.fiolino.common.util.Serializer Maven / Gradle / Ivy

Go to download

General structure to easily create dynamic logic via MethodHandles and others.

There is a newer version: 1.0.10
Show newest version
package org.fiolino.common.util;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;

import static java.lang.invoke.MethodHandles.publicLookup;
import static java.lang.invoke.MethodType.methodType;

/**
 * This class builds a MethodHandle that prints the content
 * Created by kuli on 21.03.17.
 */
public final class Serializer {
    private static final MethodHandle NEW_STRINGBUILDER, APPEND_OPENING_PARENTHESIS, APPEND_CLOSING_PARENTHESIS;

    static {
        MethodHandle appendChar;
        try {
            NEW_STRINGBUILDER = publicLookup().findConstructor(StringBuilder.class, methodType(void.class));
            appendChar = publicLookup().findVirtual(StringBuilder.class, "append", methodType(StringBuilder.class, char.class))
                    .asType(methodType(void.class, StringBuilder.class, char.class));
        } catch (NoSuchMethodException | IllegalAccessException ex) {
            throw new InternalError(ex);
        }

        APPEND_OPENING_PARENTHESIS = MethodHandles.insertArguments(appendChar, 1, '(');
        APPEND_CLOSING_PARENTHESIS = MethodHandles.insertArguments(appendChar, 1, ')');
    }

    private final MethodHandle appender, printer;

    Serializer(Class type, MethodHandle serializingHandle) {
        MethodHandle closingParenthesis = MethodHandles.dropArguments(APPEND_CLOSING_PARENTHESIS, 1, type);
        MethodHandle a = MethodHandles.foldArguments(closingParenthesis, serializingHandle);
        appender = MethodHandles.foldArguments(a, APPEND_OPENING_PARENTHESIS);

        printer = MethodHandles.foldArguments(serializingHandle, NEW_STRINGBUILDER);
    }

    public MethodHandle getAppender() {
        return appender;
    }

    public MethodHandle getPrinter() {
        return printer;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy