A bug in the JAVA7 environment in which the spring container deserializes the locale object. __java

Source: Internet
Author: User
Tags getscript locale


When you deserialize an object in spring, you need to call the Readresolve method to verify the integrity of the object. For the locale object of JAVA6, the concrete implementation

Private Object Readresolve () throws Java.io.ObjectStreamException {return
        getinstance (language, country, variant) ;
}

No problem. But for JAVA7 's real

Private Object Readresolve () throws Java.io.ObjectStreamException {return
           getinstance (baselocale.getlanguage), Baselocale.getscript (), Baselocale.getregion (), Baselocale.getvariant (),
                  localeextensions);

We know that the locale object that invokes the Readresolve method is the Hessian implementation in the spring framework, is generated directly in memory using the Sun.misc.UnSafe Allocateinstance method, and then through ReadObject (Abstracthessianinput in, Object obj,string[) FieldNames) assigns a value to the field of this object and does not construct it by invoking the new method of the object, which is plainly not constructed by means of the Java general construction method. So here's the problem.

In Java7, the locale baselocale field is a transient field that is not serialized and is dynamically constructed from several other fields in the object's construction method.

Public Locale (String language, String country, string variant) {
            if (language== null | | country = NULL | | | variant = = NULL) {
               	throw new NullPointerException ();
            }
            Baselocale = Baselocale.getinstance (convertoldisocodes (language), "", country, variant);
            Localeextensions = getcompatibilityextensions (Language, "", country, variant);
}


So through the implementation of spring above, the Baselocale field of the locale object calling the Readresolve object cannot be initialized, it can only be null, so

Return getinstance (Baselocale.getlanguage (), Baselocale.getscript (),
                  baselocale.getregion (), Baselocale.getvariant (), localeextensions);
This will definitely throw a null pointer exception.


The repair of this problem does not go to hack hessian implementation, do not put locale directly into the serialization of deserialization, the best conversion is also string, when taken out and then back to locale according to the string.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.