diff options
author | Parshad Patel <pars.patel@samsung.com> | 2018-08-22 11:09:49 +0900 |
---|---|---|
committer | Parshad Patel <pars.patel@samsung.com> | 2018-08-23 13:09:54 +0900 |
commit | 718ebdc7cab68315bfa9d622d3b6932221920dee (patch) | |
tree | cb247f5928ce701099fb1fb9e98b3675d70cd80d | |
parent | 3b0d3167a2ad458bbe59e0f703b40ef4f4976424 (diff) |
Fix sonar issues
Fix use try-with-resources issues
Issue-ID: AAI-1368
Change-Id: I267df61915ac1596cc8645c62ccf43eb04fd7ac5
Signed-off-by: Parshad Patel <pars.patel@samsung.com>
3 files changed, 20 insertions, 23 deletions
diff --git a/aai-core/src/main/java/org/onap/aai/util/PojoUtils.java b/aai-core/src/main/java/org/onap/aai/util/PojoUtils.java index 8354d841..f3e67388 100644 --- a/aai-core/src/main/java/org/onap/aai/util/PojoUtils.java +++ b/aai-core/src/main/java/org/onap/aai/util/PojoUtils.java @@ -162,16 +162,17 @@ public class PojoUtils { * @param clazz the clazz * @return the xml from object * @throws JAXBException the JAXB exception + * @throws IOException */ - public <T> String getXmlFromObject(T clazz) throws JAXBException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - JAXBContext jc = JAXBContext.newInstance(clazz.getClass().getPackage().getName()); - - Marshaller marshaller = jc.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - marshaller.marshal(clazz, baos); - - return baos.toString(); + public <T> String getXmlFromObject(T clazz) throws JAXBException, IOException { + try(ByteArrayOutputStream baos = new ByteArrayOutputStream()){ + JAXBContext jc = JAXBContext.newInstance(clazz.getClass().getPackage().getName()); + + Marshaller marshaller = jc.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + marshaller.marshal(clazz, baos); + return baos.toString(); + } } /** diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/YAMLfromOXM.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/YAMLfromOXM.java index 930e2fb2..f90d54e6 100644 --- a/aai-core/src/main/java/org/onap/aai/util/genxsd/YAMLfromOXM.java +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/YAMLfromOXM.java @@ -111,7 +111,7 @@ public class YAMLfromOXM extends OxmFileProcessor { try { init(); } catch(Exception e) { - logger.error( "Error initializing " + this.getClass()); + logger.error( "Error initializing " + this.getClass(),e); throw e; } pathSb.append(getDocumentHeader()); @@ -441,7 +441,7 @@ public class YAMLfromOXM extends OxmFileProcessor { results.get(key).stream().filter((i) -> (i.getFrom().equals(xmlRootElementName) && (! i.isPrivateEdge() && i.getPreventDelete().equals("OUT")))).forEach((i) ->{ preventDelete.add(i.getTo().toUpperCase());} ); } } catch(Exception e) { - logger.debug("xmlRootElementName: "+xmlRootElementName+"\n"+e); + logger.debug("xmlRootElementName: "+xmlRootElementName+"\n",e); } try { EdgeRuleQuery q1 = new EdgeRuleQuery.Builder(xmlRootElementName).version(v).toOnly().build(); @@ -454,7 +454,7 @@ public class YAMLfromOXM extends OxmFileProcessor { results.get(key).stream().filter((i) -> (i.getTo().equals(xmlRootElementName) && (! i.isPrivateEdge() && i.getPreventDelete().equals("IN")))).forEach((i) ->{ preventDelete.add(i.getFrom().toUpperCase());} ); } } catch(Exception e) { - logger.debug("xmlRootElementName: "+xmlRootElementName+"\n"+e); + logger.debug("xmlRootElementName: "+xmlRootElementName+"\n",e); } if(preventDelete.size() > 0) { prevent = xmlRootElementName.toUpperCase()+" cannot be deleted if related to "+String.join(",",preventDelete); @@ -505,7 +505,7 @@ public class YAMLfromOXM extends OxmFileProcessor { javaTypeDefinitions.put(xmlRootElementName, definitionsLocalSb.toString()); } } catch (Exception e) { - e.printStackTrace(); + logger.error("Exception adding in javaTypeDefinitions",e); } if ( xmlRootElementName.equals("inventory") ) { logger.trace("skip xmlRootElementName(2)="+xmlRootElementName); @@ -535,21 +535,16 @@ public class YAMLfromOXM extends OxmFileProcessor { try { outfile.createNewFile(); } catch (IOException e) { - logger.error( "Exception creating output file " + outfileName); - e.printStackTrace(); + logger.error( "Exception creating output file " + outfileName,e); } - BufferedWriter bw = null; try { Charset charset = Charset.forName("UTF-8"); Path path = Paths.get(outfileName); - bw = Files.newBufferedWriter(path, charset); - bw.write(fileContent); - if ( bw != null ) { - bw.close(); + try(BufferedWriter bw = Files.newBufferedWriter(path, charset)){ + bw.write(fileContent); } } catch ( IOException e) { - logger.error( "Exception writing output file " + outfileName); - e.printStackTrace(); + logger.error( "Exception writing output file " + outfileName,e); } } diff --git a/aai-core/src/test/java/org/onap/aai/util/PojoUtilsTest.java b/aai-core/src/test/java/org/onap/aai/util/PojoUtilsTest.java index 3658704e..35065fd5 100644 --- a/aai-core/src/test/java/org/onap/aai/util/PojoUtilsTest.java +++ b/aai-core/src/test/java/org/onap/aai/util/PojoUtilsTest.java @@ -23,6 +23,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import java.io.IOException; import java.time.LocalDateTime; import java.time.Month; import java.util.ArrayList; @@ -159,7 +160,7 @@ public class PojoUtilsTest { } @Test - public void testGetXmlFromObject() throws JAXBException { + public void testGetXmlFromObject() throws JAXBException, IOException { NotificationEvent notificationEvent = new NotificationEvent(); notificationEvent.setCambriaPartition("partition"); |