Skip to content
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

Can not set static final field #2607

Closed
mvysny opened this issue Jan 24, 2024 · 4 comments · Fixed by #2609
Closed

Can not set static final field #2607

mvysny opened this issue Jan 24, 2024 · 4 comments · Fixed by #2609

Comments

@mvysny
Copy link

mvysny commented Jan 24, 2024

If the select returns a column with the same name as a public static final field in the bean, JDBI will try to write the value to that column and will fail with:

Caused by: java.lang.IllegalAccessException: Can not set static final com.gitlab.mvysny.jdbiorm.Property field com.gitlab.mvysny.jdbiorm.NestedJoinOutcome.DEPARTMENT_NAME to java.lang.String
	at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:76)
	at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:80)
	at java.base/jdk.internal.reflect.UnsafeQualifiedStaticObjectFieldAccessorImpl.set(UnsafeQualifiedStaticObjectFieldAccessorImpl.java:77)
	at java.base/java.lang.reflect.Field.set(Field.java:799)
	at org.jdbi.v3.core.mapper.reflect.FieldMapper$BoundFieldMapper.writeField(FieldMapper.java:285)
	... 57 more

In the example above, I have a SELECT x as department_name from y mapping values to a bean with a public static final String DEPARTMENT_NAME = "foo"; field via FieldMapper.

Reasoning: in jdbi-orm I'm recommending the users to create a public static final Property<> ID fields which allows you to programmatically create Conditions such as Person.ID.lt(25). However, since there is a database column named ID, JDBI's FieldMapper tries to write the value both to the instance field id and the static final field ID and fails.

I believe that JDBI should ignore static fields in general, or at least static final fields.

Meanwhile, is there a workaround/configuration which would allow me to ignore such fields please?

JDBI 3.43.0

@mvysny
Copy link
Author

mvysny commented Jan 24, 2024

I've worked around the problem by copying the FieldMapper class to jdbi-orm and patching it to ignore static fields; that works very well. A proper solution would probably add a line to FieldMapper.createSpecializedRowMapper() and would call a new Predicate<Field> mappedFieldFilter in ReflectionMappers.

@stevenschlansker
Copy link
Member

Thanks for reporting this. I think we should filter out static fields by default.

@stevenschlansker
Copy link
Member

A fix for this issue (ignore all static fields) should be in the next release.

stevenschlansker added a commit that referenced this issue Jan 24, 2024
stevenschlansker added a commit that referenced this issue Jan 24, 2024
@mvysny
Copy link
Author

mvysny commented Jan 25, 2024

Awesome, thank you so much for such a quick fix; and super-thanks for such an awesome library 👍

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

Successfully merging a pull request may close this issue.

2 participants