Main Tutorials

How to suppress unchecked warnings – Java

The ‘unchecked warnings’ is quite popular warning message in Java. However, if you insist this is an invalid warning, and there are no ways to solve it without compromising the existing program functionality. You may just use @SuppressWarnings(“unchecked”) to suppress unchecked warnings in Java.

1. In Class

If applied to class level, all the methods and members in this class will ignore the unchecked warnings message.


@SuppressWarnings("unchecked")
public class classA{...}

2. In Method

If applied to method level, only this method will ignore the unchecked warnings message.


@SuppressWarnings("unchecked")
private void method1(){...}

3. In Property

If applied to property level, only this property will ignore the unchecked warnings message.


@SuppressWarnings("unchecked")
private List list1;

As conclusion, suppress an unchecked warning is like hiding a potential bug, it’s better to find the cause of the unchecked warning and fix it 🙂

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
7 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
M Chandry
2 years ago

Thanks 🙂

veeeer
5 years ago

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.stringtemplate.v4.ST;

public class JSONDemoR3 {
// Compare files\
// User File
static String baseDir = “C:\\Json\\”;
static String userJsonFilePath = baseDir + “UserReqest.txt”;
// Kibana file
static String kibanaJsonFilePath = baseDir + “kibana_mapper.txt”;

static Map mapContentUserRequestKeyValue = new HashMap();

public static void main(String args[]) throws IOException {
compareJsonFile();
}

static String readKibanaMapperFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}

public static void compareJsonFile() throws IOException {
// User File
readSectionJson(userJsonFilePath);
String content = readKibanaMapperFile(kibanaJsonFilePath, StandardCharsets.UTF_8);
ST renderJson = new ST(content);
for(Map.Entry map : mapContentUserRequestKeyValue.entrySet()) {
renderJson.add(map.getKey(), map.getValue());
}
generateUserRequestFile(renderJson.render());

}

private static void generateUserRequestFile(String userRequestFileJson) throws IOException {
try (FileWriter file = new FileWriter(baseDir + “User_Request__” + System.currentTimeMillis() + “.json”)) {
file.write(userRequestFileJson);
System.out.println(“User Request Json file has been generated successfully…”);
}
}
private static void readSectionJson(String file) {
JSONParser parser = new JSONParser();
try {
FileReader fileReader = new FileReader(file);
JSONObject sourceJson = (JSONObject) parser.parse(fileReader);
JSONArray jsonSectionArray = fetchSectionArray(sourceJson);
JSONObject sectionJson = null;
JSONArray fieldsJsonAry = null;
@SuppressWarnings(“unchecked”)
Iterator arrayIterator = jsonSectionArray.iterator();
while (arrayIterator.hasNext()) {
sectionJson = (JSONObject) arrayIterator.next();
fieldsJsonAry = fetchFieldsArray(sectionJson);
iteratorFields(fieldsJsonAry);
}

} catch (Exception expReadJson) {
expReadJson.printStackTrace();
}
}

private static JSONArray fetchSectionArray(JSONObject json) {
return (JSONArray) json.get(“sections”);
}

private static JSONArray fetchFieldsArray(JSONObject sectionJson) {
return (JSONArray) sectionJson.get(“fields”);
}

private static void iteratorFields(JSONArray jsonFieldArray) {
JSONObject fieldJson = null;
@SuppressWarnings(“unchecked”)
Iterator arrayIterator = jsonFieldArray.iterator();
while (arrayIterator.hasNext()) {
fieldJson = (JSONObject) arrayIterator.next();
mapContentUserRequestKeyValue.put(fieldJson.get(“id”).toString(), fieldJson.get(“value”));
}
}
}

kumar
13 years ago

Greetings!
Thanks for the solution
2 solution suited for applctn
when i tried to use @SuppressWarnings(“unchecked”) it still says previous uncheked warning why!

give me more briefing on sol1 and sol2
if sol1 used any problem to the desired result?
thanks in advance!

maninder
14 years ago

sir i am working on jdk ver 6.0 I am getting a type unchecked warning when i am compiling a code what to do to remove a error

code

package com.example.model;
import java.util.*;
import java.lang.*; 

public class BeerExpert 
{
public List getBrands(String color)  {
List brands = new ArrayList();
if (color.equals("amber")){
brands.add("Jack Amber ");
brands.add("Red Moose");

}
else{
brands.add("Jail Pale Ale");
brands.add("Gout Stout");
}
return(brands);
}
}
veer
5 years ago
Reply to  mkyong

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.stringtemplate.v4.ST;

public class JSONDemoR3 {
// Compare files\
// User File
static String baseDir = “C:\\Json\\”;
static String userJsonFilePath = baseDir + “UserReqest.txt”;
// Kibana file
static String kibanaJsonFilePath = baseDir + “kibana_mapper.txt”;

static Map mapContentUserRequestKeyValue = new HashMap();

public static void main(String args[]) throws IOException {
compareJsonFile();
}

static String readKibanaMapperFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}

public static void compareJsonFile() throws IOException {
// User File
readSectionJson(userJsonFilePath);
String content = readKibanaMapperFile(kibanaJsonFilePath, StandardCharsets.UTF_8);
ST renderJson = new ST(content);
for(Map.Entry map : mapContentUserRequestKeyValue.entrySet()) {
renderJson.add(map.getKey(), map.getValue());
}
generateUserRequestFile(renderJson.render());

}

private static void generateUserRequestFile(String userRequestFileJson) throws IOException {
try (FileWriter file = new FileWriter(baseDir + “User_Request__” + System.currentTimeMillis() + “.json”)) {
file.write(userRequestFileJson);
System.out.println(“User Request Json file has been generated successfully…”);
}
}
private static void readSectionJson(String file) {
JSONParser parser = new JSONParser();
try {
FileReader fileReader = new FileReader(file);
JSONObject sourceJson = (JSONObject) parser.parse(fileReader);
JSONArray jsonSectionArray = fetchSectionArray(sourceJson);
JSONObject sectionJson = null;
JSONArray fieldsJsonAry = null;
@SuppressWarnings(“unchecked”)
Iterator arrayIterator = jsonSectionArray.iterator();
while (arrayIterator.hasNext()) {
sectionJson = (JSONObject) arrayIterator.next();
fieldsJsonAry = fetchFieldsArray(sectionJson);
iteratorFields(fieldsJsonAry);
}

} catch (Exception expReadJson) {
expReadJson.printStackTrace();
}
}

private static JSONArray fetchSectionArray(JSONObject json) {
return (JSONArray) json.get(“sections”);
}

private static JSONArray fetchFieldsArray(JSONObject sectionJson) {
return (JSONArray) sectionJson.get(“fields”);
}

private static void iteratorFields(JSONArray jsonFieldArray) {
JSONObject fieldJson = null;
@SuppressWarnings(“unchecked”)
Iterator arrayIterator = jsonFieldArray.iterator();
while (arrayIterator.hasNext()) {
fieldJson = (JSONObject) arrayIterator.next();
mapContentUserRequestKeyValue.put(fieldJson.get(“id”).toString(), fieldJson.get(“value”));
}
}
}