DBFieldHandler.java: Fixed sonar issue 39/64739/1
authorArundathi Patil <arundpil@in.ibm.com>
Wed, 5 Sep 2018 13:15:21 +0000 (18:45 +0530)
committerIBM602-PC0F1E3C\Arundathi <arundpil@in.ibm.com>
Wed, 5 Sep 2018 13:15:41 +0000 (18:45 +0530)
Fixed sonar code-smells/issues across this file

Issue-ID: DMAAP-731
Change-Id: Id4546d844be4818f605bfce42bc87a0ce05e2604
Signed-off-by: Arundathi Patil <arundpil@in.ibm.com>
src/main/java/org/onap/dmaap/dbcapi/database/DBFieldHandler.java

index 072c548..59d610c 100644 (file)
@@ -24,7 +24,6 @@ import java.lang.reflect.*;
 import java.sql.*;
 import java.util.*;
 
-import org.apache.log4j.Logger;
 
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
@@ -33,7 +32,40 @@ import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
 
 
 public class DBFieldHandler    {
-       static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
+       static final EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();       
+
+       public DBFieldHandler(Class<?> c, String fieldname, int fieldnum) throws Exception {
+               this(c, fieldname, fieldnum, null);
+       }
+       public DBFieldHandler(Class<?> c, String fieldname, int fieldnum, SqlOp op) throws Exception {
+               this.fieldnum = fieldnum;
+               StringBuilder sb = new StringBuilder();
+               for (String s: fieldname.split("_")) {  
+                       sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1));
+               }
+               String camelcase = sb.toString();
+               try {
+                       objget = c.getMethod("is" + camelcase);
+               } catch (Exception e) {
+                       errorLogger.error("Error", e);
+                       objget = c.getMethod("get" + camelcase);
+               }
+               objset = c.getMethod("set" + camelcase, objget.getReturnType());
+               sqlop = op;
+               if (sqlop != null) {
+                       return;
+               }
+               Class<?> x = objget.getReturnType();
+               if (x.isEnum()) {
+                       sqlop = new EnumSql(x);
+                       return;
+               }
+               sqlop = sqltypes.get(x.getName());
+               if (sqlop != null) {
+                       return;
+               }
+               errorLogger.error(DmaapbcLogMessageEnum.DB_NO_FIELD_HANDLER,  c.getName(),  fieldname,  Integer.toString(fieldnum),  x.getName());
+       }
        
        public static interface SqlOp   {
                public Object get(ResultSet rs, int index) throws Exception;
@@ -121,7 +153,7 @@ public class DBFieldHandler {
         }
         private static Map<String, SqlOp> sqltypes;
         static {
-                sqltypes = new HashMap<String, SqlOp>();
+                sqltypes = new HashMap<>();
                sqltypes.put("[Ljava.lang.String;", new AofString());
                sqltypes.put("java.util.Date", new SqlDate());
                 try {
@@ -134,6 +166,7 @@ public class DBFieldHandler {
                         new SqlType("Short");
                         new SqlType("String");
                 } catch (Exception e) {
+                       errorLogger.error("Error", e);
                        errorLogger.error(DmaapbcLogMessageEnum.DB_ACCESS_INIT_ERROR,  e.getMessage() );
                 }
         }
@@ -156,37 +189,6 @@ public class DBFieldHandler        {
        public void fromSQL(ResultSet r, Object o) throws Exception {
                objset.invoke(o, sqlop.get(r, fieldnum));
        }
-       public DBFieldHandler(Class<?> c, String fieldname, int fieldnum) throws Exception {
-               this(c, fieldname, fieldnum, null);
-       }
-       public DBFieldHandler(Class<?> c, String fieldname, int fieldnum, SqlOp op) throws Exception {
-               this.fieldnum = fieldnum;
-               StringBuffer sb = new StringBuffer();
-               for (String s: fieldname.split("_")) {
-                       sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1));
-               }
-               String camelcase = sb.toString();
-               try {
-                       objget = c.getMethod("is" + camelcase);
-               } catch (Exception e) {
-                       objget = c.getMethod("get" + camelcase);
-               }
-               objset = c.getMethod("set" + camelcase, objget.getReturnType());
-               sqlop = op;
-               if (sqlop != null) {
-                       return;
-               }
-               Class<?> x = objget.getReturnType();
-               if (x.isEnum()) {
-                       sqlop = new EnumSql(x);
-                       return;
-               }
-               sqlop = sqltypes.get(x.getName());
-               if (sqlop != null) {
-                       return;
-               }
-               errorLogger.error(DmaapbcLogMessageEnum.DB_NO_FIELD_HANDLER,  c.getName(),  fieldname,  Integer.toString(fieldnum),  x.getName());
-       }
        public static String fesc(String s) {
                if (s == null) {
                        return(s);