2

I just heard of AspectJ and it doesn't look too easy to understand, so I want to know beforehand if it (or anything else) will help me with my problem or not.

I have bunch of simple POJO classes and want to write binary serializers for them but without writing Write/Read methods by hand for each class. I could've done so with help of reflection but that will affect runtime performance. I believe I need something similar to Macroses in Scala with compile-time reflection and quasiquotes.

Update: I'm unable to use any serialization available, because I have custom binary protocol which I can't modify (online game)

Update 2:

Example POJO with it's read, write and some helper methods. Not final version, there possibly could be some annotations, for example, but general structure should be the same. I also omitted inheritance for simplicity, in reality LoginPacket extends CommandPacket class which in turn extends Packet class.

public class LoginPacket {
    public short length;
    public int sessionId;
    public short command;
    public short error;
    public String reason;

    private String getString(ByteBuffer data) {
        short length = data.getShort();
        byte[] stringData = new byte[length];
        data.get(stringData);
        return new String(stringData, "UTF-8");
    }

    private void putString(ByteBuffer data, String someString) {
        data.putShort(someString.length());
        byte[] stringData = someString.getBytes("UTF-8");
        data.put(stringData);
    }

    public static LoginPacket read(ByteBuffer data) {
        LoginPacker loginPacket = new LoginPacket();
        loginPacket.length = data.getShort();
        loginPacket.sessionId = data.getInt();
        loginPacket.command = data.getShort();
        loginPacket.error = data.getShort();
        loginPacket.reason = getString(data);
        return loginPacket;
    }

    public void write(ByteBuffer data) {
        data.putShort(this.length);
        data.putInt(this.sessionId);
        data.putShort(this.command);
        data.putShort(this.error);
        putString(data, this.reason);
    }
}
bobby
  • 617
  • 1
  • 8
  • 19
  • Are you not able to use simple Java Serialization? – Nikolai97 Dec 13 '14 at 18:27
  • Are you interested in an answer that could reliably generate the code using just the source (pre-compile time)? – Ira Baxter Dec 13 '14 at 21:28
  • @IraBaxter, yes, that will be great. – bobby Dec 13 '14 at 21:53
  • 1
    The question is unclear and too general. I will vote to close it unless you update it with some code showing what kind of POJOs you have and what the result should be. How would you write a serializer manually? Only then somebody can judge if it is possible to generate it via AspectJ or by some other means. – kriegaex Dec 13 '14 at 22:03
  • @kriegaex, updated with sample code. – bobby Dec 13 '14 at 22:22
  • 1
    Thanks, now the requirements are clearer. I could offer you an AspectJ solution, but in order to dynamically determine the property types it would also have to use reflection and you already said that this was too slow for your purpose. In this case either use a framework like [Kryo](https://github.com/EsotericSoftware/kryo) (or [Kryonet](https://github.com/EsotericSoftware/kryonet) if you have a distributed scenario and want to serialise network communication) or write your own framework, e.g. by defining some kind of DSL which then can be used by a code generator. – kriegaex Dec 13 '14 at 23:37
  • @kriegaex, Kryo uses ReflectASM which is few times slower than direct field access. – bobby Dec 13 '14 at 23:47
  • 1
    Then create a DSL for data definition and generate your classes includig (de-)serialisers from it. But beware, one Scrum team I coached a while ago did that and it was a heck of a lot of work to get all corner cases working, such as nested data objects, resolving cyclic dependencies etc. Have fun! – kriegaex Dec 13 '14 at 23:56
  • 1
    One more hint: AspectJ has a new [annotation processing feature](http://andrewclement.blogspot.de/2014/08/annotation-processing-in-ajdt.html) which enables you to generate aspect code on the fly from within an annotation processor. This way you could generate one aspect per data class and define your read/write methods via reflection during the annotation processing phase in your build process. During runtime there would be no more reflection and it could be blazingly fast. But I have not thought it through completely, it is just an idea. – kriegaex Dec 13 '14 at 23:59

1 Answers1

1

I don't think you need to use AspectJ to modify your classes. I don't see what benefits using compile team weaving would add. I would suggest having your POJOs use implements Serializableand then serialize your objects using an ObjectOutputStream.

A simple example writing an object to a file:

outputStream = new ObjectOutputStream(new FileOutputStream(filePath)); 
outputStream.writeObject(yourObject);
...
// do whatever else and close stream

Similar questions:

  1. Saving to binary/serialization java
  2. Best way to store data for your game? (Images, maps, and such)
Community
  • 1
  • 1
mkobit
  • 43,979
  • 12
  • 156
  • 150
  • Sorry, I didn't mentioned this from the start. See update please. Java Serialization is slow too. – bobby Dec 13 '14 at 19:50