Search in sources :

Example 1 with FieldInjectionPoint

use of org.b3log.latke.ioc.point.FieldInjectionPoint in project latke by b3log.

the class BeanImpl method resolveSuperclassFieldDependencies.

/**
 * Resolves super class field dependencies for the specified reference.
 *
 * @param reference the specified reference
 * @param clazz     the super class of the specified reference
 * @throws Exception exception
 */
private void resolveSuperclassFieldDependencies(final Object reference, final Class<?> clazz) throws Exception {
    if (clazz.equals(Object.class)) {
        return;
    }
    final Class<?> superclass = clazz.getSuperclass();
    resolveSuperclassFieldDependencies(reference, superclass);
    if (Modifier.isAbstract(clazz.getModifiers()) || Modifier.isInterface(clazz.getModifiers())) {
        return;
    }
    final BeanImpl<?> bean = (BeanImpl<?>) beanManager.getBean(clazz);
    final Set<FieldInjectionPoint> injectionPoints = bean.fieldInjectionPoints;
    for (final FieldInjectionPoint injectionPoint : injectionPoints) {
        Object injection = beanManager.getInjectableReference(injectionPoint, null);
        if (injection == null) {
            for (final FieldProvider<?> provider : bean.fieldProviders) {
                if (provider.getAnnotated().equals(injectionPoint.getAnnotated())) {
                    injection = provider;
                    break;
                }
            }
        }
        final Field field = injectionPoint.getAnnotated().getJavaMember();
        try {
            final Field declaredField = proxyClass.getDeclaredField(field.getName());
            if (!Reflections.matchInheritance(declaredField, field)) {
                // Hide
                try {
                    field.set(reference, injection);
                } catch (final Exception e) {
                    throw new RuntimeException(e);
                }
            }
        } catch (final NoSuchFieldException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : ProxyObject(javassist.util.proxy.ProxyObject) FieldInjectionPoint(org.b3log.latke.ioc.point.FieldInjectionPoint)

Example 2 with FieldInjectionPoint

use of org.b3log.latke.ioc.point.FieldInjectionPoint in project latke by b3log.

the class BeanImpl method initFieldInjectionPoints.

/**
 * Initializes field injection points.
 */
private void initFieldInjectionPoints() {
    final Set<AnnotatedField<? super T>> annotatedFields = annotatedType.getFields();
    for (final AnnotatedField<? super T> annotatedField : annotatedFields) {
        final Field field = annotatedField.getJavaMember();
        if (field.getType().equals(Provider.class)) {
            // by provider
            final FieldProvider<T> provider = new FieldProvider<T>(beanManager, annotatedField);
            fieldProviders.add(provider);
            final FieldInjectionPoint fieldInjectionPoint = new FieldInjectionPoint(this, annotatedField);
            fieldInjectionPoints.add(fieldInjectionPoint);
        } else {
            // by qualifier
            final FieldInjectionPoint fieldInjectionPoint = new FieldInjectionPoint(this, annotatedField);
            fieldInjectionPoints.add(fieldInjectionPoint);
        }
    }
}
Also used : FieldProvider(org.b3log.latke.ioc.provider.FieldProvider) FieldInjectionPoint(org.b3log.latke.ioc.point.FieldInjectionPoint)

Example 3 with FieldInjectionPoint

use of org.b3log.latke.ioc.point.FieldInjectionPoint in project latke by b3log.

the class BeanImpl method resolveCurrentclassFieldDependencies.

/**
 * Resolves current class field dependencies for the specified reference.
 *
 * @param reference the specified reference
 */
private void resolveCurrentclassFieldDependencies(final Object reference) {
    for (final FieldInjectionPoint injectionPoint : fieldInjectionPoints) {
        Object injection = beanManager.getInjectableReference(injectionPoint, null);
        if (injection == null) {
            for (final FieldProvider<?> provider : fieldProviders) {
                if (provider.getAnnotated().equals(injectionPoint.getAnnotated())) {
                    injection = provider;
                    break;
                }
            }
        }
        final Field field = injectionPoint.getAnnotated().getJavaMember();
        try {
            final Field declaredField = proxyClass.getDeclaredField(field.getName());
            if (declaredField.isAnnotationPresent(Inject.class)) {
                try {
                    declaredField.set(reference, injection);
                } catch (final Exception e) {
                    throw new RuntimeException(e);
                }
            }
        } catch (final NoSuchFieldException ex) {
            try {
                field.set(reference, injection);
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : ProxyObject(javassist.util.proxy.ProxyObject) FieldInjectionPoint(org.b3log.latke.ioc.point.FieldInjectionPoint)

Aggregations

FieldInjectionPoint (org.b3log.latke.ioc.point.FieldInjectionPoint)3 ProxyObject (javassist.util.proxy.ProxyObject)2 FieldProvider (org.b3log.latke.ioc.provider.FieldProvider)1