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

/**
 * Serializes any Object to XML/*from ww w.  j  a  va 2  s  .c o  m*/
 *
 * @param object Object to serialize
 * @return XML serialization of the supplied object
 */
public static String toXmlJaxb(Object object) {
    String result = "";
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.marshal(object, writer);
        result = writer.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:Main.java

private static List<Object[]> parseEntityData(final Iterator<Object[]> dataIterator, final Class<?>[] classes)
        throws Exception {
    List<Object[]> list = new ArrayList<Object[]>();

    while (dataIterator.hasNext()) {

        Object[] rowDataArray = dataIterator.next();

        List<Object> rowData = new ArrayList<Object>();

        if (classes != null) {
            for (Object data : rowDataArray) {
                for (Class<?> clazz : classes) {
                    if (data.getClass() == clazz) {
                        rowData.add(data);
                    }/*from   ww w. j av a 2s .c om*/
                }

            }
        }

        list.add(rowData.toArray(new Object[] { rowData.size() }));
    }

    return list;
}

From source file:com.dangdang.ddframe.job.spring.util.AopTargetUtils.java

private static Object getTargetObject(final Object object) {
    try {/*ww w  . j a va  2  s.c o  m*/
        Field advised = object.getClass().getDeclaredField("advised");
        advised.setAccessible(true);
        return ((AdvisedSupport) advised.get(object)).getTargetSource().getTarget();
        // CHECKSTYLE:OFF
    } catch (final Exception ex) {
        // CHECKSTYLE:ON
        throw new JobException(ex);
    }
}

From source file:me.yyam.beanutils.BeanUtilEx.java

private static boolean isPrimitive(Object value) {
    return value instanceof String || value instanceof Date || value.getClass().isPrimitive()
            || value instanceof Integer || value instanceof Long || value instanceof Short
            || value instanceof Byte || value instanceof Character || value instanceof Float
            || value instanceof Double || value instanceof Boolean || value instanceof BigInteger
            || value instanceof BigDecimal;
}

From source file:Main.java

/**
 * Used to optimize performance by avoiding expensive file access every time
 * a DTMManager is constructed as a result of constructing a Xalan xpath
 * context!//from  w  w  w. ja  va 2  s. c o  m
 */
private static void speedUpDTMManager() throws Exception {
    // https://github.com/aws/aws-sdk-java/issues/238
    // http://stackoverflow.com/questions/6340802/java-xpath-apache-jaxp-implementation-performance
    if (System.getProperty(DTM_MANAGER_DEFAULT_PROP_NAME) == null) {
        Class<?> XPathContextClass = Class.forName(XPATH_CONTEXT_CLASS_NAME);
        Method getDTMManager = XPathContextClass.getMethod("getDTMManager");
        Object XPathContext = XPathContextClass.newInstance();
        Object dtmManager = getDTMManager.invoke(XPathContext);

        if (DTM_MANAGER_IMPL_CLASS_NAME.equals(dtmManager.getClass().getName())) {
            // This would avoid the file system to be accessed every time
            // the internal XPathContext is instantiated.
            System.setProperty(DTM_MANAGER_DEFAULT_PROP_NAME, DTM_MANAGER_IMPL_CLASS_NAME);
        }
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Object searchProperty(Object leftParameter, String name) throws Exception {
    Class<?> leftClass = leftParameter.getClass();
    Object result;//w  ww. ja va2s . c om
    if (leftParameter.getClass().isArray() && "length".equals(name)) {
        result = Array.getLength(leftParameter);
    } else if (leftParameter instanceof Map) {
        result = ((Map<Object, Object>) leftParameter).get(name);
    } else {
        try {
            String getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
            Method method = leftClass.getMethod(getter, new Class<?>[0]);
            if (!method.isAccessible()) {
                method.setAccessible(true);
            }
            result = method.invoke(leftParameter, new Object[0]);
        } catch (NoSuchMethodException e2) {
            try {
                String getter = "is" + name.substring(0, 1).toUpperCase() + name.substring(1);
                Method method = leftClass.getMethod(getter, new Class<?>[0]);
                if (!method.isAccessible()) {
                    method.setAccessible(true);
                }
                result = method.invoke(leftParameter, new Object[0]);
            } catch (NoSuchMethodException e3) {
                Field field = leftClass.getField(name);
                result = field.get(leftParameter);
            }
        }
    }
    return result;
}

From source file:Main.java

public static <T> T getMember(Object object, String member, Class<T> klass)
        throws NoSuchFieldException, IllegalAccessException {
    Field field = getField(object.getClass(), member);
    field.setAccessible(true);/*w w w.j a v  a2s.c  o  m*/
    return (T) field.get(object);
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static void marshal(Object object, Writer w) throws JAXBException {
    Class clazz = object.getClass();
    JAXBContext context = s_contexts.get(clazz);
    if (context == null) {
        context = JAXBContext.newInstance(clazz);
        s_contexts.put(clazz, context);/*from  ww  w. j ava2 s  .c om*/
    }

    ValidationEventCollector valEventHndlr = new ValidationEventCollector();
    Marshaller marshaller = context.createMarshaller();
    marshaller.setSchema(null);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setEventHandler(valEventHndlr);

    try {
        marshaller.marshal(object, w);
    } catch (Exception e) {
        if (e instanceof JAXBException) {
            throw (JAXBException) e;
        } else {
            throw new MarshalException(e.getMessage(), e);
        }
    }
    if (valEventHndlr.hasEvents()) {
        for (ValidationEvent valEvent : valEventHndlr.getEvents()) {
            if (valEvent.getSeverity() != ValidationEvent.WARNING) {
                // throw a new Marshall Exception if there is a parsing error
                throw new MarshalException(valEvent.getMessage(), valEvent.getLinkedException());
            }
        }
    }
}

From source file:br.com.modoagil.util.ReflectionUtil.java

/**
 * Seta em um atributo de uma classe o atributo com nome '{@code name}' o valor '{@code value}' no objeto desta instncia
 *
 * @param target/*from w  w w .  ja v  a2  s  .co  m*/
 *            objeto a ter o campo e valores setados
 * @param name
 *            nome do atributo a ser setado o contedo
 * @param value
 *            contedo a ser setado
 * @see ReflectionUtils#findField(Class, String)
 * @see ReflectionUtils#makeAccessible(Field)
 * @see ReflectionUtils#setField(Field, Object, Object)
 */
public static void setField(final Object target, final String name, final Object value) {
    final Field field = ReflectionUtils.findField(target.getClass(), name);
    ReflectionUtils.makeAccessible(field);
    ReflectionUtils.setField(field, target, value);
}

From source file:com.github.tddts.jet.util.SpringUtil.java

/**
 * Checks if given bean is a dynamic proxy and if it is so returns actual bean behind proxy and it's type.
 *
 * @param bean bean object//from  w w w  .j av a 2s.  c o  m
 * @return pair containing of bean and it's class
 * @throws BeanInitializationException in case of any exception
 */
public static Pair<Class<?>, Object> checkForDinamicProxy(Object bean) throws BeanInitializationException {
    try {
        Class<?> type = bean.getClass();
        if (AopUtils.isJdkDynamicProxy(bean)) {
            Advised advised = (Advised) bean;
            type = advised.getTargetClass();
            bean = advised.getTargetSource().getTarget();
        }
        return Pair.of(type, bean);
    } catch (Exception e) {
        throw new BeanInitializationException(e.getMessage(), e);
    }
}