jmix-framework / jmix

Jmix framework
https://www.jmix.io
Apache License 2.0
561 stars 121 forks source link

ClassCastException. Hot deployment for dialog window #3695

Closed rusiaikinat closed 1 week ago

rusiaikinat commented 2 weeks ago

Environment

Jmix version: 2.3.3

Bug Description

A change in a dialog box raises an exception

Steps To Reproduce

public class CommentEditView extends StandardView {

 @ViewComponent
    protected JmixTextArea messageCommentField;

    @Autowired
    protected ViewValidation viewValidation;

    @Autowired
    protected NotBlankValidator notBlankValidator;

    public void setMessage(String message) {
        messageCommentField.setValue(message);
    }

    public String getMessage() {
        return messageCommentField.getValue();
    }

    @Subscribe
    public void onInit(final InitEvent event) {
        messageCommentField.addValidator(notBlankValidator);
    }

    @Subscribe("messageCommentField")
    public void onMessageCommentFieldComponentValueChange(final AbstractField.ComponentValueChangeEvent<JmixTextArea, String> event) {
        // add character counter e.g. 388/600
        String editedMessage = event.getValue();
        int messageLength = 0;

        if (editedMessage != null && !editedMessage.isBlank()) {
            messageLength = editedMessage.length();
        }

        messageCommentField.setHelperText(messageLength + "/" + messageCommentField.getMaxLength());
    }

    @Subscribe("closeAction")
    public void onCloseAction(final ActionPerformedEvent event) {
        close(StandardOutcome.DISCARD);
    }

    @Subscribe("saveAction")
    public void onSaveAction(final ActionPerformedEvent event) {
        ValidationErrors errors = viewValidation.validateUiComponent(messageCommentField);

        if (!errors.isEmpty()) {
            viewValidation.showValidationErrors(errors);
            viewValidation.focusProblemComponent(errors);
            System.out.println("Test");
            return;
        }

        close(StandardOutcome.SAVE);
    }
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<view xmlns="http://jmix.io/schema/flowui/view"
      title="msg://commentEditView.title">
    <actions>
        <action id="saveAction"
                text="msg:///actions.Ok"
                icon="CHECK"
                shortcutCombination="${SAVE_SHORTCUT}"
                actionVariant="PRIMARY"/>
        <action id="closeAction"
                text="msg:///actions.Cancel"
                icon="BAN"
                shortcutCombination="${CLOSE_SHORTCUT}"/>
    </actions>
    <layout>
        <textArea id="messageCommentField" width="100%" maxLength="600"
                  placeholder="XXXr"
                  helperText="0/600" valueChangeMode="EAGER"
                  required="true"

                  maxHeight="130px" minHeight="20px"
                  classNames="lumo-size-s"/>
        <hbox id="detailActions">
            <button id="saveBtn" action="saveAction"/>
            <button id="closeBtn" action="closeAction"/>
        </hbox>
    </layout>
</view>
        dialogWindows.view(this, CommentEditView.class)
                .withViewConfigurer(view -> view.setMessage(message))
                .withAfterCloseListener(event -> {
                    if (event.closedWith(StandardOutcome.SAVE)) {
                        CommentEditView commentEditView = event.getView();
                        String editedMessage = commentEditView.getMessage();
                        // update message of comment
                        if (!editedMessage.equals(message)) {
                            System.out.println("mess");
                        }
                    }
                })
                .build()
                .open();

Current Behavior

java.lang.ClassCastException: class com.company.hotbug.view.commentedit.CommentEditView cannot be cast to class com.company.hotbug.view.commentedit.CommentEditView (com.company.hotbug.view.commentedit.CommentEditView is in unnamed module of loader io.jmix.core.impl.JavaClassLoader$FileClassLoader @2644ad4e; com.company.hotbug.view.commentedit.CommentEditView is in unnamed module of loader 'app')
    at io.jmix.flowui.view.builder.AbstractWindowBuilderProcessor.lambda$initDialog$0(AbstractWindowBuilderProcessor.java:76) ~[jmix-flowui-2.3.3.jar:na]
    at java.base/java.util.Optional.ifPresent(Optional.java:178) ~[na:na]
    at io.jmix.flowui.view.builder.AbstractWindowBuilderProcessor.initDialog(AbstractWindowBuilderProcessor.java:76) ~[jmix-flowui-2.3.3.jar:na]
    at io.jmix.flowui.view.builder.WindowBuilderProcessor.build(WindowBuilderProcessor.java:47) ~[jmix-flowui-2.3.3.jar:na]
    at io.jmix.flowui.view.builder.AbstractWindowBuilder.build(AbstractWindowBuilder.java:113) ~[jmix-flowui-2.3.3.jar:na]
    at com.company.hotbug.view.newentity.NewEntityListView.onButtonClick(NewEntityListView.java:43) ~[main/:na]
    at com.vaadin.flow.component.ComponentEventBus.fireEventForListener(ComponentEventBus.java:239) ~[flow-server-24.3.13.jar:24.3.13]

Sample Project

hotBug.zip

glebfox commented 1 week ago

Not a bug. A view that uses hot deployed one by class, must be also re-opened, because it's also re-compiled. See log message:

Hot deploy: CommentEditView.java [752 ms]
Compiled files:
    com/company/hotbug/view/commentedit/CommentEditView.java
    com/company/hotbug/view/newentity/NewEntityListView.java