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

io.micronaut.guice.MicronautInjector Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
/*
 * Copyright 2017-2024 original authors
 *
 * 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
 *
 * https://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 io.micronaut.guice;

import com.google.inject.Binding;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.MembersInjector;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Scope;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Named;
import com.google.inject.spi.Element;
import com.google.inject.spi.InjectionPoint;
import com.google.inject.spi.TypeConverterBinding;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.Qualifier;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.type.Argument;
import io.micronaut.inject.qualifiers.Qualifiers;
import jakarta.inject.Singleton;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * Subset implementation of the {@link Injector} interface.
 */
@Singleton
@Internal
final class MicronautInjector
    implements Injector {
    private final ApplicationContext applicationContext;

    MicronautInjector(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public void injectMembers(Object instance) {
        applicationContext.inject(instance);
    }

    @Override
    public  MembersInjector getMembersInjector(TypeLiteral typeLiteral) {
        return applicationContext::inject;
    }

    @Override
    public  MembersInjector getMembersInjector(Class type) {
        return applicationContext::inject;
    }

    @Override
    public Map, Binding> getBindings() {
        return Map.of();
    }

    @Override
    public Map, Binding> getAllBindings() {
        return Map.of();
    }

    @Override
    public  Binding getBinding(Key key) {
        throw new UnsupportedOperationException("Method getBinding is not supported");
    }

    @Override
    public  Binding getBinding(Class type) {
        throw new UnsupportedOperationException("Method getBinding is not supported");
    }

    @Override
    public  Binding getExistingBinding(Key key) {
        throw new UnsupportedOperationException("Method getExistingBinding is not supported");
    }

    @Override
    public  List> findBindingsByType(TypeLiteral type) {
        return List.of();
    }

    @Override
    public  Provider getProvider(Key key) {
        Objects.requireNonNull(key, "Key cannot be null");
        Argument argument = (Argument) Argument.of(key.getTypeLiteral().getType());
        Qualifier qualifier = toQualifier(key);
        return () -> applicationContext.getBean(argument, qualifier);
    }

    @Override
    public  Provider getProvider(Class type) {
        return () -> applicationContext.getBean(type);
    }

    @Override
    public  T getInstance(Key key) {
        Objects.requireNonNull(key, "Key cannot be null");
        Argument argument = (Argument) Argument.of(key.getTypeLiteral().getType());
        Qualifier qualifier = toQualifier(key);
        return applicationContext.getBean(argument, qualifier);
    }

    private static  Qualifier toQualifier(Key key) {
        Class annotationType = key.getAnnotationType();
        Qualifier qualifier = null;
        Annotation annotation = key.getAnnotation();
        if (annotation instanceof Named named) {
            qualifier = Qualifiers.byName(named.value());
        } else if (annotation != null) {
            qualifier = Qualifiers.byAnnotation(annotation);
        } else if (annotationType != null) {
            qualifier = new AnnotationTypeQualifier<>(annotationType);
        }
        return qualifier;
    }

    @Override
    public  T getInstance(Class type) {
        return applicationContext.getBean(type);
    }

    @Override
    public Injector getParent() {
        return null;
    }

    @Override
    public Injector createChildInjector(Iterable modules) {
        throw new UnsupportedOperationException("Method createChildInjector is not supported");
    }

    @Override
    public Injector createChildInjector(Module... modules) {
        throw new UnsupportedOperationException("Method createChildInjector is not supported");
    }

    @Override
    public Map, Scope> getScopeBindings() {
        return Map.of();
    }

    @Override
    public Set getTypeConverterBindings() {
        return Set.of();
    }

    @Override
    public List getElements() {
        return List.of();
    }

    @Override
    public Map, List> getAllMembersInjectorInjectionPoints() {
        return Map.of();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy