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 Activity getCurrentTopActivity()
        throws ClassNotFoundException, IllegalArgumentException, SecurityException, IllegalAccessException,
        InvocationTargetException, NoSuchMethodException, NoSuchFieldException {
    Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
    Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
    Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
    activitiesField.setAccessible(true);
    Map<?, ?> activities = (Map<?, ?>) activitiesField.get(activityThread);
    for (Object activityRecord : activities.values()) {
        Class<?> activityRecordClass = activityRecord.getClass();
        Field pausedField = activityRecordClass.getDeclaredField("paused");
        pausedField.setAccessible(true);
        if (!pausedField.getBoolean(activityRecord)) {
            Field activityField = activityRecordClass.getDeclaredField("activity");
            activityField.setAccessible(true);
            Activity activity = (Activity) activityField.get(activityRecord);
            return activity;
        }/*from  w w  w.j  a  v  a2  s .c o m*/
    }
    return null;
}

From source file:ArrayDemo.java

/** 
 * Print out 2 arrays in columnar format.
 *
 * @param first The array for the first column.
 * @param second The array for the second column.
 *
 * @throws IllegalArgumentException __UNDOCUMENTED__
 *//*from   w  w w .ja v a2 s .  co m*/
public static void outputArrays(final Object first, final Object second) {
    if (!first.getClass().isArray()) {
        throw new IllegalArgumentException("first is not an array.");
    }
    if (!second.getClass().isArray()) {
        throw new IllegalArgumentException("second is not an array.");
    }

    final int lengthFirst = Array.getLength(first);
    final int lengthSecond = Array.getLength(second);
    final int length = Math.max(lengthFirst, lengthSecond);

    for (int idx = 0; idx < length; idx++) {
        System.out.print("[" + idx + "]\t");
        if (idx < lengthFirst) {
            System.out.print(Array.get(first, idx) + "\t\t");
        } else {
            System.out.print("\t\t");
        }
        if (idx < lengthSecond) {
            System.out.print(Array.get(second, idx) + "\t");
        }
        System.out.println();
    }
}

From source file:Main.java

public static Object mergedObject(Object oldObject, Object newObject) throws Exception {
    Class<?> cls = newObject.getClass();
    Field[] fields = cls.getDeclaredFields();
    for (Field field : fields) {
        Class<?> clsType = field.getType();
        String name = field.getName();
        String method = name.substring(0, 1).toUpperCase() + name.substring(1, name.length());
        String strGet = "get" + method;
        Method methodGet = cls.getDeclaredMethod(strGet);
        Object object = methodGet.invoke(newObject);
        if (object != null) {
            String strSet = "set" + method;
            Method methodSet = cls.getDeclaredMethod(strSet, clsType);
            Object objValue = typeConversion(clsType, object.toString());
            methodSet.invoke(oldObject, objValue);
        }//  w w w . ja  va  2  s .  c om
    }
    return oldObject;
}

From source file:cn.wanghaomiao.seimi.utils.StructValidator.java

public static boolean validateAnno(Object object) {
    for (Field field : object.getClass().getDeclaredFields()) {
        NotNull notNullCheck = field.getAnnotation(NotNull.class);
        if (notNullCheck != null) {
            try {
                Object val = FieldUtils.readField(field, object, true);
                if (StringUtils.isBlank(String.valueOf(val))) {
                    logger.error("Field={}.{} can not be null!", object.getClass().getSimpleName(),
                            field.getName());
                    return false;
                }/*from   w w w  . j  a v a  2 s.  c  om*/
            } catch (IllegalAccessException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
    return true;
}

From source file:io.fabric8.forge.rest.model.Models.java

public static String toJson(Object dto) throws JsonProcessingException {
    Class<?> clazz = dto.getClass();
    return objectMapper.writerFor(clazz).writeValueAsString(dto);
}

From source file:Main.java

public static Map<String, String> compareMap(Map<String, String> map, Object obj) {
    Map<String, String> mapValue = new HashMap<String, String>();
    Field[] fields = obj.getClass().getDeclaredFields();
    for (Field field : fields) {
        String name = field.getName();
        if (map.containsKey(name)) {
            mapValue.put(name, map.get(name));
        }/*from  www  .j a  va2  s.c  o  m*/
    }
    return mapValue;
}

From source file:Main.java

/**
 * Method to merge two arrays.//w ww  .j ava2 s .c om
 * 
 * @param firstObject
 *            The first array to be merged
 * 
 * @param secondObject
 *            The second array to be merged
 * 
 * @return an object containing the elements of the merged arrays
 */
private static Object joinArrays(Object firstObject, Object secondObject) {
    Class<?> o1Type = firstObject.getClass().getComponentType();
    Class<?> o2Type = secondObject.getClass().getComponentType();

    if (o1Type != o2Type)
        throw new IllegalArgumentException();

    int firstObjectSize = Array.getLength(firstObject);
    int secondObjectSize = Array.getLength(secondObject);
    Object array = Array.newInstance(o1Type, firstObjectSize + secondObjectSize);

    int offset = 0, i;
    for (i = 0; i < firstObjectSize; i++, offset++)
        Array.set(array, offset, Array.get(firstObject, i));
    for (i = 0; i < secondObjectSize; i++, offset++)
        Array.set(array, offset, Array.get(secondObject, i));

    return array;
}

From source file:Main.java

public static <T extends Annotation> boolean hasObjectMethodWithAnnotation(final Object o,
        final Class<T> annotation) {
    final Class<?>[] interfaces = o.getClass().getInterfaces();
    for (final Class<?> anInterface : interfaces) {
        for (final Method method : anInterface.getMethods()) {
            if (method.getAnnotation(annotation) != null) {
                return true;
            }/*  w  w  w  . ja  va2  s. co  m*/
        }

    }
    return false;
}

From source file:Main.java

public static int hashCode(Object paramObject) {
    int i = 1;//  w  w w .  j av  a 2  s  .c  om
    Field[] arrayOfField = paramObject.getClass().getDeclaredFields();
    int j = arrayOfField.length;
    for (int k = 0;; k++) {
        int m;
        int i1;
        if (k < j) {
            Field localField = arrayOfField[k];
            localField.setAccessible(true);
            try {
                Object localObject = localField.get(paramObject);
                m = i * 31;
                if (localObject == null) {
                    i1 = 0;
                } else {
                    int n = localObject.hashCode();
                    i1 = n;
                }
            } catch (IllegalArgumentException localIllegalArgumentException) {
                localIllegalArgumentException.printStackTrace();
                continue;
            } catch (IllegalAccessException localIllegalAccessException) {
                localIllegalAccessException.printStackTrace();
                continue;
            }
        } else {
            return i;
        }
        i = m + i1;
    }
}

From source file:psiprobe.controllers.threads.ListThreadsController.java

/**
 * To uid./*from w  w w .  j  a  v  a2 s  . com*/
 *
 * @param obj the obj
 * @return the string
 */
private static String toUid(Object obj) {
    return obj.getClass().getName() + "@" + obj.hashCode();
}