Java Code Examples for java.io.Serializable#getClass()

The following examples show how to use java.io.Serializable#getClass() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Gets the value for the named parameter. Checks the type of the parameter
 * is correct and throws a {@link RenditionServiceException} if it isn't.
 * Returns <code>null</code> if the parameter value is <code>null</code>
 * 
 * @param paramName the name of the parameter being checked.
 * @param clazz the expected {@link Class} of the parameter value.
 * @param definition the {@link RenditionDefinition} containing the
 *            parameters.
 * @return the parameter value or <code>null</code>.
 */
@SuppressWarnings("unchecked")
public static <T> T getCheckedParam(String paramName, Class<T> clazz, RenditionDefinition definition)
{
    Serializable value = definition.getParameterValue(paramName);
    if (value == null)
        return null;
    else
    {
        if(clazz == null)
            throw new RenditionServiceException("The class must not be null!", new NullPointerException());
        Class<? extends Serializable> valueClass = value.getClass();
        if ( !clazz.isAssignableFrom(valueClass))
        {
            throw new RenditionServiceException("The parameter: " + paramName + " must be of type: "
                        + clazz.getName() + "but was of type: " + valueClass.getName());
        }
        else
            return (T) value;
    }
}
 
Example 2
Source Project: flow   File: AbstractBasicModelType.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Converts the given model value to the application type of this model
 * type. The conversion automatically handles default values for primitive
 * types and conversion of client-originated numbers to the expected Java
 * number type.
 *
 * @param modelValue
 *            the model value to convert
 * @return the converted value, not <code>null</code> if the application
 *         type is a primitive
 */
protected Object convertToApplication(Serializable modelValue) {
    if (modelValue == null && getJavaType().isPrimitive()) {
        return ReflectTools.getPrimitiveDefaultValue(getJavaType());
    }
    if (modelValue == null) {
        return null;
    }

    Class<?> convertedJavaType = ReflectTools
            .convertPrimitiveType(getJavaType());

    // Numeric value from the client is always Double
    if (modelValue instanceof Double
            && convertedJavaType == Integer.class) {
        modelValue = Integer.valueOf(((Double) modelValue).intValue());
    }

    if (convertedJavaType == modelValue.getClass()) {
        return modelValue;
    } else {
        throw new IllegalArgumentException(String.format(
                "The stored model value '%s' type '%s' "
                        + "cannot be used as a type for a model property with type '%s'",
                modelValue, modelValue.getClass().getName(),
                getJavaType().getName()));
    }
}
 
Example 3
/**
 * Checks that the given object serializes to the expected class.
 */
static void checkSerialClassName(Serializable obj, String expected) {
    String cn = serialClassName(obj);
    if (!cn.equals(expected))
        throw new RuntimeException(obj.getClass() + " serialized as " + cn
            + ", expected " + expected);
}
 
Example 4
Source Project: jdk8u60   File: Serial.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks that the given object serializes to the expected class.
 */
static void checkSerialClassName(Serializable obj, String expected) {
    String cn = serialClassName(obj);
    if (!cn.equals(expected))
        throw new RuntimeException(obj.getClass() + " serialized as " + cn
            + ", expected " + expected);
}
 
Example 5
Source Project: jdk8u-jdk   File: Serial.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks that the given object serializes to the expected class.
 */
static void checkSerialClassName(Serializable obj, String expected) {
    String cn = serialClassName(obj);
    if (!cn.equals(expected))
        throw new RuntimeException(obj.getClass() + " serialized as " + cn
            + ", expected " + expected);
}
 
Example 6
Source Project: jdk8u_jdk   File: Serial.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks that the given object serializes to the expected class.
 */
static void checkSerialClassName(Serializable obj, String expected) {
    String cn = serialClassName(obj);
    if (!cn.equals(expected))
        throw new RuntimeException(obj.getClass() + " serialized as " + cn
            + ", expected " + expected);
}
 
Example 7
/**
 * Check ObjectStreamClass facts match between core serialization and CORBA.
 *
 * @param value
 */
@Test(dataProvider = "Objects")
void factCheck(Serializable value) {
    Class<?> clazz = value.getClass();
    java.io.ObjectStreamClass sOSC = java.io.ObjectStreamClass.lookup(clazz);
    java.io.ObjectStreamField[] sFields = sOSC.getFields();
    com.sun.corba.se.impl.io.ObjectStreamClass cOSC = corbaLookup(clazz);
    com.sun.corba.se.impl.io.ObjectStreamField[] cFields = cOSC.getFields();

    Assert.assertEquals(sFields.length, cFields.length, "Different number of fields");
    for (int i = 0; i < sFields.length; i++) {
        Assert.assertEquals(sFields[i].getName(), cFields[i].getName(),
                "different field names " + cFields[i].getName());
        Assert.assertEquals(sFields[i].getType(), cFields[i].getType(),
                "different field types " + cFields[i].getName());
        Assert.assertEquals(sFields[i].getTypeString(), cFields[i].getTypeString(),
                "different field typestrings " + cFields[i].getName());
    }

    Assert.assertEquals(baseMethod("hasReadObjectMethod", sOSC, (Class<?>[]) null),
            corbaMethod("hasReadObject", cOSC, (Class<?>[]) null),
            "hasReadObject: " + value.getClass());

    Assert.assertEquals(baseMethod("hasWriteObjectMethod", sOSC, (Class<?>[]) null),
            corbaMethod("hasWriteObject", cOSC, (Class<?>[]) null),
            "hasWriteObject: " + value.getClass());

    Assert.assertEquals(baseMethod("hasWriteReplaceMethod", sOSC, (Class<?>[]) null),
            corbaMethod("hasWriteReplaceMethod", cOSC, (Class<?>[]) null),
            "hasWriteReplace: " + value.getClass());

    Assert.assertEquals(baseMethod("hasReadResolveMethod", sOSC, (Class<?>[]) null),
            corbaMethod("hasReadResolveMethod", cOSC, (Class<?>[]) null),
            "hasReadResolve: " + value.getClass());

    Assert.assertEquals(baseMethod("getSerialVersionUID", sOSC, (Class<?>[]) null),
            corbaMethod("getSerialVersionUID", cOSC, (Class<?>[]) null),
            "getSerialVersionUID: " + value.getClass());

}
 
Example 8
Source Project: hottub   File: Serial.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks that the given object serializes to the expected class.
 */
static void checkSerialClassName(Serializable obj, String expected) {
    String cn = serialClassName(obj);
    if (!cn.equals(expected))
        throw new RuntimeException(obj.getClass() + " serialized as " + cn
            + ", expected " + expected);
}
 
Example 9
/**
 * Checks that the given object serializes to the expected class.
 */
static void checkSerialClassName(Serializable obj, String expected) {
    String cn = serialClassName(obj);
    if (!cn.equals(expected))
        throw new RuntimeException(obj.getClass() + " serialized as " + cn
            + ", expected " + expected);
}
 
Example 10
/**
 * Helper method to convert the <code>Serializable</code> value into a full, persistable {@link NodePropertyValue}.
 * <p>
 * Where the property definition is null, the value will take on the {@link DataTypeDefinition#ANY generic ANY}
 * value.
 * <p>
 * Collections are NOT supported. These must be split up by the calling code before calling this method. Map
 * instances are supported as plain serializable instances.
 * 
 * @param propertyDef the property dictionary definition, may be null
 * @param value the value, which will be converted according to the definition - may be null
 * @return Returns the persistable property value
 */
public NodePropertyValue makeNodePropertyValue(PropertyDefinition propertyDef, Serializable value)
{
    // get property attributes
    final QName propertyTypeQName;
    if (propertyDef == null) // property not recognised
    {
        // allow it for now - persisting excess properties can be useful sometimes
        propertyTypeQName = DataTypeDefinition.ANY;
    }
    else
    {
        propertyTypeQName = propertyDef.getDataType().getName();
    }
    try
    {
        NodePropertyValue propertyValue = null;
        propertyValue = new NodePropertyValue(propertyTypeQName, value);

        // done
        return propertyValue;
    }
    catch (TypeConversionException e)
    {
        throw new TypeConversionException(
                "The property value is not compatible with the type defined for the property: \n" +
                "   property: " + (propertyDef == null ? "unknown" : propertyDef) + "\n" +
                "   value: " + value + "\n" +
                "   value type: " + value.getClass(),
                e);
    }
}
 
Example 11
Source Project: redkale   File: EntityCache.java    License: Apache License 2.0 4 votes vote down vote up
private <V> T updateColumn(Attribute<T, V> attr, final T entity, final ColumnExpress express, Serializable val) {
    final Class ft = attr.type();
    Number numb = null;
    Serializable newval = null;
    switch (express) {
        case INC:
        case DEC:
        case MUL:
        case DIV:
        case MOD:
        case AND:
        case ORR:
            numb = getValue((Number) attr.get(entity), express, val);
            break;
        case MOV:
            if (val instanceof ColumnNodeValue) val = updateColumnNodeValue(attr, entity, (ColumnNodeValue) val);
            newval = val;
            if (val instanceof Number) numb = (Number) val;
            break;
    }
    if (numb != null) {
        if (ft == int.class || ft == Integer.class) {
            newval = numb.intValue();
        } else if (ft == long.class || ft == Long.class) {
            newval = numb.longValue();
        } else if (ft == short.class || ft == Short.class) {
            newval = numb.shortValue();
        } else if (ft == float.class || ft == Float.class) {
            newval = numb.floatValue();
        } else if (ft == double.class || ft == Double.class) {
            newval = numb.doubleValue();
        } else if (ft == byte.class || ft == Byte.class) {
            newval = numb.byteValue();
        } else if (ft == AtomicInteger.class) {
            newval = new AtomicInteger(numb.intValue());
        } else if (ft == AtomicLong.class) {
            newval = new AtomicLong(numb.longValue());
        }
    } else {
        if (ft == AtomicInteger.class && newval != null && newval.getClass() != AtomicInteger.class) {
            newval = new AtomicInteger(((Number) newval).intValue());
        } else if (ft == AtomicLong.class && newval != null && newval.getClass() != AtomicLong.class) {
            newval = new AtomicLong(((Number) newval).longValue());
        }
    }
    attr.set(entity, (V) newval);
    return entity;
}
 
Example 12
public void write_value(Serializable object, String repository_id) {

        // Handle null references
        if (object == null) {
            // Write null tag and return
            write_long(0);
            return;
        }

        // Handle shared references
        if (valueCache != null && valueCache.containsKey(object)) {
            writeIndirection(INDIRECTION_TAG, valueCache.getVal(object));
            return;
        }

        Class clazz = object.getClass();
        boolean oldMustChunk = mustChunk;

        if (mustChunk)
            mustChunk = true;

        if (inBlock)
            end_block();

        if (clazz.isArray()) {
            // Handle arrays
            writeArray(object, clazz);
        } else if (object instanceof org.omg.CORBA.portable.ValueBase) {
            // Handle IDL Value types
            writeValueBase((org.omg.CORBA.portable.ValueBase)object, clazz);
        } else if (shouldWriteAsIDLEntity(object)) {
            writeIDLEntity((IDLEntity)object);
        } else if (object instanceof java.lang.String) {
            writeWStringValue((String)object);
        } else if (object instanceof java.lang.Class) {
            writeClass(repository_id, (Class)object);
        } else {
            // RMI-IIOP value type
            writeRMIIIOPValueType(object, clazz);
        }

        mustChunk = oldMustChunk;

        // Check to see if we need to start another block for a
        // possible outer value
        if (mustChunk)
            start_block();

    }
 
Example 13
public void write_value(Serializable object, String repository_id) {

        // Handle null references
        if (object == null) {
            // Write null tag and return
            write_long(0);
            return;
        }

        // Handle shared references
        if (valueCache != null && valueCache.containsKey(object)) {
            writeIndirection(INDIRECTION_TAG, valueCache.getVal(object));
            return;
        }

        Class clazz = object.getClass();
        boolean oldMustChunk = mustChunk;

        if (mustChunk)
            mustChunk = true;

        if (inBlock)
            end_block();

        if (clazz.isArray()) {
            // Handle arrays
            writeArray(object, clazz);
        } else if (object instanceof org.omg.CORBA.portable.ValueBase) {
            // Handle IDL Value types
            writeValueBase((org.omg.CORBA.portable.ValueBase)object, clazz);
        } else if (shouldWriteAsIDLEntity(object)) {
            writeIDLEntity((IDLEntity)object);
        } else if (object instanceof java.lang.String) {
            writeWStringValue((String)object);
        } else if (object instanceof java.lang.Class) {
            writeClass(repository_id, (Class)object);
        } else {
            // RMI-IIOP value type
            writeRMIIIOPValueType(object, clazz);
        }

        mustChunk = oldMustChunk;

        // Check to see if we need to start another block for a
        // possible outer value
        if (mustChunk)
            start_block();

    }
 
Example 14
public void write_value(Serializable object, String repository_id) {

        // Handle null references
        if (object == null) {
            // Write null tag and return
            write_long(0);
            return;
        }

        // Handle shared references
        if (valueCache != null && valueCache.containsKey(object)) {
            writeIndirection(INDIRECTION_TAG, valueCache.getVal(object));
            return;
        }

        Class clazz = object.getClass();
        boolean oldMustChunk = mustChunk;

        if (mustChunk)
            mustChunk = true;

        if (inBlock)
            end_block();

        if (clazz.isArray()) {
            // Handle arrays
            writeArray(object, clazz);
        } else if (object instanceof org.omg.CORBA.portable.ValueBase) {
            // Handle IDL Value types
            writeValueBase((org.omg.CORBA.portable.ValueBase)object, clazz);
        } else if (shouldWriteAsIDLEntity(object)) {
            writeIDLEntity((IDLEntity)object);
        } else if (object instanceof java.lang.String) {
            writeWStringValue((String)object);
        } else if (object instanceof java.lang.Class) {
            writeClass(repository_id, (Class)object);
        } else {
            // RMI-IIOP value type
            writeRMIIIOPValueType(object, clazz);
        }

        mustChunk = oldMustChunk;

        // Check to see if we need to start another block for a
        // possible outer value
        if (mustChunk)
            start_block();

    }
 
Example 15
private void writeRMIIIOPValueType(Serializable object, Class clazz) {
    if (valueHandler == null)
        valueHandler = ORBUtility.createValueHandler(); //d11638

    Serializable key = object;

    // Allow the ValueHandler to call writeReplace on
    // the Serializable (if the method is present)
    object = valueHandler.writeReplace(key);

    if (object == null) {
        // Write null tag and return
        write_long(0);
        return;
    }

    if (object != key) {
        if (valueCache != null && valueCache.containsKey(object)) {
            writeIndirection(INDIRECTION_TAG, valueCache.getVal(object));
            return;
        }

        clazz = object.getClass();
    }

    if (mustChunk || valueHandler.isCustomMarshaled(clazz)) {
        mustChunk = true;
    }

    // Write value_tag
    int indirection = writeValueTag(mustChunk, true, Util.getCodebase(clazz));

    // Write rep. id
    write_repositoryId(repIdStrs.createForJavaType(clazz));

    // Add indirection for object to indirection table
    updateIndirectionTable(indirection, object, key);

    if (mustChunk) {
        // Write Value chunk
        end_flag--;
        chunkedValueNestingLevel--;
        start_block();
    } else
        end_flag--;

    if (valueHandler instanceof ValueHandlerMultiFormat) {
        ValueHandlerMultiFormat vh = (ValueHandlerMultiFormat)valueHandler;
        vh.writeValue(parent, object, streamFormatVersion);
    } else
        valueHandler.writeValue(parent, object);

    if (mustChunk)
        end_block();

    // Write end tag
    writeEndTag(mustChunk);
}
 
Example 16
private void writeRMIIIOPValueType(Serializable object, Class clazz) {
    if (valueHandler == null)
        valueHandler = ORBUtility.createValueHandler(); //d11638

    Serializable key = object;

    // Allow the ValueHandler to call writeReplace on
    // the Serializable (if the method is present)
    object = valueHandler.writeReplace(key);

    if (object == null) {
        // Write null tag and return
        write_long(0);
        return;
    }

    if (object != key) {
        if (valueCache != null && valueCache.containsKey(object)) {
            writeIndirection(INDIRECTION_TAG, valueCache.getVal(object));
            return;
        }

        clazz = object.getClass();
    }

    if (mustChunk || valueHandler.isCustomMarshaled(clazz)) {
        mustChunk = true;
    }

    // Write value_tag
    int indirection = writeValueTag(mustChunk, true, Util.getCodebase(clazz));

    // Write rep. id
    write_repositoryId(repIdStrs.createForJavaType(clazz));

    // Add indirection for object to indirection table
    updateIndirectionTable(indirection, object, key);

    if (mustChunk) {
        // Write Value chunk
        end_flag--;
        chunkedValueNestingLevel--;
        start_block();
    } else
        end_flag--;

    if (valueHandler instanceof ValueHandlerMultiFormat) {
        ValueHandlerMultiFormat vh = (ValueHandlerMultiFormat)valueHandler;
        vh.writeValue(parent, object, streamFormatVersion);
    } else
        valueHandler.writeValue(parent, object);

    if (mustChunk)
        end_block();

    // Write end tag
    writeEndTag(mustChunk);
}
 
Example 17
public void write_value(Serializable object, String repository_id) {

        // Handle null references
        if (object == null) {
            // Write null tag and return
            write_long(0);
            return;
        }

        // Handle shared references
        if (valueCache != null && valueCache.containsKey(object)) {
            writeIndirection(INDIRECTION_TAG, valueCache.getVal(object));
            return;
        }

        Class clazz = object.getClass();
        boolean oldMustChunk = mustChunk;

        if (mustChunk)
            mustChunk = true;

        if (inBlock)
            end_block();

        if (clazz.isArray()) {
            // Handle arrays
            writeArray(object, clazz);
        } else if (object instanceof org.omg.CORBA.portable.ValueBase) {
            // Handle IDL Value types
            writeValueBase((org.omg.CORBA.portable.ValueBase)object, clazz);
        } else if (shouldWriteAsIDLEntity(object)) {
            writeIDLEntity((IDLEntity)object);
        } else if (object instanceof java.lang.String) {
            writeWStringValue((String)object);
        } else if (object instanceof java.lang.Class) {
            writeClass(repository_id, (Class)object);
        } else {
            // RMI-IIOP value type
            writeRMIIIOPValueType(object, clazz);
        }

        mustChunk = oldMustChunk;

        // Check to see if we need to start another block for a
        // possible outer value
        if (mustChunk)
            start_block();

    }
 
Example 18
private void writeRMIIIOPValueType(Serializable object, Class clazz) {
    if (valueHandler == null)
        valueHandler = ORBUtility.createValueHandler(); //d11638

    Serializable key = object;

    // Allow the ValueHandler to call writeReplace on
    // the Serializable (if the method is present)
    object = valueHandler.writeReplace(key);

    if (object == null) {
        // Write null tag and return
        write_long(0);
        return;
    }

    if (object != key) {
        if (valueCache != null && valueCache.containsKey(object)) {
            writeIndirection(INDIRECTION_TAG, valueCache.getVal(object));
            return;
        }

        clazz = object.getClass();
    }

    if (mustChunk || valueHandler.isCustomMarshaled(clazz)) {
        mustChunk = true;
    }

    // Write value_tag
    int indirection = writeValueTag(mustChunk, true, Util.getCodebase(clazz));

    // Write rep. id
    write_repositoryId(repIdStrs.createForJavaType(clazz));

    // Add indirection for object to indirection table
    updateIndirectionTable(indirection, object, key);

    if (mustChunk) {
        // Write Value chunk
        end_flag--;
        chunkedValueNestingLevel--;
        start_block();
    } else
        end_flag--;

    if (valueHandler instanceof ValueHandlerMultiFormat) {
        ValueHandlerMultiFormat vh = (ValueHandlerMultiFormat)valueHandler;
        vh.writeValue(parent, object, streamFormatVersion);
    } else
        valueHandler.writeValue(parent, object);

    if (mustChunk)
        end_block();

    // Write end tag
    writeEndTag(mustChunk);
}
 
Example 19
Source Project: sofa-bolt   File: RpcCommandEncoder.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @see com.alipay.remoting.CommandEncoder#encode(io.netty.channel.ChannelHandlerContext, java.io.Serializable, io.netty.buffer.ByteBuf)
 */
@Override
public void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
    try {
        if (msg instanceof RpcCommand) {
            /*
             * ver: version for protocol
             * type: request/response/request oneway
             * cmdcode: code for remoting command
             * ver2:version for remoting command
             * requestId: id of request
             * codec: code for codec
             * (req)timeout: request timeout.
             * (resp)respStatus: response status
             * classLen: length of request or response class name
             * headerLen: length of header
             * cotentLen: length of content
             * className
             * header
             * content
             */
            RpcCommand cmd = (RpcCommand) msg;
            out.writeByte(RpcProtocol.PROTOCOL_CODE);
            out.writeByte(cmd.getType());
            out.writeShort(((RpcCommand) msg).getCmdCode().value());
            out.writeByte(cmd.getVersion());
            out.writeInt(cmd.getId());
            out.writeByte(cmd.getSerializer());
            if (cmd instanceof RequestCommand) {
                //timeout
                out.writeInt(((RequestCommand) cmd).getTimeout());
            }
            if (cmd instanceof ResponseCommand) {
                //response status
                ResponseCommand response = (ResponseCommand) cmd;
                out.writeShort(response.getResponseStatus().getValue());
            }
            out.writeShort(cmd.getClazzLength());
            out.writeShort(cmd.getHeaderLength());
            out.writeInt(cmd.getContentLength());
            if (cmd.getClazzLength() > 0) {
                out.writeBytes(cmd.getClazz());
            }
            if (cmd.getHeaderLength() > 0) {
                out.writeBytes(cmd.getHeader());
            }
            if (cmd.getContentLength() > 0) {
                out.writeBytes(cmd.getContent());
            }
        } else {
            String warnMsg = "msg type [" + msg.getClass() + "] is not subclass of RpcCommand";
            logger.warn(warnMsg);
        }
    } catch (Exception e) {
        logger.error("Exception caught!", e);
        throw e;
    }
}
 
Example 20
private void writeRMIIIOPValueType(Serializable object, Class clazz) {
    if (valueHandler == null)
        valueHandler = ORBUtility.createValueHandler(); //d11638

    Serializable key = object;

    // Allow the ValueHandler to call writeReplace on
    // the Serializable (if the method is present)
    object = valueHandler.writeReplace(key);

    if (object == null) {
        // Write null tag and return
        write_long(0);
        return;
    }

    if (object != key) {
        if (valueCache != null && valueCache.containsKey(object)) {
            writeIndirection(INDIRECTION_TAG, valueCache.getVal(object));
            return;
        }

        clazz = object.getClass();
    }

    if (mustChunk || valueHandler.isCustomMarshaled(clazz)) {
        mustChunk = true;
    }

    // Write value_tag
    int indirection = writeValueTag(mustChunk, true, Util.getCodebase(clazz));

    // Write rep. id
    write_repositoryId(repIdStrs.createForJavaType(clazz));

    // Add indirection for object to indirection table
    updateIndirectionTable(indirection, object, key);

    if (mustChunk) {
        // Write Value chunk
        end_flag--;
        chunkedValueNestingLevel--;
        start_block();
    } else
        end_flag--;

    if (valueHandler instanceof ValueHandlerMultiFormat) {
        ValueHandlerMultiFormat vh = (ValueHandlerMultiFormat)valueHandler;
        vh.writeValue(parent, object, streamFormatVersion);
    } else
        valueHandler.writeValue(parent, object);

    if (mustChunk)
        end_block();

    // Write end tag
    writeEndTag(mustChunk);
}