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

/**
 * Returns a list of classes containing the class of the object and all its super classes
 * /*from   w  w  w .  j a  v a 2  s  . co  m*/
 * @author Tristan Bepler
 */
private static List<Class<?>> getSupers(Object o) {
    List<Class<?>> classes = new ArrayList<Class<?>>();
    Class<?> curClass = o.getClass();
    while (curClass != null) {
        classes.add(curClass);
        curClass = curClass.getSuperclass();
    }
    return classes;
}

From source file:Main.java

private static void methodInvoker(Object receiver, String methodName, HashMap<String, Object> map) {
    for (Class<?> klass = receiver.getClass(); !(Object.class.equals(klass)); klass = klass.getSuperclass()) {
        try {//w  ww  .j  a v a2 s.  c om
            Method method = klass.getDeclaredMethod(methodName, Map.class);
            method.setAccessible(true);
            method.invoke(receiver, map);
            break;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

/**
 *
 * @param obj//from   ww w  .  jav a  2s.c o  m
 * @throws JAXBException
 */
public static void serializeToSTD(Object obj) throws JAXBException {
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(JAXB_FRAGMENT, TRUE);
    m.marshal(obj, out);
}

From source file:Main.java

public static void setFieldValue(final Object object, final String name, final Object value) throws Exception {
    Field field = object.getClass().getDeclaredField(name);
    field.setAccessible(true);//from   w  ww . j a  v  a  2s. c  om
    field.set(object, value);
}

From source file:Main.java

public static long NonSearchingJavaToUnoType(Object obj, boolean errorIfMissing) throws Throwable {
    Class<?> firstCls = obj.getClass();
    String clsName = firstCls.getName();
    Long unoTypePtr = unoTypes.get(clsName);
    if (unoTypePtr == null) {
        if (errorIfMissing) {
            String msg = "NonSearchingJavaToUnoType: Could not find uno type for " + clsName;
            throw new Exception(msg);
        } else {//from   ww  w . j  av  a2 s.co m
            return 0L;
        }
    }
    return (long) unoTypePtr;
}

From source file:Main.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static List parents(Object o) {
    try {//from   www. j  a v a 2  s  .c  o m

        Method getParent = o.getClass().getMethod("getParent");
        getParent.setAccessible(true);

        List result = new ArrayList(8);
        try {
            while (true) {
                Object parent = getParent.invoke(o);
                if (parent == null) {
                    return result;
                } else {
                    result.add(parent);
                }
                o = parent;
            }
        } catch (IllegalArgumentException e) {
            return result;
        } catch (IllegalAccessException e) {
            return result;
        } catch (InvocationTargetException e) {
            return result;
        }

    } catch (NoSuchMethodException e) {
        return Collections.EMPTY_LIST;
    }

}

From source file:Main.java

public static Object invokeGet(Object o, String fieldName) {

    Method method = getGetMethod(o.getClass(), fieldName);

    try {/* w  w  w.j  av  a2  s  .  c  o  m*/

        return method.invoke(o, new Object[0]);

    } catch (Exception e) {

        e.printStackTrace();

    }

    return null;

}

From source file:Main.java

public static OutputStream serializerOutputStream(Object xmlObj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(xmlObj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshal(xmlObj, baos);//from  w  w w.ja  v a  2  s  .  c om
    return baos;
}

From source file:Main.java

private static String fileName(Object o) {
    if (o instanceof String)
        return (String) o;
    return fileName(o.getClass());
}

From source file:Main.java

public static File Marshall(Object object, String filePath) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(object.getClass());
    Marshaller u = jc.createMarshaller();
    u.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    u.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    File f = new File(filePath);
    u.marshal(object, f);//from w w w.  j av a2s .c  om
    return f;
}