aspose file tools*
The moose likes Java in General and the fly likes Is there a good way to do this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is there a good way to do this?" Watch "Is there a good way to do this?" New topic
Author

Is there a good way to do this?

Kurro Zaki
Greenhorn

Joined: Dec 08, 2009
Posts: 14
This is rather complicated and a bit hard to explain I hope you understand. If not you can ask me
for clarification. (ooh and thank you for trying to read my question because it's a bit long :s)

I'm trying to find a good way to do something in java but I'm not sure if there is a proper way of doing.
There is a big change it isn't That would make me a bit sad but I hope some experienced people here
can figure it out please :p

So what I'm doing is creating a network protocol. It would word by transmitting messages
first a message ID is send (1 byte) and then all the fields are send (just by using a DataOuputStream)
So for example a LoginRequestMessage would have ID 0 and 2 fields a String for the username
and a int for the protocol version.
Now there would be quite a lot of these messages so I would create an abstract class Message
give it an abstract read() method and an abstract write() method (maybe some other once). Then every Message would
extend that base class.
So I want a better method. I want be be able to write at text file like this

0:LoginRequestMessage
{
String:Username
int:ProtocolVersion
}

1:HeartbeatMessage{
...
...
...

Well the point is that a text file defines all the message like this
message id:message name{
fields
}
where fields is a list of "java type:field name"

Then I want to be able to do the following in my code:
Create 1 class called Message be able to create one of these messages by either giving it the id of the message
or the message name.
then just be able to call message.getUsername() or if not possible message.get("Username") (for the protocol version this would become message.getProtocolVersion() or message.get("ProtocolVersion"))
So I basically want to be able to have a getter and setter method
for every field in the message and get the correct java type returned from that, not just Object. And with java type I mean every possible java primitive or Object so also objects from classes I define elsewhere in my code.

Oooh yeah performance is also a bit of an issue because I don't want to sarifice to much of it by not writing every message class.

aaaaaaaahhhhhhh. If java.lang.invoke.MethodHandle was generic most of my problem would be solved. But it just returns Object aaaaaaaaaaaaaaaargh :p
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Is there a good way to do this?