/* I made this paste in haste of an error in NetBeans 8.0 so this will indeed be unstable ;) */ package game.debug; import java.lang.reflect.*; import java.util.Arrays; import java.util.Date; /** * This is a debugger. It makes life easier. * * @author Link_The_Programmer * @version 1.1 * * =name changed from Initializer to Debugger * =name changed from method initialize(Object obj) to parseObject * =name changed from method initialize(Object[] obj) to parseData * -removed redundancies (some things were already done by the JVM) * -(volatile Exception irrelevant) */ public final class Debugger { private static final String version = "1.1"; private static transient Exception e; private static Object obj; private static transient int numOfObjects = 0; private static transient Date date = new Date(); static { System.out.println("~" + Debugger + ".java version " + version); System.out.println(); } Debugger() { } //[Constructor] public Debugger(Object obj) { parseObject(obj); System.out.println(obj + " initialized."); } /** * * @param obj * @throws ClassNotFoundException * @throws NoSuchFieldException * @throws Exception */ public Debugger(Object[] obj) throws Exception { int mill1; int mill2; System.out.println("--[" + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + ":" + System.currentTimeMillis() / date.getSeconds() + "] Parsing objects..."); mill1 = (int) (System.currentTimeMillis() / date.getSeconds()); parseData(obj); System.out.println("--[" + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + ":" + System.currentTimeMillis() / date.getSeconds() + "] Objects data parsed. Total quantity: " + numOfObjects); mill2 = (int) (mill1 - System.currentTimeMillis() / date.getSeconds()); System.out.println("--Time Elapsed: " + Math.abs(mill2) + " millisecond(s)"); System.out.println(); for (Object object : obj) { if (object == null) { System.err.println(e.getStackTrace()[e.getStackTrace().length - 1].getClassName() + ".Field" + Arrays.toString(e.getStackTrace()[e.getStackTrace().length - 1].getClassName().getBytes()) + "] was not parsed; value = " + e.getLocalizedMessage()); } } } //[/Constructor] /** * Parses a single Object. * * @param obj */ private synchronized void parseObject(Object obj) { if (obj instanceof Field || obj instanceof Class) { if (obj instanceof Field) { Field.setAccessible((AccessibleObject[]) obj, true); System.out.println(obj + " initialized. value: " + getFieldValue((Field) obj)); numOfObjects += 1; Field.setAccessible((AccessibleObject[]) obj, false); } else { System.out.println(((Class) obj).getCanonicalName() + " initialized."); numOfObjects += 1; } } else { System.out.println(getContainingClass(obj) + ".Field" + Arrays.toString(e.getStackTrace()[getStackSize() - 1].getClassName().getBytes()) + "] initialized. value: " + "(" + getInstanceAsString(obj) + ")" + obj); numOfObjects += 1; } } /** * Parses an array of objects. * * * @param obj * @return * @throws ClassNotFoundException * @throws NoSuchFieldException * @throws Exception */ private synchronized void parseData(Object[] obj) throws ClassNotFoundException, NoSuchFieldException, Exception { try { throw new Exception(); } catch (Exception ex) { Debugger.e = ex; for (Object object : obj) { parseObject(object); } } } /** * Gets a field value specified by the parameter obj. * * @param obj * @return */ @SuppressWarnings("BoxingBoxedValue") public static Object getFieldValue(Object obj) { try { if (!(obj instanceof Class)) { Field f = (Field) obj; if (f.getType().isPrimitive() || f.getType().isSynthetic() || (f.getType() == String.class || f.getType() == Character.class) || f.getType() == Boolean.class) { return f.get(f); } else { return null; } } } catch (IllegalArgumentException | IllegalAccessException ex) { } return null; } /** * Gets the last value that the Initializer set a value for. * * @return the last value that was set */ public Object getLastValueSet() { return obj; } /** * * @param clss * @return */ public static Object[] getClasses(Class clss) { return clss.getDeclaredClasses(); } /** * * @param clss * @return */ public static Object[] getFields(Class clss) { return clss.getDeclaredFields(); } /** * * @return */ public static int getNumOfObjects() { return numOfObjects; } /** * Gets the stack object length. The stack object is Initializer.e (private * field) * * @return the stack size */ @SuppressWarnings("BoxingBoxedValue") public static int getStackSize() { if (e != null) { return e.getStackTrace().length; } return 0; } /** * Gets the StackTrace context of this object. * * @return Exception e which is the Exception that has the stack being used. */ public static Exception getStackContext() { return Debugger.e; } /** * Gets the type of an object. * * @param obj * @return the type of obj * @throws NoSuchFieldException */ public static Object getInstance(Object obj) throws NoSuchFieldException { return ((Class) obj).getTypeName(); } /** * Gets the type of an Object and returns it as a String object. * * @param obj * @return the instance as a String object */ public static String getInstanceAsString(Object obj) { return obj.getClass().getTypeName(); } /** * Gets the class in which the object is declared in. This is equivalent to * @code{object.getClass().getDeclaringClass()} except it uses the stacktrace * in order to find that class. * * @param obj * @return the class which the object is declared in. */ public static Class getContainingClass(Object obj) { try{ throw new Exception(); } catch(Exception ex){ e = ex; return e.getStackTrace()[getStackSize() - 1].getClassName().getClass().getDeclaringClass(); } } /** * Gets the version of Initializer.java * * @return the version */ public static String getVersion() { return version; } }