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

@SuppressWarnings("unchecked")
public static Method getMethod(Object target, String name, Class... parameterTypes) {
    String key = target.getClass().getSimpleName() + ":" + name + ":"
            + (parameterTypes == null ? 0 : parameterTypes.length);
    Method method = methods.get(key);
    if (method == null) {
        for (Class obj = target.getClass(); !obj.equals(Object.class); obj = obj.getSuperclass()) {
            try {
                method = obj.getDeclaredMethod(name, parameterTypes);
                method.setAccessible(true);
                methods.put(key, method);
                break;
            } catch (SecurityException e) {
                e.printStackTrace();/*  ww w.  ja va2 s  .  com*/
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
    }
    return method;
}

From source file:Main.java

public static boolean isEmptyArray(Object arrayObj) {
    if (null == arrayObj) {
        return true;
    }//ww w  .j  a v a  2 s .c o m

    Class clazz = arrayObj.getClass();

    if (!clazz.isArray()) {
        return true;
    }

    if (Array.getLength(arrayObj) == 0) {
        return true;
    }

    return false;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T objectCast(Object obj, Class<T> classToCast) {
    if (obj == null)
        return null;
    Class<? extends Object> curClass = obj.getClass();
    if (classToCast.isAssignableFrom(curClass))
        return (T) obj;
    return null;/*w  w  w  .  j  ava 2s  .  c o  m*/
}

From source file:net.eusashead.hateoas.header.impl.PropertyUtil.java

public static Object getValue(Object target, String name) {
    PropertyDescriptor property = BeanUtils.getPropertyDescriptor(target.getClass(), name);
    try {//www.j av a 2s .  c om
        return property.getReadMethod().invoke(target);
    } catch (Exception e) {
        throw new RuntimeException("Unable to get value for target " + target.getClass());
    }
}

From source file:Main.java

public static String[][] returnTable(Collection E) {
    if ((E == null) || E.isEmpty()) {
        System.out.println("The collection is empty!");
    }/*www.j  a  va 2s  . co  m*/

    Set<Field> collectionFields = new TreeSet<>(new Comparator<Field>() {
        @Override
        public int compare(Field o1, Field o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    for (Object o : E) {
        Class c = o.getClass();
        createSetOfFields(collectionFields, c);
        while (c.getSuperclass() != null) {
            c = c.getSuperclass();
            createSetOfFields(collectionFields, c);
        }
    }
    String[][] exitText = new String[E.size() + 1][collectionFields.size() + 1];
    exitText[0][0] = String.format("%20s", "Class");
    int indexOfColumn = 0;
    for (Field f : collectionFields) {
        exitText[0][indexOfColumn + 1] = String.format("%20s", f.getName());
        indexOfColumn++;

    }
    int indexOfRow = 0;
    for (Object o : E) {
        indexOfColumn = 0;
        exitText[indexOfRow + 1][0] = String.format("%20s", o.getClass().getSimpleName());
        for (Field field : collectionFields) {
            try {
                field.setAccessible(true);
                if (field.get(o) instanceof Date) {
                    exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20tD", field.get(o));
                } else {
                    String temp = String.valueOf(field.get(o));
                    exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20s", temp);
                }
            } catch (Exception e) {
                exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20s", "-");
            }
            indexOfColumn++;
        }
        indexOfRow++;
    }
    return exitText;
}

From source file:Main.java

public static Object findElementInstance(Object collection) {
    if (collection == null)
        return null;
    if (collection.getClass().isArray()) {
        for (int i = 0; i < Array.getLength(collection); i++) {
            Object o = Array.get(collection, i);
            if (o != null) {
                return o;
            }/*from w w w .  java 2 s . co m*/
        }
    } else if (collection instanceof Collection) {
        for (Object o : ((Collection<?>) collection)) {
            if (o != null) {
                return o;
            }
        }
    }
    return null;
}

From source file:Main.java

public static Object getObjectField(Object obj, String fieldName) {
    try {/*from  w w w .j ava 2  s.c o m*/
        return findField(obj.getClass(), fieldName).get(obj);
    } catch (IllegalAccessException e) {
        // should not happen
        Log.v("test", e.getMessage());
        throw new IllegalAccessError(e.getMessage());
    } catch (IllegalArgumentException e) {
        throw e;
    }
}

From source file:Main.java

public static boolean setReflection(Object itemToGetObject, String objectName, Object newValue) {
    try {//from ww w.  j ava2  s .  c  o  m
        Class<?> clazz = itemToGetObject.getClass();
        Field field;
        field = clazz.getDeclaredField(objectName);
        field.setAccessible(true);
        field.set(itemToGetObject, newValue);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:com.angstoverseer.util.ReflectionUtil.java

public static Method extractMethod(Object target, String methodName) {
    final Method[] declaredMethods = target.getClass().getDeclaredMethods();
    for (Method method : declaredMethods) {
        if (method.getName().equals(methodName)) {
            return method;
        }// w  w  w .  j a  v a 2  s.c  o m
    }

    throw new RuntimeException("Method not found: " + methodName);
}

From source file:com.threewks.thundr.view.jsp.el.CollectionFunctions.java

private static boolean isArray(Object object) {
    return object == null ? false : object.getClass().isArray();
}