Skip to content

JDBC: create objects without reflection #1434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

singhpk234
Copy link
Collaborator

@singhpk234 singhpk234 commented Apr 23, 2025

Create model objects without reflection :

Address feedback here

cc @flyrain @eric-maynard

(Converter<T>)
entityClass.getDeclaredConstructor().newInstance(); // Create a new instance
R entity = transformer.apply(object.fromResultSet(resultSet));
Converter<T> model = (Converter<T>) createModelInstance(entityClass);
Copy link
Contributor

@dimas-b dimas-b Apr 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about changing entityClass to Function<ResultSet, R> converter and removing transformer.

Then:

public static ModelEntity fromResultSet(ResultSet r)  {
    return ModelEntity.builder()
        ....

End result: no reflection, not if/else, one less method param, simpler entity classes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. It's a good idea to combine toEntity() and fromResultSet() here.

@eric-maynard
Copy link
Contributor

In #1487 I solved this by just having the caller pass in an instance of the Converter, which seems easy enough

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants