AndrewBourgeois
AndrewBourgeois

Reputation: 2765

CodeModel: manually insert import statement

How do I manually insert an import statement using CodeModel?

I'd like to use Arrays.toString(...) in a direct statement.

Context: I'm generating a toString() method in the generated code just like Eclipse does. The Arrays class is used to avoid printing references to the arrays I have declared.

Upvotes: 2

Views: 2117

Answers (2)

med zied a
med zied a

Reputation: 1

My workaround is to referance the class by its full package declarationinside the direct statement.

This is a bite of code I am currently writing, notice the full package declaration. I also write some helpers or enum types to rapidly generate the full path of a certain class as String.

save.addClickListener(event -> {
        if (binder.writeBeanIfValid(pojo())) {
            infoLabel.setText("Saved bean values: " + pojo());
        } else {
            com.vaadin.flow.data.binder.BinderValidationStatus<POJO> validate = binder.validate();
            String errorText = validate.getFieldValidationStatuses()
                    .stream().filter(com.vaadin.flow.data.binder.BindingValidationStatus::isError)
                    .map(com.vaadin.flow.data.binder.BindingValidationStatus::getMessage)
                    .map(java.util.Optional::get).distinct()
                    .collect(java.util.streamCollectors.joining(", "));
            infoLabel.setText("There are errors: " + errorText);
        }
    });

Upvotes: 0

AndrewBourgeois
AndrewBourgeois

Reputation: 2765

The solution was to reference the class:

JClass arrays = codeModel.ref(Arrays.class);

Upvotes: 2

Related Questions