[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-6215) final fields can't be set if they're public

Bogdan Butnaru (JIRA) noreply at atlassian.com
Wed May 11 05:24:59 EDT 2011


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-6215?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=42283#action_42283 ] 

Bogdan Butnaru commented on HHH-6215:
-------------------------------------

Minor update: I tested the proposed solution by making a copy of {{org.hibernate.property.DirectPropertyAccessor}} inside my project, so that it overrides the one in the Hibernate jar. (Dangerous hack, I know.) At least for the tests I did, replacing the condition with
{noformat} 
(!ReflectHelper.isPublic(clazz, field) || Modifier.isFinal(field.getModifiers()))
{noformat}
makes things also work with {{public final}} fields.


> final fields can't be set if they're public
> -------------------------------------------
>
>                 Key: HHH-6215
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-6215
>             Project: Hibernate Core
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 3.6.4
>         Environment: Hibernate 3.6.4
>            Reporter: Bogdan Butnaru
>
> As far as I can tell, Hibernate 3.6.4 can set a {{final}} field, _except_ if both the field and the class it belongs to are declared {{public}}. In other words,
> {noformat}
> public class Test{
> 	@Column(name = "field")
> 	private final String field = null;
> }
> {noformat}
> will work, as will making the class non-{{public}} and the field {{public}}, but 
> {noformat}
> public class Test{
> 	@Column(name = "field")
> 	public final String field = null;
> }
> {noformat}
> will not. The latter causes an {{org.hibernate.PropertyAccessException}} to be thrown when a {{Test}} object is initialized from the database (caused by a {{java.lang.IllegalAccessException}}).
> (The message I get is _"could not set a field value by reflection setter of one.of.my.Classes.field at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:151)"_)
> I looked a bit around the sources for 3.6.4 (which I downloaded earlier today) and did a little debugging, and as far as I can tell the problem is in {{DirectPropertyAccessor.getField(Class, String)}}; Specifically, line 176 reads
> {noformat}
> if ( !ReflectHelper.isPublic(clazz, field) ) field.setAccessible(true);
> {noformat}
> that is, {{setAccessible}} is not called for {{public}} fields even if they are {{final}}. I don't know the code enough to be sure, but I believe the test should check for {{final}} too.
> There is an identical line in the next method (same name but with two {{Class}} arguments, line 191); I suspect that causes the same problem, but I didn't hit that code in my case (AFAIK because I don't have inherited fields).
> I'm sorry for not uploading a test case, I don't quite get how to check this, but I hope I pointed close enough to the problem.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list