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.job.portal.utils.BeanUtils.java

public static JSONObject convertToJSON(Object bean) {
    JSONObject obj = new JSONObject();
    try {/*from w w w .  j  a  v  a 2s  .  c  o m*/
        for (Field field : bean.getClass().getDeclaredFields()) {
            field.setAccessible(true); // You might want to set modifier to public first.
            Object value = field.get(bean);
            if (value != null) {
                obj.put(field.getName(), value);
            }
        }
    } catch (Exception e) {
        LogOut.log.error("In " + new Object() {
        }.getClass().getEnclosingClass().getName() + "." + new Object() {
        }.getClass().getEnclosingMethod().getName() + " " + e);
    }
    return obj;
}

From source file:Main.java

/**
 * override is set class/*w ww . j  a v a2s. c  om*/
 */
public static boolean isSet(Object obj) {
    if (obj == null) {
        return false;
    }
    if (Set.class.isAssignableFrom(obj.getClass())) {
        return true;
    }
    return false;
}

From source file:Main.java

private static String marshal(Object objJAXB) throws Exception {
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    newMarshaller(objJAXB.getClass()).marshal(objJAXB, newWriter(result));
    return result.toString();
}

From source file:Main.java

public static Object xmlToPojo(Object o, String arquivo) throws JAXBException {
    File file = new File(arquivo);

    JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    return jaxbUnmarshaller.unmarshal(file);

}

From source file:Main.java

public static Class<?> findCommonElementType(Collection collection) {
    if (isEmpty(collection))
        return null;
    Class<?> candidate = null;
    for (Object val : collection) {
        if (val != null) {
            if (candidate == null)
                candidate = val.getClass();
            else if (candidate != val.getClass())
                return null;
        }/*from w  w  w.  j  a  va  2  s .  c  o m*/
    }
    return candidate;
}

From source file:Main.java

public static String convertToXmlString(Object source) {
    try {/*from w  w  w.j a  v a  2s  . c  o m*/
        StringWriter sw = new StringWriter();
        JAXBContext jAXBContext = JAXBContext.newInstance(source.getClass());
        Marshaller marshaller = jAXBContext.createMarshaller();
        marshaller.marshal(source, sw);
        return sw.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.bxf.hradmin.common.utils.QueryParameterTransformer.java

@SuppressWarnings("rawtypes")
private static Object[] asArray(Object value) {
    if (value.getClass().isArray()) {
        Object[] result = new Object[Array.getLength(value)];
        for (int i = 0; i < result.length; ++i) {
            result[i] = Array.get(value, i);
        }// w ww  .  ja v a  2  s. c om
        return result;
    } else if (value instanceof Collection) {
        return ((Collection) value).toArray();
    }
    return new Object[] { value };
}

From source file:Main.java

public static Object invokeObjectMethod(Object object, String methodName, Class[] argsClass, Object[] args) {
    Object returnValue = null;/*ww w .  jav  a2  s  .com*/
    try {
        Class<?> c = object.getClass();
        Method method;
        method = c.getMethod(methodName, argsClass);
        returnValue = method.invoke(object, args);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return returnValue;
}

From source file:ObjectUtils.java

/**
 * Return a String representation of an object's overall identity.
 * @param obj the object (may be <code>null</code>)
 * @return the object's identity as String representation,
 * or an empty String if the object was <code>null</code>
 *///from w  w  w  . j a  va 2 s.c om
public static String identityToString(Object obj) {
    if (obj == null) {
        return EMPTY_STRING;
    }
    return obj.getClass().getName() + "@" + getIdentityHexString(obj);
}

From source file:koper.util.ReflectUtil.java

public static void invoke(Object targetObject, Method method, Object[] objects) {
    final Class<?> clazz = targetObject.getClass();
    final FastClass fastClass = FastClass.create(clazz);
    try {//  ww  w. j a  va  2  s .c  o m
        fastClass.getMethod(method).invoke(targetObject, objects);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}