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 <T> List<T> ofType(List<Object> list, Class<T> targetClass) {
    List<T> result = new ArrayList<T>();

    for (Object object : list) {
        boolean isTargetClass = object.getClass().equals(targetClass);
        if (isTargetClass) {
            result.add(targetClass.cast(object));
        }/*ww  w. ja v a 2 s  . c  o  m*/
    }

    return result;
}

From source file:Main.java

/**
 * Get all fields' value and put them to a map.
 * //from   ww w  .  ja  v a2s .  c o m
 * @param bean
 * @return Map
 */
public static Map<String, Object> getFieldValueMap(Object bean) {
    Class<?> cls = bean.getClass();
    Map<String, Object> valueMap = new HashMap<String, Object>();
    // Get all fields.
    Field[] fields = cls.getDeclaredFields();

    for (Field field : fields) {
        try {
            field.setAccessible(true);
            Object value = field.get(bean);
            //            if(value == null) {
            //               valueMap.put(field.getName(), "");
            //               continue;
            //            }
            valueMap.put(field.getName(), value);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    return valueMap;
}

From source file:Main.java

private static Class<?>[] classess(Object[] args) {
    Class<?>[] result = new Class<?>[args.length];
    for (int i = 0; i < args.length; i++) {
        Object z = args[i];
        if (z != null) {
            result[i] = z.getClass();
        }//from w  w  w.  java 2  s  .c om
    }
    return result;
}

From source file:Main.java

protected static boolean isArray(Object obj) {
    if (obj.getClass().isArray()) {
        return true;
    }// www  . java  2s. co m
    return false;
}

From source file:Main.java

public static Object getSp(Context context, String key, Object defaultObject) {
    String type = defaultObject.getClass().getSimpleName();
    String packageName = context.getPackageName();
    SharedPreferences sp = context.getSharedPreferences(packageName, Context.MODE_PRIVATE);
    if ("String".equals(type)) {
        return sp.getString(key, (String) defaultObject);
    } else if ("Integer".equals(type)) {
        return sp.getInt(key, (Integer) defaultObject);
    } else if ("Boolean".equals(type)) {
        return sp.getBoolean(key, (Boolean) defaultObject);
    } else if ("Float".equals(type)) {
        return sp.getFloat(key, (Float) defaultObject);
    } else if ("Long".equals(type)) {
        return sp.getLong(key, (Long) defaultObject);
    }//from w  w  w  . j a  v a2 s.  co m
    return null;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> List<T> generatePropertyList(Collection<?> collection, String property) {
    assert property != null;
    if (collection == null || collection.isEmpty()) {
        return new ArrayList<T>(0);
    }/*from  www .  ja  v  a 2s .  com*/
    List<T> list = new ArrayList<T>(collection.size());
    try {
        for (Object obj : collection) {
            Field field = obj.getClass().getDeclaredField(property);
            field.setAccessible(true);
            Object object = field.get(obj);
            list.add((T) object);
        }
        return list;
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Object getObjValue(String methodName, Object object) {
    try {/*from   w w  w . j a  va 2s  .  com*/
        Class<? extends Object> cls = object.getClass();
        Method fieldMethod = cls.getMethod(methodName);
        if (fieldMethod != null) {
            return fieldMethod.invoke(object);
        }
        return null;
    } catch (Exception e) {
    }
    return null;
}

From source file:Main.java

public static Object readJavaObject(Connection conn, long id) throws Exception {
    PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL);
    pstmt.setLong(1, id);/*from   ww w . ja v  a 2 s . co  m*/
    ResultSet rs = pstmt.executeQuery();
    rs.next();
    Object object = rs.getObject(1);
    String className = object.getClass().getName();
    rs.close();
    pstmt.close();
    return object;
}

From source file:Main.java

public static String getClassName(Object obj) {
    if (obj == null) {
        return "";
    }//from w w w .j  a v  a2  s.  c o  m
    return obj.getClass().getName();
}

From source file:Main.java

private static String callMethod(Object o, String methodName) {
    try {//from ww  w  .j  a va2  s  . com
        Method method = o.getClass().getMethod(methodName);
        String value = (String) method.invoke(o);
        return value;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}