AspectJ/使用编译时反射生成方法 [英] AspectJ / Generate methods using compile-time reflection

查看:124
本文介绍了AspectJ/使用编译时反射生成方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是听说过AspectJ,而且看起来不太容易理解,所以我想事先知道它(或其他任何东西)是否可以帮助我解决问题.

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.

我有一堆简单的POJO类,并且想为它们编写二进制序列化程序,但不想为每个类手动编写Write/Read方法.我可以借助反射来完成此操作,但这会影响运行时性能.我相信我需要与Scala中的Macroses类似的东西,并带有编译时反射和准引用.

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)

更新2:

示例POJO及其readwrite和一些辅助方法.不是最终版本,例如可能会有一些注释,但一般结构应相同.为了简化起见,我也省略了继承,实际上LoginPacket扩展了CommandPacket类,而后者又扩展了Packet类.

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);
    }
}

推荐答案

我认为您不需要使用AspectJ来修改您的类.我看不出使用编译团队编织会带来什么好处.我建议您的POJO使用implements Serializable,然后使用ObjectOutputStream序列化对象.

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

类似的问题:

  1. 保存为二进制/串行化Java
  2. 最佳存储游戏数据的方法? (图像,地图等)
  1. Saving to binary/serialization java
  2. Best way to store data for your game? (Images, maps, and such)

这篇关于AspectJ/使用编译时反射生成方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆