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:com.ginema.api.reflection.ReflectionUtils.java

public static boolean isAnnotatedWith(Object c, Class clazz) {
    return c.getClass().isAnnotationPresent(clazz);
}

From source file:Main.java

/**
 * Add a {@code (key, value)} data into the given {@code sharedPreferences}.
 * Now, this method supports only {@code boolean} and {@link String}.
 */// ww  w .j  a  v  a2s.c  o  m
public static void updateSharedPreference(SharedPreferences sharedPreferences, String key, Object value) {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    if (value.getClass() == Boolean.class) {
        editor.putBoolean(key, Boolean.class.cast(value));
    } else if (value.getClass() == String.class) {
        editor.putString(key, String.class.cast(value));
    } else if (value.getClass() == Integer.class) {
        editor.putInt(key, Integer.class.cast(value));
    } else {
        throw new IllegalArgumentException("value's type can be only Boolean or String.");
    }
    editor.commit();
}

From source file:Main.java

/**
 * Loads the class with the specified name. This method first attempts  to load the
 * class with the current context classloader and only if the search failed, it
 * tries to load the class with the class loader of the given object.
 *
 * @param name the name of the class.// w ww .j a va 2 s  . c o m
 * @param o the object which classloader should be used.
 *
 * @return the result {@link java.lang.Class} object.
 *
 * @throws ClassNotFoundException if the class was not found.
 */
public static Class loadClass(final String name, final Object o) throws ClassNotFoundException {
    return loadClass(name, o.getClass().getClassLoader());
}

From source file:Main.java

/**
 * Returns a duplicates of an array.//from   ww  w. ja v  a  2 s.  c  om
 */
public static Object duplicateArray(Object source) {

    int size = Array.getLength(source);
    Object newarray = Array.newInstance(source.getClass().getComponentType(), size);

    System.arraycopy(source, 0, newarray, 0, size);

    return newarray;
}

From source file:Main.java

public static Object getReflection(Object itemToGetObject, String objectName) {
    try {//from  ww w.j a  v  a2s . c om
        Class<?> clazz = itemToGetObject.getClass();
        Field field;
        field = clazz.getDeclaredField(objectName);
        field.setAccessible(true);
        return field.get(itemToGetObject);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Warn./*  www  . j av  a  2  s.  co m*/
 *
 * @param object  the object
 * @param message the message
 */
public static void warn(Object object, String message) {
    warn(object.getClass().getSimpleName(), message);
}

From source file:com.nec.strudel.entity.EntityUtil.java

public static int hashCode(Object entity) {
    int hashCode = entity.getClass().hashCode();
    hashCode = hashCode * HASH_BASE + describe(entity).hashCode();
    return hashCode;
}

From source file:Main.java

/**
 * <p>//from   w  ww  . jav a 2s  .c o  m
 * Converts an Object reference that is known to be an array into a List.
 * Semantically very similar to {@link java.util.Arrays#asList(Object[])}
 * except that this method can deal with arrays of primitives in the same
 * manner as arrays as objects.
 * </p>
 * 
 * <p>
 * A new List is created of the same size as the array, and elements are
 * copied from the array into the List. If elements are primitives then they
 * are converted to the appropriate wrapper types in order to return a List.
 * </p>
 * 
 * @param in
 *            an array of Objects or primitives (null values are not
 *            allowed)
 * @return a List containing an element for each element in the input array
 * @throws IllegalArgumentException
 *             thrown if the in parameter is null or not an array
 */
public static List<Object> asList(Object in) {
    if (in == null || !in.getClass().isArray()) {
        throw new IllegalArgumentException("Parameter to asObjectArray must be a non-null array.");
    } else {
        int length = Array.getLength(in);
        LinkedList<Object> list = new LinkedList<Object>();
        for (int i = 0; i < length; ++i) {
            list.add(i, Array.get(in, i));
        }

        return list;
    }
}

From source file:Main.java

/**
 * Error./*from   ww w . ja v  a 2s  .  co  m*/
 *
 * @param object  the object
 * @param message the message
 */
public static void error(Object object, String message) {
    error(object.getClass().getSimpleName(), message);
}

From source file:Main.java

public static void setProperty(Object paramObject1, String paramString, Object paramObject2) {
    try {//from   w  ww .j a va  2s. c  om
        Field localField = paramObject1.getClass().getDeclaredField(paramString);
        localField.setAccessible(true);
        localField.set(paramObject1, paramObject2);
    } catch (Exception var4) {
        ;
    }

}