class AnnotationInjector {

    public static void inject(final Object object, final Factory factory) {
        if (object == null) {
            throw new IllegalArgumentException("object cannot be null.");
        }
        if (factory == null) {
            throw new IllegalArgumentException("factory cannot be null.");
        }
        Class<?> c = object.getClass();
        // first to all annotated public setter methods.
        for (final Method method : c.getMethods()) {
            if (method.isAnnotationPresent(InjectForRequest.class)) {
                injectIntoMethod(object, factory, method);
            }
        }
        // and then all annotated fields.
        while (c != Object.class && c != null) {
            for (final Field field : c.getDeclaredFields()) {
                if (field.isAnnotationPresent(InjectForRequest.class)) {
                    injectIntoField(object, factory, field);
                }

            }

            c = c.getSuperclass();
        }
    }

}

class AnnotationInjectorTest {

    @Test
    public void testInject() {

        final Factory f = new Factory() {

            @SuppressWarnings("unchecked")
            public <T> T getObject(final String name, final Class<T> type) {
                return (T) name;
            }

        };
        final Sample object = new Sample();
        AnnotationInjector.inject(object, f);
        assertEquals("me", object.foo);
        assertEquals("foot", object.foot);

    }

}