Example usage for java.io Serializable getClass

List of usage examples for java.io Serializable getClass

Introduction

In this page you can find the example usage for java.io Serializable getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.cleandroid.core.rest.JSONMapper.java

@SuppressWarnings("unchecked")
public static String parseJson(Serializable object) {
    if (TypeUtils.isPrimitiveOrWrapper(object.getClass()))
        return String.valueOf(object);

    String jsonString = "{";
    for (Field f : object.getClass().getDeclaredFields()) {
        f.setAccessible(true);//from   www .  java  2 s .c  o  m
        jsonString = jsonString.concat(f.getName()).concat(":");
        try {
            Object value = null;

            if (Collection.class.isAssignableFrom(f.getType()))
                value = parseJson((Collection<Serializable>) value);
            else {
                value = String.valueOf(f.get(object));
                if (TypeUtils.isTypeInRange(value, String.class, CharSequence.class))
                    value = "\"".concat(String.valueOf(value)).concat("\"");
            }

            jsonString = jsonString.concat(String.valueOf(value)).concat(", ");
        }

        catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
    jsonString = StringUtils.rtrim(jsonString, ", ").concat("}");

    return jsonString;
}

From source file:com.reactive.hzdfs.utils.Digestor.java

/**
 * Deep clone of an object having a default constructor.
 * @param o/*w  ww. ja v a 2  s.c o m*/
 * @return
 * @throws IOException ignored silently
 * @throws ClassNotFoundException ignored silently
 */
@SuppressWarnings("unchecked")
public static <T> T deepCopy(Serializable o) {
    try {
        T t = (T) o.getClass().newInstance();
        BeanUtils.copyProperties(o, t);

        return t;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

From source file:org.seadva.dataone.SeadQueryService.java

static String marshal(Serializable object) throws JiBXException, TransformerException {
    IBindingFactory bfact = BindingDirectory.getFactory(object.getClass());
    IMarshallingContext mctx = bfact.createMarshallingContext();

    StringWriter outputWriter = new StringWriter();

    mctx.marshalDocument(object, "UTF-8", true, outputWriter);

    StringWriter finaloutWriter = new StringWriter();
    transformer.transform(new StreamSource(new StringReader(outputWriter.toString())),
            new StreamResult(finaloutWriter));
    return finaloutWriter.toString();
}

From source file:org.nuxeo.ecm.platform.audit.impl.ExtendedInfoImpl.java

public static ExtendedInfoImpl createExtendedInfo(Serializable value) {
    Class<?> clazz = value.getClass();
    if (Long.class.isAssignableFrom(clazz)) {
        return new LongInfo((Long) value);
    }/*from   w w w  . j a v a 2 s.  c  o  m*/
    if (Double.class.isAssignableFrom(clazz)) {
        return new DoubleInfo((Double) value);
    }
    if (Date.class.isAssignableFrom(clazz)) {
        return new DateInfo((Date) value);
    }
    if (String.class.isAssignableFrom(clazz)) {
        return new StringInfo((String) value);
    }
    if (Boolean.class.isAssignableFrom(clazz)) {
        return new BooleanInfo((Boolean) value);
    }
    return new BlobInfo(value);
}

From source file:org.apache.jmeter.junit.JMeterTest.java

private static Test suiteSerializableElements() throws Exception {
    TestSuite suite = new TestSuite("SerializableElements");
    for (Object o : getObjects(Serializable.class)) {
        Serializable serObj = (Serializable) o;
        if (serObj.getClass().getName().endsWith("_Stub")) {
            continue;
        }/*from w w w  . ja  v  a 2 s .c  om*/
        TestSuite ts = new TestSuite(serObj.getClass().getName());
        ts.addTest(new JMeterTest("runSerialTest", serObj));
        suite.addTest(ts);
    }
    return suite;
}

From source file:org.shept.persistence.provider.hibernate.HibernateUtils_old.java

public static Object copyTemplate(HibernateDaoSupport dao, Object entityModelTemplate) {
    if (entityModelTemplate != null) {
        // hier besser die Metadaten von Hibernate fragen
        if (null != getClassMetadata(dao, entityModelTemplate)) {
            //         if (null != AnnotationUtils.findAnnotation(entityModelTemplate.getClass(), Entity.class)) {
            String idName = HibernateUtils_old.getIdentifierPropertyName(dao, entityModelTemplate);
            Object newModel = BeanUtils.instantiateClass(entityModelTemplate.getClass());
            BeanUtils.copyProperties(entityModelTemplate, newModel, new String[] { idName });

            Serializable idx = getIdValue(dao, entityModelTemplate);

            ClassMetadata meta = getClassMetadata(dao, idx);
            Type type = meta.getIdentifierType();

            if (meta != null && type.isComponentType()) {
                // alternaitv               if (id != null && (null != AnnotationUtils.findAnnotation(id.getClass(), Embeddable.class))) {
                Serializable copyId = BeanUtils.instantiate(idx.getClass());
                BeanUtils.copyProperties(idx, copyId);
                Method idMth = ReflectionUtils.findMethod(entityModelTemplate.getClass(),
                        "set" + StringUtils.capitalize(idName), new Class[] {});
                if (idMth != null) {
                    ReflectionUtils.invokeMethod(idMth, newModel, copyId);
                }/* w ww.  ja  va  2s. c  o  m*/
            }
            return newModel;
        }
    }
    return null;
}

From source file:com.ebay.erl.mobius.util.SerializableUtil.java

public final static String serializeToBase64(Serializable obj) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {//from   w w  w . j a  v  a  2s  .co  m
        oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();

        String result = new String(Base64.encodeBase64(bos.toByteArray()), "UTF-8");
        return result;
    } catch (NotSerializableException e) {
        throw new IllegalArgumentException("Cannot serialize " + obj.getClass().getCanonicalName(), e);
    } finally {
        try {
            bos.close();
        } catch (Throwable e) {
            e = null;
        }

        try {
            if (oos != null)
                oos.close();
        } catch (Throwable e) {
            e = null;
        }
    }
}

From source file:io.cloudslang.lang.compiler.validator.PreCompileValidatorImpl.java

public static void validateStringValue(String errorMessagePrefix, Serializable value) {
    if (value != null && !(value instanceof String)) {
        throw new RuntimeException(errorMessagePrefix + "' should have a String value, but got value '" + value
                + "' of type " + value.getClass().getSimpleName() + ".");
    }/*from   w w  w .jav  a 2s  .  co m*/
}

From source file:io.coala.factory.ClassUtil.java

/**
 * Write the object to a Base64 string// w w  w. j  a v  a2  s .  c om
 * 
 * @param object the {@link Serializable} object to serialize
 * @return the {@link String} representation of the {@link Serializable}
 *         object
 * @throws CoalaException
 */
public static String serialize(final Serializable object) throws CoalaException {
    try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            final ObjectOutputStream oos = new ObjectOutputStream(baos)) {
        oos.writeObject(object);
        final byte[] data = baos.toByteArray();
        LOG.trace("Serialized:\n\n" + new String(data));
        return new String(CODER.encode(data));
    } catch (final Throwable e) {
        throw CoalaExceptionFactory.MARSHAL_FAILED.create(e, object, object.getClass());
    }
}

From source file:org.objectstyle.cayenne.util.Util.java

/**
 * Creates Serializable object copy using serialization/deserialization.
 *///ww  w  . j a  v  a 2  s  .co m
public static Object cloneViaSerialization(Serializable obj) throws Exception {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bytes);
    out.writeObject(obj);
    out.close();

    byte[] data = bytes.toByteArray();

    if (logObj.isDebugEnabled()) {
        logObj.debug("Serialized size (bytes) of " + obj.getClass().getName() + ": " + data.length);
    }

    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data));
    Object objCopy = in.readObject();
    in.close();
    return objCopy;
}