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

kos.injector.InjectorImplementationLoader Maven / Gradle / Ivy

/*
 * Copyright 2019 Skullabs Contributors (https://github.com/skullabs)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package kos.injector;

import injector.Factory;
import injector.Injector;
import kos.api.ImplementationLoader;
import kos.api.KosContext;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

@Slf4j
public class InjectorImplementationLoader implements ImplementationLoader {

    private final Injector injector;

    public InjectorImplementationLoader() {
        this.injector = createInjector();
    }

    private Injector createInjector() {
        return Injector.create(false).setLogger(log::debug);
    }

    @Override
    public  Iterable instancesExposedAs(Class interfaceType) {
        return injector.instancesExposedAs(interfaceType);
    }

    @Override
    public  Result instanceOf(Class type) {
        try {
            return Result.of(injector.instanceOf(type));
        } catch ( IllegalArgumentException cause ) {
            log.debug("Could not get instance of " + type.getCanonicalName(), cause);
            return Result.failure(cause);
        }
    }

    @Override
    public  void register(Class type, T instance) {
        injector.registerFactoryOf(type, new StaticFactory<>(type, instance));
    }

    @Value
    private static class StaticFactory implements Factory {

        Class exposedType;
        T instance;

        @Override
        public T create(Injector injector, Class aClass) {
            return instance;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy