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

com.github.romanqed.jeflect.loader.DefineObjectFactory Maven / Gradle / Ivy

package com.github.romanqed.jeflect.loader;

import com.github.romanqed.jfunc.Function1;

import java.util.Objects;
import java.util.concurrent.Callable;

public final class DefineObjectFactory implements ObjectFactory {
    private final DefineLoader loader;

    public DefineObjectFactory(DefineLoader loader) {
        this.loader = Objects.requireNonNull(loader);
    }

    public DefineLoader getLoader() {
        return loader;
    }

    @Override
    public T create(String name, Callable provider, Function1, ? extends T> creator) {
        var clazz = this.loader.load(name);
        try {
            if (clazz == null) {
                var bytes = provider.call();
                clazz = this.loader.define(name, bytes);
            }
            return creator.invoke(clazz);
        } catch (Error | RuntimeException e) {
            throw e;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public T create(String name, Callable provider) {
        return create(name, provider, clazz -> (T) clazz.getDeclaredConstructor().newInstance());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy