mvvmfx/src/main/java/de/saxsys/mvvmfx/internal/viewloader/ViewLoaderReflectionUtils.java
@SuppressWarnings("unchecked")
    public static <V extends View<? extends VM>, VM extends ViewModel> void createAndInjectViewModel(final V view,
            Consumer<ViewModel> newVmConsumer) {
        final Class<?> viewModelType = TypeResolver.resolveRawArgument(View.class, view.getClass());

        if (viewModelType == ViewModel.class) {
            // if no viewModel can be created, we have to check if the user has
            // tried to inject a ViewModel
            final List<Field> viewModelFields = ViewLoaderReflectionUtils.getViewModelFields(view.getClass());

            if (!viewModelFields.isEmpty()) {
                throw new RuntimeException("The given view of type <" + view.getClass()
                        + "> has no generic viewModel type declared but tries to inject a viewModel.");
            }
            return;
        }
        if (viewModelType == TypeResolver.Unknown.class) {
            return;
        }

        final Optional<Field> fieldOptional = getViewModelField(view.getClass(), viewModelType);
        if (fieldOptional.isPresent()) {
            Field field = fieldOptional.get();

            ReflectionUtils.accessMember(field, () -> {
                Object existingViewModel = field.get(view);

                if (existingViewModel == null) {
                    final Object newViewModel = DependencyInjector.getInstance().getInstanceOf(viewModelType);

                    field.set(view, newViewModel);

                    newVmConsumer.accept((ViewModel) newViewModel);
                }
            }, "Can't inject ViewModel of type <" + viewModelType + "> into the view <" + view + ">");

        }
    }
@SuppressWarnings("unchecked")
    public static <V extends View<? extends VM>, VM extends ViewModel> void createAndInjectViewModel(final V view,
            Consumer<ViewModel> newVmConsumer) {
        final Class<?> viewModelType = TypeResolver.resolveRawArgument(View.class, view.getClass());

        if (viewModelType == ViewModel.class) {
            // if no viewModel can be created, we have to check if the user has
            // tried to inject a ViewModel
            final List<Field> viewModelFields = ViewLoaderReflectionUtils.getViewModelFields(view.getClass());

            if (!viewModelFields.isEmpty()) {
                throw new RuntimeException("The given view of type <" + view.getClass()
                        + "> has no generic viewModel type declared but tries to inject a viewModel.");
            }
            return;
        }
        if (viewModelType == TypeResolver.Unknown.class) {
            return;
        }

        final Optional<Field> fieldOptional = getViewModelField(view.getClass(), viewModelType);
        if (fieldOptional.isPresent()) {
            Field field = fieldOptional.get();

            ReflectionUtils.accessMember(field, () -> {
                Object existingViewModel = field.get(view);

                if (existingViewModel == null) {
                    final Object newViewModel = DependencyInjector.getInstance().getInstanceOf(viewModelType);

                    field.set(view, newViewModel);

                    newVmConsumer.accept((ViewModel) newViewModel);
                }
            }, "Can't inject ViewModel of type <" + viewModelType + "> into the view <" + view + ">");

        }
    }
@ SuppressWarnings ( "unchecked" ) public static < V extends View < ? extends VM > , VM extends ViewModel > void createAndInjectViewModel ( final V view , Consumer < ViewModel > newVmConsumer ) { final Class < ? > viewModelType = TypeResolver . resolveRawArgument ( View . class , view . getClass ( ) ) ; if ( viewModelType == ViewModel . class ) { // if no viewModel can be created, we have to check if the user has // tried to inject a ViewModel final List < Field > viewModelFields = ViewLoaderReflectionUtils . getViewModelFields ( view . getClass ( ) ) ; if ( ! viewModelFields . isEmpty ( ) ) { throw new RuntimeException ( "The given view of type <" + view . getClass ( ) + "> has no generic viewModel type declared but tries to inject a viewModel." ) ; } return ; } if ( viewModelType == TypeResolver . Unknown . class ) { return ; } final Optional < Field > fieldOptional = getViewModelField ( view . getClass ( ) , viewModelType ) ; if ( fieldOptional . isPresent ( ) ) { Field field = fieldOptional . get ( ) ; ReflectionUtils . accessMember ( field , ( ) -> { Object existingViewModel = field . get ( view ) ; if ( existingViewModel == null ) { final Object newViewModel = DependencyInjector . getInstance ( ) . getInstanceOf ( viewModelType ) ; field . set ( view , newViewModel ) ; newVmConsumer . accept ( ( ViewModel ) newViewModel ) ; } } , "Can't inject ViewModel of type <" + viewModelType + "> into the view <" + view + ">" ) ; } }
This method is used to create and inject the ViewModel for a given View
instance.

The following checks are done:
<ul>
<li>Check whether the View class specifies a ViewModel type as generic
type.</li>
<li>Check whether the View has a field with a matching ViewModel type and
the annotation {@link InjectViewModel}.</li>

<li>Check whether field in the view instance already contains a ViewModel
instance. In this case nothing will happen to the existing ViewModel
instance.</li>

</ul>

If a suitable field was found a new ViewModel instance will be created
and injected into the field. After that the given Consumer function will
be applied with the injected ViewModel instance as argument.

@param view
the view instance.
@param <V>
the generic type of the View.
@param <VM>
the generic type of the ViewModel.
@param newVmConsumer
a Consumer function that is applied when a new ViewModel
instance is created.

@throws RuntimeException
if there is a ViewModel field in the View with the
{@link InjectViewModel} annotation whose type doesn't match
the generic ViewModel type from the View class.
This method is used to create and inject the ViewModel for a given View instance .