app/src/main/java/db/juhaku/juhakudb/core/android/RepositoryLookupInjector.java
/**
     * Method will lookup and inject repositories if found from given object. Repository should be
     * mapped with {@link Repository} annotation and injected to objects field with {@link Inject}
     * annotation.
     *
     * @param obj Object to look for Injectable repositories.
     * @throws IllegalStateException if lookup injection will fail for number of reason.
     */
Method will lookup and inject repositories if found from given object. Repository should be
mapped with  Repository annotation and injected to objects field with  Inject
annotation.

@param obj Object to look for Injectable repositories.
@throws IllegalStateException if lookup injection will fail for number of reason.
Method will lookup and inject repositories if found from given object . Repository should be mapped with Repository annotation and injected to objects field with Inject annotation . @param obj Object to look for Injectable repositories . @throws IllegalStateException if lookup injection will fail for number of reason .
void lookupRepositories(Object obj) {
        if (obj == null) {
            throw new IllegalStateException("Object provided must not be null");
        }
        Class<?> lookup = obj.getClass();
        while (lookup.getSuperclass() != null) {
            for (Field field : lookup.getDeclaredFields()) {
                boolean accessible = field.isAccessible();
                field.setAccessible(true);
                if (field.getAnnotation(Inject.class) != null) {
                    Class<?> repositoryType = ReflectionUtils.getFieldType(field);
                    if (repositoryType.getAnnotation(Repository.class) == null) {
                        throw new IllegalStateException("Wrongly placed " + Inject.class.getName() + " annotation. " +
                                "It should only be placed on classes mapped with: " + Repository.class.getName() + " annotation.");
                    }
                    try {
                        Object repository = field.get(obj);
                        if (repository == null) {
                            field.set(obj, databaseManager.getRepository(repositoryType));
                        }
                    } catch (IllegalAccessException e) {
                        throw new IllegalStateException("Could not access field value: " + field, e);
                    }
                }
                field.setAccessible(accessible);
            }
            lookup = lookup.getSuperclass();
        }
    }
void lookupRepositories ( Object obj ) { if ( obj == null ) { throw new IllegalStateException ( "Object provided must not be null" ) ; } Class < ? > lookup = obj . getClass ( ) ; while ( lookup . getSuperclass ( ) != null ) { for ( Field field : lookup . getDeclaredFields ( ) ) { boolean accessible = field . isAccessible ( ) ; field . setAccessible ( true ) ; if ( field . getAnnotation ( Inject . class ) != null ) { Class < ? > repositoryType = ReflectionUtils . getFieldType ( field ) ; if ( repositoryType . getAnnotation ( Repository . class ) == null ) { throw new IllegalStateException ( "Wrongly placed " + Inject . class . getName ( ) + " annotation. " + "It should only be placed on classes mapped with: " + Repository . class . getName ( ) + " annotation." ) ; } try { Object repository = field . get ( obj ) ; if ( repository == null ) { field . set ( obj , databaseManager . getRepository ( repositoryType ) ) ; } } catch ( IllegalAccessException e ) { throw new IllegalStateException ( "Could not access field value: " + field , e ) ; } } field . setAccessible ( accessible ) ; } lookup = lookup . getSuperclass ( ) ; } }
Method will lookup and inject repositories if found from given object.
Method will lookup and inject repositories if found from given object .
// make sure found field with Inject annotation is repository // if repository is not injected inject it