Example usage for java.lang Object getClass

List of usage examples for java.lang Object getClass

Introduction

In this page you can find the example usage for java.lang Object getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

public static void toXml(Object bean) {
    Method[] declaredMethods = bean.getClass().getDeclaredMethods();
    for (Method declaredMethod : declaredMethods) {
        if (declaredMethod.getName().startsWith("get")) {
            try {
                Object invoke = declaredMethod.invoke(bean);
                System.out.println(declaredMethod.getName() + " = " + invoke);
            } catch (Exception e) {
                e.printStackTrace(); //TODO To change body of catch statement use File | Settings | File Templates.
            }/*from  ww w  . ja va2  s  . co m*/
        }
    }
}

From source file:Main.java

public static Class<?> messageClass(Object message) {

    final Class<?> messageClass = message.getClass();

    if (messageClass.isAnonymousClass() || messageClass.getSimpleName().contains("-$Lambda$") // Jack
            || messageClass.getSimpleName().contains("$$Lambda$") // Retrolambda
    )/*from  ww w . j a v  a2s . c o m*/
        return messageClass.getInterfaces()[0];

    return messageClass;
}

From source file:Main.java

static public Method isMethodImplemented(Object obj, String methodName) {
    Method[] methods = obj.getClass().getDeclaredMethods();
    for (Method m : methods) {
        if (m.getName().contains(methodName)) {
            return m;
        }/*from   w w  w .  jav a 2 s .com*/
    }
    return null;
}

From source file:Main.java

public static <T> T getAnnotation(Object annotatedClass, Class<T> annotation) {
    Class screenType = annotatedClass.getClass();
    return (T) screenType.getAnnotation(annotation);
}

From source file:Main.java

public static Object getProperty(Object o, String field) {
    try {//from  w w  w.j a  va 2s  .c  o  m
        Field f = o.getClass().getDeclaredField(field);
        return f.get(o);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Field getDeclaredField(Object object, String fieldName) {
    Class<?> clz = object.getClass();
    for (; clz != Object.class; clz = clz.getSuperclass()) {
        try {//  w ww. jav a  2  s . c  o  m
            Field field = clz.getDeclaredField(fieldName);
            return field;
        } catch (Exception e) {
        }
    }
    return null;
}

From source file:Main.java

public static Object[] getArray(Object val) {
    Class<?> valKlass = val.getClass();
    Object[] outputArray = null;//from  www.  ja  v  a 2  s  . com
    for (Class<?> arrKlass : ARRAY_PRIMITIVE_TYPES) {
        if (valKlass.isAssignableFrom(arrKlass)) {
            int arrlength = Array.getLength(val);
            outputArray = new Object[arrlength];
            for (int i = 0; i < arrlength; ++i) {
                outputArray[i] = Array.get(val, i);
            }
            break;
        }
    }
    if (outputArray == null) // not primitive type array
        outputArray = (Object[]) val;
    return outputArray;
}

From source file:Main.java

public static void copyBean(Object from, Object to) {
    Class<?> beanClass = from.getClass();
    Field[] fields = beanClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];/*w  ww  .  j  av  a  2  s .  c o m*/
        field.setAccessible(true);
        try {
            Object value = field.get(from);
            field.set(to, value);
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static void outputArrays(final Object first, final Object second) {
    if (!first.getClass().isArray()) {
        throw new IllegalArgumentException("first is not an array.");
    }/* ww w. jav a 2  s. co  m*/
    if (!second.getClass().isArray()) {
        throw new IllegalArgumentException("second is not an array.");
    }

    final int lengthFirst = Array.getLength(first);
    final int lengthSecond = Array.getLength(second);
    final int length = Math.max(lengthFirst, lengthSecond);

    for (int idx = 0; idx < length; idx++) {
        System.out.print("[" + idx + "]\t");
        if (idx < lengthFirst) {
            System.out.print(Array.get(first, idx) + "\t\t");
        } else {
            System.out.print("\t\t");
        }
        if (idx < lengthSecond) {
            System.out.print(Array.get(second, idx) + "\t");
        }
        System.out.println();
    }
}

From source file:Main.java

/**
 * Debug.//from w  ww  . ja va2s. c  o  m
 *
 * @param object  the object
 * @param message the message
 */
public static void debug(Object object, String message) {
    debug(object.getClass().getSimpleName(), message);
}