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 boolean hasMethod(Object obj, String method) {
    assert method != null;
    Method[] ms = obj.getClass().getMethods();
    for (Method m : ms) {
        if (m.getName().equals(method))
            return true;
    }/* w  w w  .j av  a  2s .  co  m*/
    return false;
}

From source file:Main.java

public static void setter(Object obj, String att, Object value, Class<?> type) {
    try {//ww  w. ja v a  2  s  .c o m
        Method method = obj.getClass().getMethod("set" + att, type);
        method.invoke(obj, value);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Object invokeMethod(Object object, String methodName) throws Exception {
    Class clazz = object.getClass();
    return invokeMethod(clazz, object, methodName, null, null);
}

From source file:Main.java

public static String object2Xml(Object obj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    StringWriter writer = new StringWriter();
    marshaller.marshal(new JAXBElement(new QName("xml"), obj.getClass(), obj), writer);

    return writer.toString();
}

From source file:com.ginema.api.reflection.ReflectionUtils.java

public static boolean isPrimitive(Object c) {
    return c.getClass().isPrimitive() || ClassUtils.wrapperToPrimitive(c.getClass()) != null;

}

From source file:Main.java

public static void dumpIntent(Intent intent) {
    System.out.println("action: " + intent.getAction());
    System.out.println("data: " + intent.getData());
    System.out.println("extras:");
    Bundle bundle = intent.getExtras();//from   ww  w .  j a  va  2s .c o  m
    for (String key : bundle.keySet()) {
        Object object = bundle.get(key);
        System.out.println(key + "->" + object + "(" + object.getClass().getName() + ")");
    }
}

From source file:Main.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static List subviews(Object o) {

    try {//from  www. ja  v a  2  s  .c o m
        Method getChild = o.getClass().getMethod("getChildAt", int.class);
        getChild.setAccessible(true);
        Method getChildCount = o.getClass().getMethod("getChildCount");
        getChildCount.setAccessible(true);
        List result = new ArrayList(8);
        int childCount = (Integer) getChildCount.invoke(o);
        for (int i = 0; i < childCount; i++) {
            result.add(getChild.invoke(o, i));
        }
        return result;

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

}

From source file:Main.java

public static String beanToXml(ByteArrayOutputStream out, Object to) {
    try {//w w w .jav a  2s. c om
        JAXBContext context = JAXBContext.newInstance(to.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(to, out);
        return new String(out.toByteArray(), "UTF-8");
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Object arrayExpandAddElements(Object array, Collection elementsToAdd) {
    Class cl = array.getClass();
    if (!cl.isArray())
        return null;
    int length = Array.getLength(array);
    int newLength = length + elementsToAdd.size();
    Class componentType = array.getClass().getComponentType();
    Object newArray = Array.newInstance(componentType, newLength);
    System.arraycopy(array, 0, newArray, 0, length);
    int count = 0;
    for (Object element : elementsToAdd) {
        Array.set(newArray, length + count, element);
        count++;/*from   w w  w  . j  ava 2  s  .c om*/
    }
    return newArray;
}

From source file:SerializeJavaObjects_MySQL.java

public static long writeJavaObject(Connection conn, Object object) throws Exception {
    String className = object.getClass().getName();
    PreparedStatement pstmt = conn.prepareStatement(WRITE_OBJECT_SQL);

    // set input parameters
    pstmt.setString(1, className);//from w  w  w  . j  a v a  2 s.  c o  m
    pstmt.setObject(2, object);
    pstmt.executeUpdate();

    // get the generated key for the id
    ResultSet rs = pstmt.getGeneratedKeys();
    int id = -1;
    if (rs.next()) {
        id = rs.getInt(1);
    }

    rs.close();
    pstmt.close();
    System.out.println("writeJavaObject: done serializing: " + className);
    return id;
}