Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@SuppressWarnings vs @SuppressLint

Can anyone explain to me the differences between @SuppressWarnings and @SuppressLint? When we should use one over another?

I've read the documentation, but still don't get the differences. Explain using an example/sample code will be much appreciated. Thanks.

like image 844
cartesify Avatar asked Oct 19 '16 08:10

cartesify


People also ask

What does @SuppressWarnings serial mean?

Using the annotation @SuppressWarnings("serial") makes the compiler shut up about a missing serialVersionUID . So yes, you can use that to get rid of the warning message, but a better solution is to make your class not implement Serializable (directly or indirectly) if it's not meant to be serialized.

What is @SuppressLint range?

android.annotation.SuppressLint. Indicates that Lint should ignore the specified warnings for the annotated element.

What is @SuppressWarnings resource?

Adding the @SuppressWarnings({ "resource" }) should remove the warning for a potential resource leak. Adding a list for other ones, it's for Eclipse but they should be fairly similar for reference.

What is the use of @SuppressWarnings in Java?

Use of @SuppressWarnings is to suppress or ignore warnings coming from the compiler, i.e., the compiler will ignore warnings if any for that piece of code. 1. @SuppressWarnings("unchecked") public class Calculator { } - Here, it will ignore all unchecked warnings coming from that class.


2 Answers

There are actually two lints: one belongs to the compiler, so is Java-specific, and one belongs to Google and is Android-specific.

If your warning is about something in Java that isn't specific to Android, it's suppressed with @SuppressWarnings, and if it's Android-specific, it's suppressed with @SuppressLint.

Android Lint Warnings

Lint warnings are listed here: http://tools.android.com/tips/lint-checks

So let's say you have a warning about missing permissions, and the warning description starts off "This check scans through your code and libraries and looks at the APIs being used, and checks this against the set of permissions required to access those APIs." In the lint warnings page linked above, we find this:

MissingPermission

Summary: Missing Permissions

Priority: 9 / 10 Severity: Error Category: Correctness

This check scans through your code and libraries and looks at the APIs being used, and checks this against the set of permissions required to access those APIs. If the code using those APIs is called at runtime, then the program will crash.

Furthermore, for permissions that are revocable (with targetSdkVersion 23), client code must also be prepared to handle the calls throwing an exception if the user rejects the request for permission at runtime.

So to suppress this, we put this annotation on the code:

@SuppressLint("MissingPermission")

Compiler Warnings

Let's say we find this warning:

"Unchecked cast: 'java.lang.Object' to 'java.lang.Integer' ..."

If you are reading this in hover popup in Android Studio, there is a More... link at the end. When you click the More... link, the text expands and you find this at the bottom:

"Hint: Pass -Xlint:unchecked to javac to get more details."

This tells you that you would use "unchecked" in the annotation like this:

@SuppressWarnings("unchecked")

For a list of compiler warnings, run javac -X:

C:\>javac -X
  -Xlint                     Enable recommended warnings
  -Xlint:{all,auxiliaryclass,cast,classfile,deprecation,dep-ann,divzero,empty,fallthrough,finally,options,overloads,overrides,path,processing,rawtypes,serial,static,try,unchecked,varargs,-auxiliaryclass,-cast,-classfile,-deprecation,-dep-ann,-divzero,-empty,-fallthrough,-finally,-options,-overloads,-overrides,-path,-processing,-rawtypes,-serial,-static,-try,-unchecked,-varargs,none} Enable or disable specific warnings
  .
  .
  .

Those are the values that you can use in @SuppressWarnings.

like image 145
kris larson Avatar answered Oct 16 '22 06:10

kris larson


Also, there is @Suppress (kotlin.Suppress) which suppresses Android warnings as well as @SuppressLint.

For example, @Suppress("AlwaysShowAction").

like image 31
Daniel Avatar answered Oct 16 '22 06:10

Daniel