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

@SuppressWarnings("unchecked")
static public <T> T dynamicCast(Object object, Class<T> clazz) {
    if (object != null && clazz != null && object.getClass().equals(clazz)) {
        return (T) object;
    }/*from  ww  w .j av  a2 s.  c  o m*/
    return null;
}

From source file:Main.java

public static long writeJavaObject(Connection conn, Object object) throws Exception {
    String className = object.getClass().getName();
    PreparedStatement pstmt = conn.prepareStatement(WRITE_OBJECT_SQL);
    pstmt.setString(1, className);/*from w  w  w .  j ava  2s .c o m*/
    pstmt.setObject(2, object);
    pstmt.executeUpdate();
    ResultSet rs = pstmt.getGeneratedKeys();
    int id = -1;
    if (rs.next()) {
        id = rs.getInt(1);
    }
    rs.close();
    pstmt.close();
    return id;
}

From source file:Main.java

static public <T> T callMethodWithNoArgs(Object obj, String methodName) {
    Class<?> clazz = obj.getClass();
    try {//from ww w.  j  av  a  2 s.  com
        Method method = clazz.getMethod(methodName);
        return (T) method.invoke(obj);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();

        throw new RuntimeException("NoSuchMethodException " + methodName);
    } catch (InvocationTargetException e) {
        e.printStackTrace();

        throw new RuntimeException("InvocationTargetException " + methodName);
    } catch (IllegalAccessException e) {
        e.printStackTrace();

        throw new RuntimeException("IllegalAccessException " + methodName);
    }
}

From source file:Main.java

public static void copyProperties(Object desc, Object obj) {
    Class<?> descClass = desc.getClass();
    Class<?> objClass = obj.getClass();
    Field[] fields = objClass.getDeclaredFields();
    try {//from w  w  w.j a va  2 s .  c om
        for (int i = 0; i < fields.length; i++) {
            String name = fields[i].getName();
            String getMethodName = "get" + toFirstLetterUpperCase(name);
            String setMethodName = "set" + toFirstLetterUpperCase(name);
            try {
                Object value = objClass.getMethod(getMethodName).invoke(obj);
                descClass.getMethod(setMethodName, value.getClass()).invoke(desc, value);
            } catch (Exception e) {
            }
        }
    } catch (Exception e) {

    }
}

From source file:Main.java

private static boolean ignoreClass(Object o, Class<?> c) {
    return c.isAssignableFrom(o.getClass());
}

From source file:Main.java

public static Object getProperty(Object o, String field) {
    try {//from   w  ww .  j a  va2s .co m
        Field f = o.getClass().getDeclaredField(field);
        f.setAccessible(true);
        String name = f.getName();
        name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase(Locale.US));
        Method m = o.getClass().getMethod("get" + name);
        // return f.get(o);
        return (Object) m.invoke(o);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Object removeAdditionalStaticField(Object obj, String key) {
    return removeAdditionalInstanceField(obj.getClass(), key);
}

From source file:Main.java

public static Field getPrivateField(Object target, String fieldName) throws NoSuchFieldException {
    Class targetClass = target.getClass();
    Field field = targetClass.getDeclaredField(fieldName);
    field.setAccessible(true);//from   w ww  .ja v  a  2  s .  c o m
    return field;
}

From source file:Main.java

public static Collection<?> getFieldFrom(Collection<?> set, String FieldName) {
    ArrayList arrayList = new ArrayList();
    for (Object o : set) {
        try {//from  w w  w  .j av a 2  s  .c  o  m
            Object object = o.getClass().getField(FieldName).get(o);
            arrayList.add(o);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static Object getProperty(Object classObject, String paramString) {
    try {/* www . j av  a  2  s  . com*/
        Field localField = classObject.getClass().getDeclaredField(paramString);
        localField.setAccessible(true);
        return localField.get(classObject);
    } catch (Exception var3) {
        return null;
    }
}