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

io.github.pustike.inject.impl.InstanceProvider Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright (C) 2016-2017 the original author or 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
 *
 * 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 io.github.pustike.inject.impl;

import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import javax.inject.Provider;

import io.github.pustike.inject.Injector;
import io.github.pustike.inject.bind.InjectionPoint;

/**
 * Provides new instance of the target for binding.
 */
final class InstanceProvider implements Provider {
    private final Class targetType;
    private final Executable executable;
    private final Object methodInstance;
    private final Class providerType;
    private Provider providerInstance;
    private Injector injector;
    private InjectionPoint injectionPoint;

    private InstanceProvider(Class targetType, Executable executable, Object methodInstance,
            Class providerType) {
        this.targetType = targetType;
        this.executable = executable;
        this.methodInstance = methodInstance;
        this.providerType = providerType;
    }

    static  InstanceProvider from(Class targetType) {
        return new InstanceProvider<>(targetType, null, null, null);
    }

    static  Provider from(Constructor constructor) {
        return new InstanceProvider<>(null, constructor, null, null);
    }

    static  Provider from(Method providerMethod, Object instance) {
        return new InstanceProvider<>(null, providerMethod, instance, null);
    }

    static  Provider fromProvider(Class> providerType) {
        return new InstanceProvider<>(null, null, null, providerType);
    }

    void setInjector(Injector injector) {
        this.injector = injector;
    }

    @Override
    public T get() {
        try {
            // if provider type is defined,
            if (providerType != null) {
                return getProviderInstance().get();// return the instance from this provider
            } else {// create a new instance from targetType or factory-constructor or factory-method
                if (injectionPoint == null) {
                    injectionPoint = createInjectionPoint();
                }
                @SuppressWarnings("unchecked")
                T newInstance = (T) injectionPoint.injectTo(methodInstance, injector);
                return newInstance;
            }
        } catch (Throwable t) {
            throw new RuntimeException("error when creating provider", t);
        }
    }

    @SuppressWarnings("unchecked")
    private Provider getProviderInstance() {
        if(providerInstance == null) {// create the provider instance
            InjectionPoint injectionPoint = DefaultInjectionPointLoader.createInjectionPoint(providerType);
            providerInstance = (Provider) injectionPoint.injectTo(null, injector);
            injector.injectMembers(providerInstance);// and inject its members first
        }
        return providerInstance;
    }

    private InjectionPoint createInjectionPoint() {
        if (targetType != null) {
            return DefaultInjectionPointLoader.createInjectionPoint(targetType);
        } else if (executable != null) {
            return DefaultInjectionPointLoader.createInjectionPoint(executable);
        }
        throw new RuntimeException("injection point can not be created with the parameters provided!");
    }
}