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 🙂
Thanks 🙂
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”));
}
}
}
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!
Sol1 apply to the class level, pls provide your code.
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
It caused by the generic data type is undefined in your List.
Solution ..
1. Add @SuppressWarnings(“unchecked”) to ignore the checking.
2. Declare the data type for your List
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”));
}
}
}