Skip to content

fix java.lang.IllegalAccessException: Field is final #16

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

Closed
wants to merge 1 commit into from

Conversation

fengxx
Copy link
Contributor

@fengxx fengxx commented Mar 15, 2013

Signed-off-by: Ted Shaw [email protected]
AutoMapping is default to PARTIAL which may map column to static final field by name, and a <java.lang.IllegalAccessException: Field is final> error will throw out. The pull request fixes the issue and attaches test cases to verify "final static" and "final" setter

@emacarron
Copy link
Member

Hi Ted!

First of all thanks for the contribution!!

I am not sure the change is right for two reasons:

@fengxx
Copy link
Contributor Author

fengxx commented Mar 16, 2013

Hi emacarron,
Thanks for the prompt reply. I am sorry I should make it more clear in first place. I am not adding checks of final which were removed by #issue 379, what I did is to add checks for final static, final (non static) values can be set by Mybatis because it is instance level, but final static values can only be set by class loader.

Below is my answer for the concerns

If the user specified a property, there is no problem in calling the setter without checking if that property exist.

autoMappingBehavior default to PARTIAL, Mybatis try to map column to bean property by name when it is not specified. e.g. the test case included in the pull request

    <resultMap type="org.apache.ibatis.submitted.automapping.Book" id="bookResult">
         <result property="name" column="name"/>
    </resultMap>

    <select id="getBooks" resultMap="bookResult">
        select version,name from books
    </select>

Book has a final static field called version, there will be "java.lang.IllegalAccessException: Field is final" throw out when getBooks executed. This mostly happens when user use select * in SQL and column name conflicts with bean's final static field name.

Seems the check of finals was intentionally removed for issue #379 so why whould we get it back?

The pull request will not break this, another test case in the pull request

        public class Article {
            public final Integer version=0; 
        }

        <select id="getArticle" resultType="org.apache.ibatis.submitted.automapping.Article">
                select 9 as version from INFORMATION_SCHEMA.SYSTEM_USERS
        </select>

        Article article = mapper.getArticle();
        Assert.assertTrue("should update version in mapping", article.version > 0);

@emacarron emacarron closed this in 1712d69 Mar 17, 2013
@emacarron
Copy link
Member

Thanks both for the explanation and the contribution Ted!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve a feature or add a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants