Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet325/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   321   322   323   324   325   326   327   328   ...   341
Bog'liq
Effective Java



CHAPTER 12
SERIALIZATION
364
The presence of this method on the enclosing class causes the serialization system
to emit a 
SerializationProxy
instance instead of an instance of the enclosing
class. In other words, the 
writeReplace
method translates an instance of the
enclosing class to its serialization proxy prior to serialization.
With this 
writeReplace
method in place, the serialization system will never
generate a serialized instance of the enclosing class, but an attacker might
fabricate one in an attempt to violate the class’s invariants. To guarantee that such
an attack would fail, merely add this 
readObject
method to the enclosing class:
// readObject method for the serialization proxy pattern
private void readObject(ObjectInputStream stream) 
throws InvalidObjectException {
throw new InvalidObjectException("Proxy required");
}
Finally, provide a 
readResolve
method on the 
SerializationProxy
class
that returns a logically equivalent instance of the enclosing class. The presence of
this method causes the serialization system to translate the serialization proxy back
into an instance of the enclosing class upon deserialization.
This 
readResolve
method creates an instance of the enclosing class using
only its public API and therein lies the beauty of the pattern. It largely eliminates
the extralinguistic character of serialization, because the deserialized instance is
created using the same constructors, static factories, and methods as any other
instance. This frees you from having to separately ensure that deserialized
instances obey the class’s invariants. If the class’s static factories or constructors
establish these invariants and its instance methods maintain them, you’ve ensured
that the invariants will be maintained by serialization as well. 
Here is the 
readResolve
method for 
Period.SerializationProxy
above:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   321   322   323   324   325   326   327   328   ...   341




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish