简体   繁体   中英

Java getClass returns different class

I'm trying to create Dialog with text fields inside it. The code is following

private void showBatchDialog() {
    Dialog<Batch> dialog = new Dialog<Batch>();
    dialog.setTitle("Add batch");
    dialog.setHeaderText("adding new batch");

    ButtonType addButtonType = new ButtonType("Add batch",
            ButtonData.OK_DONE);

    Collection<ButtonType> buttons = new ArrayList<ButtonType>();
    buttons.add(addButtonType);
    buttons.add(ButtonType.CANCEL);

    dialog.getDialogPane().getButtonTypes().addAll(buttons);

    GridPane grid = new GridPane();
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(20, 150, 10, 10));

    TextField code = new TextField();
    code.setPromptText("code");
    TextField name = new TextField();
    name.setPromptText("name");
    grid.add(new Label("code"), 0, 0);
    grid.add(code, 1, 0);
    grid.add(new Label("name"), 0, 1);
    grid.add(name, 1, 1);

    dialog.getDialogPane().setContent(grid);
    dialog.setResultConverter(value -> {
        if (value == addButtonType) {
            return new Batch() {
                {
                    setCode(code.getText());
                    setName(name.getText());
                }
            };
        } else {
            return null;
        }
    });

    Optional<Batch> result = dialog.showAndWait();


    result.ifPresent(consumer -> {
        Batch batch = (Batch) consumer;
        batch.getClass(); // Doesn't returns Batch class
    });
}

So when I'm trying to get returned by dialog class it doesn't returns Batch. The question is how to get Batch class?

The body of Batch class is the following

@Entity
public class Batch {
    @Id
    @GeneratedValue(generator="uuid")
    @GenericGenerator(name="uuid", strategy="uuid2")
    @Column(name="batch_id")
    private String uuid;
    @NotNull(message="Code field could not be an empty")
    @Column(unique=true)
    private String code;
    private String name;

    @OneToMany(mappedBy = "batch", cascade = CascadeType.ALL)
    private Collection<Product> products = new ArrayList<Product>();

    public Collection<Product> getProducts() {
        return products;
    }

    public void setProducts(Collection<Product> products) {
        this.products = products;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

}

You create instance of anonymous class that extends Batch . Replace your if statement

if (value == addButtonType) {
     return new Batch() {
        {
            setCode(code.getText());
            setName(name.getText());
        }
     };
}

with following code:

    if (value == addButtonType) {
        Batch b = new Batch();
        b.setCode(code.getText());
        b.setName(name.getText());
        return b;
    }

In java, an object's getClass() method returns Class<? extends Object> Class<? extends Object> , so you would not expect to get Batch as the result of this code -- instead you should expect an instance of Class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM