All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.jmatrix.db.common.DebugUtils Maven / Gradle / Ivy

The newest version!
package net.jmatrix.db.common;

import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;



/**
 * A series of utility methods used for debugging and logging.
 */
public class DebugUtils {
   private static boolean debug = Boolean.valueOf(System.getProperty("debug", "false"));

   public static int MAX_LENGTH=65;
   
   public static final String debugString(Object o) {
      return debugString(o, 0);
   }
   
   /** 
    * The debugString() method uses introspection to write the
    * state of an object based on its public getters.
    */
   public static final String debugString(Object obj, int depth) {
      if (obj == null)
         return "null";
      StringBuilder sb=new StringBuilder();
      
      Method methods[]=obj.getClass().getMethods();
      //System.out.println ("There are "+fields.length+" fields in "+this.getClass());
      Method method=null;
      
      String pad=padString((depth*3)+3, " ");
      try {
         sb.append(pad+"getClass: "+obj.getClass().getName()+"\n");
         
         for (int i=0; i 0) {
//            sb.append(", type="+list.get(0).getClass().getName());
//         }
//         sb.append("]");
//         
//         return truncate(sb.toString());
         return list.toString();
      } else if (obj instanceof String) {
         return truncate(obj.toString());
      } else if (obj.getClass().getName().toLowerCase().contains("domain") ||
            obj.getClass().getName().toLowerCase().contains("mongo.test")||
            obj.getClass().getName().toLowerCase().contains("etws")) {
         return "\n"+debugString(obj, depth+1);
      } else {
         return truncate(obj.toString());
      }
   }
   
   /** */
   public static final String truncate(String s) {
      return truncate(s, MAX_LENGTH);
   }
   
   /** */
   public static final String truncate(String s, int len) {
      if (s == null)
         return "null";
      
      if (s.length() > len) {
         return s.substring(0, len)+"...";
      }
      return s;
   }
   
   /** */
   public static final String splitString(String s, String prefix, 
         String suffix, int chunk) {
      
      if (s.length() < chunk) {
         return prefix+s+suffix;
      }
      
      StringBuilder split=new StringBuilder();
      int chunks=(int)Math.ceil((double)s.length()/(double)chunk);
      //System.out.println ("s.length() = "+s.length()+" chunk="+chunk);
      //System.out.println ("Chunks="+chunks);
      
      for (int i=0; i= len) 
         return s;
      int padchars=len - s.length();
      StringBuilder sb=new StringBuilder();
      sb.append(s);
      for (int i=0; i j.l.NullPointerException
    */
   public static final String abbreviateClassname(Class c) {
      if (c == null)
         return "null";
      String exc=c.getName();
      String split[]=exc.split("\\.");
      StringBuilder sb=new StringBuilder();
      for (int i=0; i "+i+":"+className+"."+stack[i].getMethodName());
         if (className != null && className.equals(calledClassName))
            found=true;
         else if (found)
            break;
      }
      if (i "+i+":"+className+"."+stack[i].getMethodName());
         return className;
      }
      return null;
   }
   
   /** */
   public static final String formatAsHtml(String s) {
      s=s.replace("<", "<");
      s=s.replace(">", ">");

      s=s.replace("\n", "
\n"); return s; } /** */ public static final String formatAsHtml(Throwable t) { ByteArrayOutputStream baos=new ByteArrayOutputStream(); PrintWriter pw=new PrintWriter(new OutputStreamWriter(baos)); t.printStackTrace(pw); pw.flush(); String stack=baos.toString(); stack=stack.replace("<", "<"); stack=stack.replace(">", ">"); return "
\n"+stack+"\n
\n"; } public static final String jsonDebug(Object o) { return jsonDebug(o, true); } public static final String jsonDebug(Object o, boolean indent) { ObjectMapper om=new ObjectMapper(); om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); om.enable(SerializationFeature.INDENT_OUTPUT); try { if (o == null) return "null"; String result = om.writeValueAsString(o); result.replaceAll("([pP]assword[ =\":]+)[^\",]*([,\"])", "$1******$2"); return result; } catch (Exception ex) { throw new RuntimeException("Error in debug serialization.", ex); } } public static final void sleep(long millis) { try { Thread.sleep(millis); } catch (Exception ex) {} } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy