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 setParam(Context context, String key, Object object) {

    String type = object.getClass().getSimpleName();
    SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();

    if ("String".equals(type)) {
        editor.putString(key, (String) object);
    } else if ("Integer".equals(type)) {
        editor.putInt(key, (Integer) object);
    } else if ("Boolean".equals(type)) {
        editor.putBoolean(key, (Boolean) object);
    } else if ("Float".equals(type)) {
        editor.putFloat(key, (Float) object);
    } else if ("Long".equals(type)) {
        editor.putLong(key, (Long) object);
    }/*from  ww  w  .j  av a 2 s  .  c om*/

    editor.commit();
}

From source file:Main.java

public static Object containsClassAttributeInCollection(Collection in, String fieldName, Object value)
        throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException {
    Iterator<Object> it = in.iterator();
    boolean isBoolean = (value instanceof Boolean || value == boolean.class);
    String methodName = (isBoolean) ? "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) //$NON-NLS-1$
            : "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); //$NON-NLS-1$

    while (it.hasNext()) {
        Object obj = it.next();
        Method m = obj.getClass().getMethod(methodName, new Class[] {});
        Object value2 = m.invoke(obj, null);
        if (value != null && value2 != null && value.equals(value2))
            return obj;
    }//from   w  w  w .j a  v a  2  s . c  o m
    return null;
}

From source file:Main.java

public static Object getParam(Context context, String key, Object defaultObject) {
    String type = defaultObject.getClass().getSimpleName();
    SharedPreferences sp = context.getSharedPreferences(FILE_NAME, 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 ww w .j a v  a2 s . c o  m
    return null;
}

From source file:Main.java

/**
 * Method that can be called to determine if given Object is the default
 * implementation Jackson uses; as opposed to a custom serializer installed by
 * a module or calling application. Determination is done using
 * {@link JacksonStdImpl} annotation on handler (serializer, deserializer etc)
 * class.//from  ww  w.  jav  a  2 s . com
 */
public static boolean isJacksonStdImpl(Object impl) {
    return (impl != null) && isJacksonStdImpl(impl.getClass());
}

From source file:Main.java

public static Collection extractField(Collection in, String fieldName, Class type) throws SecurityException,
        NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Iterator<Object> it = in.iterator();
    boolean isBoolean = (type == Boolean.class || type == boolean.class);
    String methodName = (isBoolean) ? "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) //$NON-NLS-1$
            : "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); //$NON-NLS-1$
    LinkedList<Object> out = new LinkedList<Object>();

    while (it.hasNext()) {
        Object obj = it.next();
        Method m = obj.getClass().getMethod(methodName, new Class[] {});
        Object value2 = m.invoke(obj, null);
        out.add(value2);//  w  w  w.  j  ava  2 s .  com

    }
    return out;
}

From source file:ArrayGrowTest.java

/**
 * A convenience method to print all elements in an array
 * // w w  w. jav a 2s .  c  o  m
 * @param a
 *          the array to print. It can be an object array or a primitive type
 *          array
 */
static void arrayPrint(Object a) {
    Class cl = a.getClass();
    if (!cl.isArray())
        return;
    Class componentType = cl.getComponentType();
    int length = Array.getLength(a);
    System.out.print(componentType.getName() + "[" + length + "] = { ");
    for (int i = 0; i < Array.getLength(a); i++)
        System.out.print(Array.get(a, i) + " ");
    System.out.println("}");
}

From source file:Main.java

public static String[] getMethodParameter(Object[] parameters) {
    if (parameters.length == 0) {
        return null;
    }/*  w ww .  j a v  a2 s . c  o m*/
    String[] result = new String[parameters.length];
    int i = 0;
    for (Object c : parameters) {
        result[i++] = c.getClass().getName();
    }
    return result;
}

From source file:ArrayGrowTest.java

/**
 * This method grows an array by allocating a new array of the same type and
 * copying all elements.//from   w w w  .j a  va 2 s .c  om
 * 
 * @param a
 *          the array to grow. This can be an object array or a primitive type
 *          array
 * @return a larger array that contains all elements of a.
 */
static Object goodArrayGrow(Object a) {
    Class cl = a.getClass();
    if (!cl.isArray())
        return null;
    Class componentType = cl.getComponentType();
    int length = Array.getLength(a);
    int newLength = length * 11 / 10 + 10;

    Object newArray = Array.newInstance(componentType, newLength);
    System.arraycopy(a, 0, newArray, 0, length);
    return newArray;
}

From source file:Main.java

/**
 * Logs a life cycle method for a given object.  Log message is debug and will include class name.
 *
 * @param object/*w  w w .  ja va 2s. c o  m*/
 * @param lifeCycleMethodName
 */
public static void logLC(Object object, String lifeCycleMethodName) {
    Log.d("LifeC - " + object.getClass().getSimpleName(), lifeCycleMethodName);
}

From source file:Main.java

public static String getClassName(Object o) {
    String fullyQualifiedClassName = o.getClass().getName();
    return getClassName(fullyQualifiedClassName);
}