_AMDG_ icon

Initializer.java Open-Source (0.9.5)

_AMDG_ | PRO | 05/17/14 06:58:49 AM UTC | 0 ⭐ | 686 👁️ | Never ⏰ | []
Java |

8.32 KB

|

None

|

0 👍

/

0 👎

package game.objects;
 
import java.lang.*;
import java.lang.reflect.*;
import java.util.Arrays;
import java.util.Date;
import jdk.nashorn.internal.runtime.Version;
 
/**
 * Why have your own initializer? Simple. This makes initialization of objects
 * much more manageable than just giving control to the JVM. Should the graphics
 * or actors be loaded first? You decide! Recommended for use when starting the
 * game.
 *
 * @author Link_The_Programmer
 * @version 0.9.5
 * @
 */
 
@SuppressWarnings("LocalVariableHidesMemberVariable")
public final class Initializer extends ClassLoader {
 
    private static transient volatile Exception e;
    private transient Object[] f;
    private static transient int numOfObjects = 0;
    private static transient Date date = new Date();
 
    private Initializer() {
    }
 
    //[Constructor]
    public Initializer(Object obj) throws ClassNotFoundException, NoSuchFieldException {
        initialize(obj);
        System.out.println(obj + " initialized.");
    }
 
    public Initializer(Object[] obj) throws ClassNotFoundException, NoSuchFieldException, Exception {
        System.out.println("[" + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "] Initializing objects...");
        initialize(obj);
        System.out.println("[" + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "] Objects initialized. Total quantity: " + numOfObjects);
        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()) + "] not initialized; value = " + e.getLocalizedMessage() + " at line: " + e.getStackTrace()[e.getStackTrace().length - 1].getLineNumber());
            }
        }
    }
 
    //[/Constructor]
    /**
     * Initializes an object. you must put the fully qualified name as the
     * parameter (complete package location with name) in order to work.
     *
     * @param obj
     * @return
     * @throws ClassNotFoundException
     */
    @SuppressWarnings("BoxingBoxedValue")
    private synchronized Object initialize(Object obj) throws NoSuchFieldException, ClassNotFoundException {
        if (obj instanceof Integer) {
            return new Integer(Integer.valueOf((Integer) obj));
        } else if (obj instanceof Byte) {
            return new Byte(Byte.valueOf((Byte) obj));
        } else if (obj instanceof Character) {
            return new Character(Character.valueOf((Character) obj));
        } else if (obj instanceof String) {
            return String.valueOf(obj);
        } else if (obj instanceof Boolean) {
            return Boolean.valueOf((Boolean) obj);
        } else if (obj instanceof Class) {
            return obj.getClass();
        } else if (obj instanceof Double) {
            return new Double((Double) obj);
        } else if (obj instanceof Float) {
            return new Float(Float.valueOf((Float) obj));
        } else if (obj instanceof Long) {
            return new Long(Long.valueOf((Long) obj));
        } else if (obj instanceof Short) {
            return new Short(Short.valueOf((Short) obj));
        } else if (obj instanceof Thread) {
            return new Thread((Thread) obj);
        } else {
            return obj;
        }
    }
 
    /**
     * Initializes an array of objects. This does not actually initialize the
     * objects, it just checks them. Say you have an Image, perhaps the value
     * was null!
     *
     *
     * @param obj
     * @return
     * @throws ClassNotFoundException
     * @throws NoSuchFieldException
     * @throws Exception
     */
    private synchronized void initialize(Object[] obj) throws ClassNotFoundException, NoSuchFieldException, Exception {
        if (obj != null) {
            try {
                throw new Exception();
            } catch (Exception e) {
                for (Object object : obj) {
                    initialize(object);
                    Initializer.e = e;
                    if (object instanceof Field || object instanceof Class) {
                        if (object instanceof Field) {
                            Field.setAccessible((AccessibleObject[]) obj, true);
                            System.out.println(object + " initialized. value: " + getValue(object));
                            numOfObjects += 1;
                            Field.setAccessible((AccessibleObject[]) obj, false);
                        } else {
                            System.out.println(((Class) object).getCanonicalName() + " initialized.");
                            numOfObjects += 1;
                        }
                    } else {
                        System.out.println(getContainingClass(object) + ".Field"
                                + Arrays.toString(e.getStackTrace()[getStackSize() - 1].getClassName().getBytes())
                                + "] initialized. value: " + "(" + getInstanceAsString(object) + ")" + object);
                        numOfObjects += 1;
                    }
                }
            }
        }
    }
 
    @SuppressWarnings("BoxingBoxedValue")
    public static Object getValue(Object obj) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException {
        Field f = (Field) obj;
        if (f.getType().isPrimitive() || f.getType().isSynthetic()) {
            return f.get(f);
        } else if (f.getType() == String.class || f.getType() == Character.class) {
            return f.get(f);
        } else if (f.getType() == Boolean.class) {
            return f.get(f);
        } else {
            return f.getType().getCanonicalName();
        }
    }
 
    public static Object[] getClasses(Class clss) {
        return clss.getDeclaredClasses();
    }
 
    public static Object[] getFields(Class clss) {
        return clss.getDeclaredFields();
    }
 
    public static int getNumOfObjects() {
        return numOfObjects;
    }
 
    @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 Initializer.e;
    }
 
    public static Object getInstance(Object obj) throws NoSuchFieldException {
        return ((Class) obj).getTypeName();
 
    }
 
    public static String getInstanceAsString(Object obj) {
        if (obj == null) {
            return "Object";
        } else if (obj instanceof Integer) {
            return "Integer";
        } else if (obj instanceof Byte) {
            return "Byte";
        } else if (obj instanceof Character) {
            return "Character";
        } else if (obj instanceof String) {
            return "String";
        } else if (obj instanceof Boolean) {
            return "Boolean";
        } else if (obj instanceof Class) {
            return "Class";
        } else if (obj instanceof Double) {
            return "Double";
        } else if (obj instanceof Float) {
            return "Float";
        } else if (obj instanceof Long) {
            return "Long";
        } else if (obj instanceof Short) {
            return "Short";
        } else if (obj instanceof Thread) {
            return "Thread";
        } else if (obj instanceof Enum) {
            return "Enum";
        } else if (obj instanceof Object[]) {
            return "Object[]";
        } else {
            return "Unknown";
        }
    }
 
    public static Class getContainingClass(Object obj) {
        return Initializer.e.getStackTrace()[getStackSize() - 1].getClassName().getClass().getDeclaringClass();
    }
   
    public static Version getVersion(){
        return e.getStackTrace()[getStackSize() - 1].getClassName().getClass().class.getAnnotation(Version.class);
    }
 
    private Integer[] parseFieldIntValues(Class clss) {
        this.f = clss.getDeclaredFields();
        Integer[] values = {(Integer) f[0]};
        return values;
    }
 
}

Comments