use of java.io.ObjectStreamField in project robovm by robovm.
the class OldObjectStreamFieldTest method test_ConstructorLjava_lang_StringLjava_lang_ClassB.
public void test_ConstructorLjava_lang_StringLjava_lang_ClassB() {
ObjectStreamField osf = new ObjectStreamField("aField", int.class, false);
assertTrue("Test 1: Name member not set correctly.", osf.getName().equals("aField"));
assertTrue("Test 2: Type member not set correctly.", osf.getType().equals(int.class));
assertFalse("Test 3: Unshared member not set correctly.", osf.isUnshared());
// Repeat the tests with a different object to make sure
// that we have not tested against default values.
osf = new ObjectStreamField("anotherField", String.class, true);
assertTrue("Test 4: Name member not set correctly.", osf.getName().equals("anotherField"));
assertTrue("Test 5: Type member not set correctly.", osf.getType().equals(String.class));
assertTrue("Test 6: Unshared member not set correctly.", osf.isUnshared());
// Invalid argument tests.
try {
osf = new ObjectStreamField(null, boolean.class);
fail("Test 7: NullPointerException expected.");
} catch (NullPointerException e) {
// Expected.
}
try {
osf = new ObjectStreamField("thisField", null);
fail("Test 8: NullPointerException expected.");
} catch (NullPointerException e) {
// Expected.
}
}
use of java.io.ObjectStreamField in project Bytecoder by mirkosertic.
the class ObjectStreamClass method readNonProxy.
/**
* Reads non-proxy class descriptor information from given input stream.
* The resulting class descriptor is not fully functional; it can only be
* used as input to the ObjectInputStream.resolveClass() and
* ObjectStreamClass.initNonProxy() methods.
*/
void readNonProxy(ObjectInputStream in) throws IOException, ClassNotFoundException {
name = in.readUTF();
suid = Long.valueOf(in.readLong());
isProxy = false;
byte flags = in.readByte();
hasWriteObjectData = ((flags & ObjectStreamConstants.SC_WRITE_METHOD) != 0);
hasBlockExternalData = ((flags & ObjectStreamConstants.SC_BLOCK_DATA) != 0);
externalizable = ((flags & ObjectStreamConstants.SC_EXTERNALIZABLE) != 0);
boolean sflag = ((flags & ObjectStreamConstants.SC_SERIALIZABLE) != 0);
if (externalizable && sflag) {
throw new InvalidClassException(name, "serializable and externalizable flags conflict");
}
serializable = externalizable || sflag;
isEnum = ((flags & ObjectStreamConstants.SC_ENUM) != 0);
if (isEnum && suid.longValue() != 0L) {
throw new InvalidClassException(name, "enum descriptor has non-zero serialVersionUID: " + suid);
}
int numFields = in.readShort();
if (isEnum && numFields != 0) {
throw new InvalidClassException(name, "enum descriptor has non-zero field count: " + numFields);
}
fields = (numFields > 0) ? new ObjectStreamField[numFields] : NO_FIELDS;
for (int i = 0; i < numFields; i++) {
char tcode = (char) in.readByte();
String fname = in.readUTF();
String signature = ((tcode == 'L') || (tcode == '[')) ? in.readTypeString() : new String(new char[] { tcode });
try {
fields[i] = new ObjectStreamField(fname, signature, false);
} catch (RuntimeException e) {
throw (IOException) new InvalidClassException(name, "invalid descriptor for field " + fname).initCause(e);
}
}
computeFieldOffsets();
}
use of java.io.ObjectStreamField in project Bytecoder by mirkosertic.
the class ObjectStreamClass method computeFieldOffsets.
/**
* Calculates and sets serializable field offsets, as well as primitive
* data size and object field count totals. Throws InvalidClassException
* if fields are illegally ordered.
*/
private void computeFieldOffsets() throws InvalidClassException {
primDataSize = 0;
numObjFields = 0;
int firstObjIndex = -1;
for (int i = 0; i < fields.length; i++) {
ObjectStreamField f = fields[i];
switch(f.getTypeCode()) {
case 'Z':
case 'B':
f.setOffset(primDataSize++);
break;
case 'C':
case 'S':
f.setOffset(primDataSize);
primDataSize += 2;
break;
case 'I':
case 'F':
f.setOffset(primDataSize);
primDataSize += 4;
break;
case 'J':
case 'D':
f.setOffset(primDataSize);
primDataSize += 8;
break;
case '[':
case 'L':
f.setOffset(numObjFields++);
if (firstObjIndex == -1) {
firstObjIndex = i;
}
break;
default:
throw new InternalError();
}
}
if (firstObjIndex != -1 && firstObjIndex + numObjFields != fields.length) {
throw new InvalidClassException(name, "illegal field order");
}
}
use of java.io.ObjectStreamField in project Bytecoder by mirkosertic.
the class ObjectStreamClass method getDefaultSerialFields.
/**
* Returns array of ObjectStreamFields corresponding to all non-static
* non-transient fields declared by given class. Each ObjectStreamField
* contains a Field object for the field it represents. If no default
* serializable fields exist, NO_FIELDS is returned.
*/
private static ObjectStreamField[] getDefaultSerialFields(Class<?> cl) {
Field[] clFields = cl.getDeclaredFields();
ArrayList<ObjectStreamField> list = new ArrayList<>();
int mask = Modifier.STATIC | Modifier.TRANSIENT;
for (int i = 0; i < clFields.length; i++) {
if ((clFields[i].getModifiers() & mask) == 0) {
list.add(new ObjectStreamField(clFields[i], false, true));
}
}
int size = list.size();
return (size == 0) ? NO_FIELDS : list.toArray(new ObjectStreamField[size]);
}
use of java.io.ObjectStreamField in project Bytecoder by mirkosertic.
the class ObjectStreamClass method writeNonProxy.
/**
* Writes non-proxy class descriptor information to given output stream.
*/
void writeNonProxy(ObjectOutputStream out) throws IOException {
out.writeUTF(name);
out.writeLong(getSerialVersionUID());
byte flags = 0;
if (externalizable) {
flags |= ObjectStreamConstants.SC_EXTERNALIZABLE;
int protocol = out.getProtocolVersion();
if (protocol != ObjectStreamConstants.PROTOCOL_VERSION_1) {
flags |= ObjectStreamConstants.SC_BLOCK_DATA;
}
} else if (serializable) {
flags |= ObjectStreamConstants.SC_SERIALIZABLE;
}
if (hasWriteObjectData) {
flags |= ObjectStreamConstants.SC_WRITE_METHOD;
}
if (isEnum) {
flags |= ObjectStreamConstants.SC_ENUM;
}
out.writeByte(flags);
out.writeShort(fields.length);
for (int i = 0; i < fields.length; i++) {
ObjectStreamField f = fields[i];
out.writeByte(f.getTypeCode());
out.writeUTF(f.getName());
if (!f.isPrimitive()) {
out.writeTypeString(f.getTypeString());
}
}
}
Aggregations