Annotations are a very important part of Java in modern technologies, Most of the technologies such as Hibernate, Spring, Spring Boot, JPA, and so Many other Libraries are using annotations and making developers’ life a lot easy. In Java, built-in General Annotations are –
- @Override
- @Deprecated
- @FunctionalInterface
- @SuppressWarnings
Syntax: The signature for Java @SuppressWarnings annotation is as follows:
@Retention(value=SOURCE) @Target(value = {TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE }) public @interface SuppressWarnings { String[] value; }
As we can see, the above signature has only one element, which is Array of String, with multiple possible values.
All annotations have two properties :
- Target (@Target(value = {TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE })) – It will be used with almost everything, wherever you want to suppress warnings.
- Retention (@Retention(value=SOURCE)): Retention policy of functional Interface “SOURCE”, which means annotation won’t go till compiler.
Illustrations:
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. (All methods, variables, constructors).
2. public class Calculator { @SuppressWarnings("unchecked") public int sum(x,y) { . } } - It will stop warning from that function only, and not from other functions of Calculator class.
This annotation is dangerous because a warning is something potentially wrong with the code. So if we’re getting any warning, the first approach should be resolving those errors. But if we’re suppressing any warnings, we have to have some solid reason. The reason should be commented near to the annotation every time it is used.
Possible Values Inside @SuppressWarnings Annotation Element are as follows:
Values |
Description |
---|---|
All | It will suppress all warnings. |
Cast | Suppress the warning while casting from a generic type to a nonqualified type or the other way around. |
Deprecation | Ignores when we’re using a deprecated(no longer important) method or type. |
divzero | Suppresses division by zero warning. |
empty | Ignores warning of a statement with an empty body. |
unchecked | It doesn’t check if the data type is Object or primitive. |
fallthrough | Ignores fall-through on switch statements usually (if “break” is missing). |
hiding | It suppresses warnings relative to locals that hide variable |
serial | It makes the compiler shut up about a missing serialVersionUID. |
finally | Avoids warnings relative to finally block that doesn’t return. |
unused | To suppress warnings relative to unused code. |
Note: The primary and most important benefit of using @SuppressWarnings Annotation is that if we stuck because of some known warning, then this will ignore the warning and move ahead. E.g. – deprecated and unchecked warnings.
Example:
Java
// Java Program to demonstrate Use of @SuppressWarnings // Annotation // Importing required packages import java.io.*; import java.lang.*; import java.util.*; // Class 1 // Helper class class Addition { // Method 1 public static int sum( int n1, int n2) { // Return the final sum return n1 + n2; } // Method 2 public static int sum( int ... nums) { int sum = 0 ; for ( int i : nums) { sum += i; } // Return the final sum return sum; } } // Class 2 // Main class // To test suppress warnings public class GFG { // Does not check if data type is Object or primitive @SuppressWarnings ( "unchecked" ) // Main driver method public static void main(String[] args) { // Creating an object of above class in main() // method Addition add = new Addition(); // Ignore when we're using a deprecated // (no longer important) method or type @SuppressWarnings ( "deprecation" ) int sum = Addition.sum( 10 , 20 ); // Print and display the sum System.out.println( "Sum of 10 and 20 : " + sum); @SuppressWarnings ( "rawtypes" ) // Raw data type being used instead of generic List list = new ArrayList(); // Custom input entries list.add( 12 ); list.add( 120 ); // Print and display List elements System.out.println( "List items : " + list); } } |
Sum of 10 and 20 : 30 List items : [12, 120]