Effective Java


with an  @SuppressWarnings("unchecked")



Download 2,19 Mb.
Pdf ko'rish
bet114/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   110   111   112   113   114   115   116   117   ...   341
Bog'liq
Effective Java

with an 
@SuppressWarnings("unchecked")
 annotation.
If you suppress warn-
ings without first proving that the code is typesafe, you are giving yourself a false
sense of security. The code may compile without emitting any warnings, but it can
still throw a 
ClassCastException
at runtime. If, however, you ignore unchecked
warnings that you know to be safe (instead of suppressing them), you won’t notice
when a new warning crops up that represents a real problem. The new warning
will get lost amidst all the false alarms that you didn’t silence.


CHAPTER 5
GENERICS
124
The 
SuppressWarnings
annotation can be used on any declaration, from an
individual local variable declaration to an entire class. 
Always use the
SuppressWarnings
 annotation on the smallest scope possible.
Typically this
will be a variable declaration or a very short method or constructor. Never use
SuppressWarnings
on an entire class. Doing so could mask critical warnings.
If you find yourself using the 
SuppressWarnings
annotation on a method or
constructor that’s more than one line long, you may be able to move it onto a local
variable declaration. You may have to declare a new local variable, but it’s worth
it. For example, consider this 
toArray
method, which comes from 
ArrayList
:
public  T[] toArray(T[] a) {
if (a.length < size)
return (T[]) Arrays.copyOf(elements, size, a.getClass());
System.arraycopy(elements, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
If you compile 
ArrayList
, the method generates this warning:
ArrayList.java:305: warning: [unchecked] unchecked cast
return (T[]) Arrays.copyOf(elements, size, a.getClass());
^
required: T[]
found: Object[]
It is illegal to put a 
SuppressWarnings
annotation on the return statement,
because it isn’t a declaration [JLS, 9.7]. You might be tempted to put the annota-
tion on the entire method, but don’t. Instead, declare a local variable to hold the
return value and annotate its declaration, like so:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   110   111   112   113   114   115   116   117   ...   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