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.activecq.experiments.activedecorator.mapdecorators.base.AbstractMapDecorator.java

protected static boolean isArray(final Object obj) {
    return obj.getClass().isArray();
}

From source file:Main.java

public static void objectCopy(Object from, Object to) throws Exception {

    if (from.getClass() != to.getClass()) {
        throw new IllegalArgumentException("[objectCopy]The left and right must be same class");
    }//from  ww  w . j a v a  2 s  .  co  m
    Class<?> clz = from.getClass();
    Field[] fs = clz.getDeclaredFields();
    for (int i = 0; i < fs.length; i++) {
        Field field = fs[i];

        field.setAccessible(true);

        Object value = field.get(from);
        field.set(to, value);
    }
}

From source file:Main.java

public static void marshal(Object o, OutputStream os) throws JAXBException {
    getMarshaller(o.getClass()).marshal(o, os);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E getProperty(Object object, String fieldName) {
    Class<?> clazz = object.getClass();
    while (clazz != null) {
        try {//  w  ww  .  j  a v  a2 s.  co  m
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            return (E) field.get(object);
        } catch (NoSuchFieldException e) {
            clazz = clazz.getSuperclass();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
    return null;
}

From source file:Main.java

public static Object fieldGet(Object object, String fieldName) throws Exception {
    Field field = object.getClass().getDeclaredField(fieldName);
    field.setAccessible(true);//from w w w  .  j a  v a  2s.c  o  m
    return field.get(object);
}

From source file:Main.java

public static void setSP(Context context, String key, Object object) {
    String type = object.getClass().getSimpleName();
    String packageName = context.getPackageName();
    SharedPreferences sp = context.getSharedPreferences(packageName, Context.MODE_PRIVATE);
    Editor edit = sp.edit();//from   w w w.  ja v  a 2s. c o  m
    if ("String".equals(type)) {
        edit.putString(key, (String) object);
    } else if ("Integer".equals(type)) {
        edit.putInt(key, (Integer) object);
    } else if ("Boolean".equals(type)) {
        edit.putBoolean(key, (Boolean) object);
    } else if ("Float".equals(type)) {
        edit.putFloat(key, (Float) object);
    } else if ("Long".equals(type)) {
        edit.putLong(key, (Long) object);
    }
    edit.apply();
}

From source file:Main.java

public static Field findField(Object src, String fieldName) throws Exception {
    return findField(src.getClass(), fieldName);
}

From source file:Main.java

public static Collection invokeForEach(Collection in, String methodName, Object[] args)
        throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException {
    Iterator<Object> it = in.iterator();

    Class[] argsClasses = new Class[args.length];
    for (int i = 0; i < args.length; i++)
        argsClasses[i] = args[i].getClass();

    LinkedList<Object> out = new LinkedList<Object>();

    while (it.hasNext()) {
        Object obj = it.next();

        Method m = obj.getClass().getMethod(methodName, argsClasses);
        Object value2 = m.invoke(obj, args);
        out.add(value2);//w ww .java2  s .  co m

    }
    return out;
}

From source file:blue.utility.ValuesUtility.java

public static void checkNullString(Object obj, boolean printMessages) {
    Class c = obj.getClass();

    Field[] fields = c.getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        if (fields[i].getType() == String.class) {
            try {
                if (fields[i].get(obj) == null) {
                    if (printMessages) {
                        System.err.println("ValuesUtility: Null String found in " + c.getName() + " field: "
                                + fields[i].getName());
                    }//  w w  w.java  2  s  . co m
                    fields[i].set(obj, "");
                }
            } catch (IllegalAccessException iae) {
                iae.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static Object getValueByMethodName(Object o, String methodName) {
    try {//  www .  j  a  va 2s. c o  m
        Method method = o.getClass().getMethod(methodName, new Class[] {});
        Object value = method.invoke(o, new Object[] {});
        return value;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}