[SOLVED] CollectionSerializer false exception bug?

I’m having an issue with the CollectionSerializer, where it’s throwing the “[Serializer][???] Could not determine collection type. Using ArrayList” warning when I attempt to send a message from the server to the client.



Psuedo of the message:

[java]@serializable

class GameState {

ArrayList<PlayerState> p;

GameState() {

p = new ArrayList<PlayerState>();

}

}



@serializable

class PlayerState {

// random variables

}[/java]



The client sends a message of PlayerState to the server. The server gathers all the PlayerStates, stores them in the GameState.p and attempts to send them to each client. The problem is when the server sends it throws the warning in the code block below.



CollectionSerializer.java:

[java]public class CollectionSerializer extends Serializer {



public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {

int length = data.getInt();



Collection collection = null;

try {

collection = (Collection)c.newInstance();

} catch (Exception e) {

log.log(Level.WARNING, “[Serializer][???] Could not determine collection type. Using ArrayList.”);

collection = new ArrayList(length);

}[/java]



The message is received from the client fine, but reading the logs from the console becomes a little difficult with this message getting spammed.


  1. Is this a bug?
  2. Is there a way to easily suppress this warning?

Same situation for me. If you have found the solution, please post it here. Thanks!

c.

Ok, I found that:


ArrayList is a generic container class whose state is stored as an array of objects. While arrays are first-class objects in Java, they aren't serializable objects. This means that ArrayList can't just implement the Serializable interface. It has to provide extra information to help the serialization mechanism handle its nonserializable fields. There are three basic solutions to this problem:

Fields can be declared to be transient.
The writeObject( )/readObject( ) methods can be implemented.
serialPersistentFields can be declared.


You can find more on Java RMI By William Grosso

I hope it will help you
c.

Sounds like ArrayList can’t be used with SpiderMonkey. Are there any good alternatives to ArrayLists that are serializable? Or is there anyway to suppress this message?

Collections can be Vectors, ArryList and HashSet, but I don’t know if any of them could be used without serialization issues. I think we should wait for an answer from Lars…



PS: I’ve seen that also the jme3test.network TestSerialization.class itself throws these exceptions:

com.jme3.network.serializing.serializers.CollectionSerializer readObject
[Serializer][???] Could not determine collection type. Using ArrayList.

com.jme3.network.serializing.serializers.MapSerializer readObject
[Serializer][???] Could not determine map type. Using HashMap.


So I think all this is due from these lines of the CollectionSerializer class...

[java] public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
int length = data.getInt();

Collection collection;
try {
collection = (Collection)c.newInstance();
} catch (Exception e) {
log.log(Level.WARNING, "[Serializer][???] Could not determine collection type. Using ArrayList.");
collection = new ArrayList(length);
}
...
}[/java]

so, if the serialization works fine [in effect a new arrayList is really created] but only throws this advert, for now maybe you could only suppress that line. But I'm pretty sure Lars will fix it! I hope so!

Hi! It generally isn’t really a problem, it just tells you that it created an ArrayList, even though you may have intended to use a LinkedList. Even though this usually doesn’t pose any problems, it still tells you that happened. In other words, it should still work fine. I’ll change the logging level to FINE on that one.