mvvmfx/src/main/java/de/saxsys/mvvmfx/internal/viewloader/ViewLoaderReflectionUtils.java
@SuppressWarnings("unchecked")
    public static <ViewType extends View<? extends ViewModelType>, ViewModelType extends ViewModel> ViewModelType getExistingViewModel(
            ViewType view) {
        final Class<?> viewModelType = TypeResolver.resolveRawArgument(View.class, view.getClass());
        Optional<Field> fieldOptional = getViewModelField(view.getClass(), viewModelType);
        if (fieldOptional.isPresent()) {
            Field field = fieldOptional.get();
            return ReflectionUtils.accessMember(field, () -> (ViewModelType) field.get(view),
                    "Can't get the viewModel of type <" + viewModelType + ">");
        } else {
            return null;
        }
    }
@SuppressWarnings("unchecked")
    public static <ViewType extends View<? extends ViewModelType>, ViewModelType extends ViewModel> ViewModelType getExistingViewModel(
            ViewType view) {
        final Class<?> viewModelType = TypeResolver.resolveRawArgument(View.class, view.getClass());
        Optional<Field> fieldOptional = getViewModelField(view.getClass(), viewModelType);
        if (fieldOptional.isPresent()) {
            Field field = fieldOptional.get();
            return ReflectionUtils.accessMember(field, () -> (ViewModelType) field.get(view),
                    "Can't get the viewModel of type <" + viewModelType + ">");
        } else {
            return null;
        }
    }
@ SuppressWarnings ( "unchecked" ) public static < ViewType extends View < ? extends ViewModelType > , ViewModelType extends ViewModel > ViewModelType getExistingViewModel ( ViewType view ) { final Class < ? > viewModelType = TypeResolver . resolveRawArgument ( View . class , view . getClass ( ) ) ; Optional < Field > fieldOptional = getViewModelField ( view . getClass ( ) , viewModelType ) ; if ( fieldOptional . isPresent ( ) ) { Field field = fieldOptional . get ( ) ; return ReflectionUtils . accessMember ( field , ( ) -> ( ViewModelType ) field . get ( view ) , "Can't get the viewModel of type <" + viewModelType + ">" ) ; } else { return null ; } }
This method is used to get the ViewModel instance of a given
view/codeBehind.

@param view
the view instance where the viewModel will be looked for.
@param <ViewType>
the generic type of the View
@param <ViewModelType>
the generic type of the ViewModel
@return the ViewModel instance or null if no viewModel could be found.
This method is used to get the ViewModel instance of a given view / codeBehind .