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

no.steria.osgi.jsr330activator.implementation.InjectionField Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package no.steria.osgi.jsr330activator.implementation;

import java.lang.reflect.Field;

/**
 * An {@link Injection} implementation that operates on
 * a provider field.  The field may be declared as private.
 *
 * @author Steinar Bang
 *
 */
class InjectionField extends InjectionBase {

    private Object provider;
    private Field field;

    public InjectionField(Object provider, Field field) {
        this.provider = provider;
        this.field = field;
        this.field.setAccessible(true);
    }

    public Class getInjectedServiceType() {
        return field.getType();
    }

    public boolean isInjected() {
        Object injectedService = getInjectedService();
        return injectedService != null;
    }

    public void doInject(Object service) {
        setInjectedService(service);
    }

    public void doRetract() {
        setInjectedService(null);
    }

    private Object getInjectedService() {
        try {
            return field.get(provider);
        } catch (Exception e) {
        }

        return null;
    }

    private void setInjectedService(Object service) {
        try {
            field.set(provider, service);
        } catch (Exception e) {
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy