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

/**
 * Returns an array of {@code Type} objects representing the actual type
 * arguments to this object.//from  ww w.ja v  a 2  s  . c o m
 * If the returned value is null, then this object represents a non-parameterized
 * object.
 *
 * @param object the {@code object} whose type arguments are needed.
 * @return an array of {@code Type} objects representing the actual type
 *       arguments to this object.
 *
 * @see {@link Class#getGenericSuperclass()}
 * @see {@link ParameterizedType#getActualTypeArguments()}
 */
public static Type[] getParameterizedTypes(Object object) {
    Type superclassType = object.getClass().getGenericSuperclass();
    if (!ParameterizedType.class.isAssignableFrom(superclassType.getClass())) {
        return null;
    }

    return ((ParameterizedType) superclassType).getActualTypeArguments();
}

From source file:Main.java

public static Map<String, String> convertBeanToMap(Object bean)
        throws IllegalArgumentException, IllegalAccessException {
    Field[] fields = bean.getClass().getDeclaredFields();
    HashMap<String, String> data = new HashMap<String, String>();
    for (Field field : fields) {
        field.setAccessible(true);//from w  w  w.java 2  s .c  o  m
        data.put(field.getName(), (String) field.get(bean));
    }
    return data;
}

From source file:Main.java

private static List<Activity> getAllActivitiesHack() throws ClassNotFoundException, NoSuchMethodException,
        NoSuchFieldException, IllegalAccessException, InvocationTargetException {
    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);
    List<Activity> activitiesList = new ArrayList<Activity>(activities.size());

    for (Object activityRecord : activities.values()) {
        Class activityRecordClass = activityRecord.getClass();
        Field activityField = activityRecordClass.getDeclaredField("activity");
        activityField.setAccessible(true);
        Activity activity = (Activity) activityField.get(activityRecord);
        activitiesList.add(activity);//w  w  w.j  av  a2s .c o m
    }

    return activitiesList;
}

From source file:com.xemantic.tadedon.guice.lifecycle.jsr250.Jsr250Utils.java

public static void invoke(Class<? extends Annotation> jsr250Annotation, Iterable<Object> objects) {
    for (Object object : objects) {
        Method[] methods = object.getClass().getMethods();
        for (Method method : methods) {
            if (AnnotationUtils.findAnnotation(method, jsr250Annotation) != null) {
                try {
                    method.invoke(object);
                } catch (IllegalArgumentException e) {
                    handleException(jsr250Annotation, method, e);
                } catch (IllegalAccessException e) {
                    handleException(jsr250Annotation, method, e);
                } catch (InvocationTargetException e) {
                    handleException(jsr250Annotation, method, e);
                }//from   www .  j  a v  a  2  s .c  o m
            }
        }
    }
}

From source file:com.consol.citrus.admin.mock.Mocks.java

/**
 * Inject Spring autowired fields in target instance with mocks.
 * @param target//from w  w w  . j av  a  2  s . c  o  m
 */
public static void injectMocks(Object target) {
    ReflectionUtils.doWithFields(target.getClass(),
            field -> ReflectionUtils.setField(field, target, Mockito.mock(field.getType())), field -> {
                if (field.isAnnotationPresent(Autowired.class)) {
                    if (!field.isAccessible()) {
                        ReflectionUtils.makeAccessible(field);
                    }

                    return true;
                }

                return false;
            });
}

From source file:Main.java

public static <T> List<T> getRandomCollection(Class<T> collectionType) {
    int collectionSize = new Random().nextInt(10);
    List<T> collection = new ArrayList<T>();
    for (int i = 0; i < collectionSize; i++) {
        Object obj = getRandomValue(collectionType);
        if (obj != null && obj.getClass().isAssignableFrom(collectionType)) {
            collection.add((T) obj);// w ww  . ja v  a2  s  .  c  om
        }
    }
    return collection;
}

From source file:ReflectionHelper.java

@SuppressWarnings("unchecked")
public static <T> T getAnnotationParameter(Annotation annotation, String parameterName, Class<T> type) {
    try {/*ww w .j av a 2  s. c om*/
        Method m = annotation.getClass().getMethod(parameterName);
        Object o = m.invoke(annotation);
        if (o.getClass().getName().equals(type.getName())) {
            return (T) o;
        } else {
            String msg = "Wrong parameter type. Expected: " + type.getName() + " Actual: "
                    + o.getClass().getName();
            throw new RuntimeException(msg);
        }
    } catch (NoSuchMethodException e) {
        String msg = "The specified annotation defines no parameter '" + parameterName + "'.";
        throw new RuntimeException(msg, e);
    } catch (IllegalAccessException e) {
        String msg = "Unable to get '" + parameterName + "' from " + annotation.getClass().getName();
        throw new RuntimeException(msg, e);
    } catch (InvocationTargetException e) {
        String msg = "Unable to get '" + parameterName + "' from " + annotation.getClass().getName();
        throw new RuntimeException(msg, e);
    }
}

From source file:Main.java

public static <T> T callMethod(@NonNull Object obj, @NonNull String name, Object... args) throws Exception {
    for (Class cls = obj.getClass(); //
            cls != null && cls != Object.class; //
            cls = cls.getSuperclass()) {
        for (Method m : cls.getDeclaredMethods()) {
            if (m.getName().equals(name)) {
                m.setAccessible(true);//from  ww  w. java  2 s .  c  om
                //noinspection unchecked
                return (T) m.invoke(obj, args);
            }
        }
    }
    throw new RuntimeException("no method matching " + name);
}

From source file:Main.java

/**
 * Locates a given field anywhere in the class inheritance hierarchy.
 *
 * @param instance an object to search the field into.
 * @param name field name//  ww  w  .  j  av  a  2 s .  c om
 * @return a field object
 * @throws NoSuchFieldException if the field cannot be located
 */
static Field findField(Object instance, String name) throws NoSuchFieldException {
    for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
        try {
            Field field = clazz.getDeclaredField(name);

            if (!field.isAccessible()) {
                field.setAccessible(true);
            }

            return field;
        } catch (NoSuchFieldException e) {
            // ignore and search next
        }
    }

    throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass());
}

From source file:Main.java

public static Object coerce(Object val, Class clz) {
    if (val == null)
        return val;
    if (val.getClass() == clz)
        return val;
    if (val instanceof String && (clz == Double.class || clz == double.class))
        return convertDouble((String) val);
    if (val instanceof String && (clz == Integer.class || clz == int.class))
        return convertInteger((String) val);
    return val;
}