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 void setObjValue(String methodName, Object object, String value) {
    try {//from  w  w  w  . jav a 2 s.  co  m
        Class<? extends Object> cls = object.getClass();
        cls.getMethod(methodName, String.class).invoke(object, value);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Converts an Object reference that is known to be an array into an
 * Object[]. If the array is assignable to Object[], the array passed in is
 * simply cast and returned. Otherwise a new Object[] of equal size is
 * constructed and the elements are wrapped and inserted into the new array
 * before being returned./*from   www.j a  v  a  2  s  .  c  om*/
 * 
 * @param in
 *            an array of Objects or primitives
 * @return an Object[], either the array passed in, or in the case of
 *         primitives, a new Object[] containing a wrapper for each element
 *         in the input array
 * @throws IllegalArgumentException
 *             thrown if the in parameter is null or not an array
 */
public static Object[] asObjectArray(Object in) {
    if (in == null || !in.getClass().isArray()) {
        throw new IllegalArgumentException("Parameter to asObjectArray must be a non-null array.");
    } else if (in instanceof Object[]) {
        return (Object[]) in;
    } else {
        int length = Array.getLength(in);
        Object[] out = new Object[length];
        for (int i = 0; i < length; ++i) {
            out[i] = Array.get(in, i);
        }

        return out;
    }
}

From source file:Main.java

/**
 * return image icon from resource location ( ie Classpath )
 * File could be placed in the classpath or in a jar file
 * @param aFilename file name of the image file
 * @param aRetriever object request for the image
 * @return image icon//from  w w w  . j ava 2s. co m
 */
public final static ImageIcon GetImageFromResource(String aFilename, Object aRetriever) {
    return GetImageFromResource(aFilename, aRetriever.getClass());
}

From source file:com.yoho.core.trace.DefaultSpanNamer.java

private static boolean isDefaultToString(Object delegate, String spanName) {
    return (delegate.getClass().getName() + "@" + Integer.toHexString(delegate.hashCode())).equals(spanName);
}

From source file:Main.java

/**
 * {@link Arrays#toString(Object[])} with {@link Arrays#toString(Object[])} for array elements
 *///from www.j a v  a 2s. c om
public static Object arrayToString(Object obj) {
    if (obj != null && obj.getClass().isArray()) {
        int len = Array.getLength(obj);
        String[] strings = new String[len];
        for (int i = 0; i < len; i++) {
            strings[i] = String.valueOf(Array.get(obj, i));
        }
        return Arrays.toString(strings);
    } else {
        return obj;
    }
}

From source file:Main.java

public static Object getAdditionalStaticField(Object obj, String key) {
    return getAdditionalInstanceField(obj.getClass(), key);
}

From source file:Main.java

public static String bean2XML(Object obj) throws Exception {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Marshaller m = context.createMarshaller();
    StringWriter sw = new StringWriter();
    m.marshal(obj, sw);//w w w  .  j a va2 s  .  com
    return sw.toString();
}

From source file:Main.java

/**
 * getChildObjs/*from   w  ww .  j a va  2 s .  com*/
 *
 * @param object the obj to save to db ,this object contains the property private List<Child> children;
 * @return the List<Child> value
 */
public static List<List> getChildObjs(Object object) {
    Field[] fields = object.getClass().getDeclaredFields();
    List<List> result = new ArrayList<>();
    for (Field field : fields) {
        if ("java.util.List".equals(field.getType().getName())) {
            List list = null;
            try {
                field.setAccessible(true);
                list = (List) field.get(object);
                result.add(list);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:Main.java

public static Object xml2Bean(String xml, Object obj) throws Exception {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Unmarshaller um = context.createUnmarshaller();
    StringReader sr = new StringReader(xml);
    return um.unmarshal(sr);

}

From source file:Main.java

private static Object getField(final Object obj, final String fieldName) {
    try {//from w w  w .  j  a v  a  2s  .  c  om
        Field f = obj.getClass().getField(fieldName);
        return f.get(obj);
    } catch (final Exception e) {
        Log.e(TAG, "Error getting field:", e);
    }
    return null;
}