0

So, given that Java has little to no support to unsigned types, I'm right now writing a small API to handle these (for now, I have UnsignedByte and UnsignedInt). The algorithm is simple: store each of them as their higher representation (byte->short, int->long), extends the Number class and implement some calculation and representation utility methods.

The problem is: it is actually very verbose - and boring - to have to, every time, code things like:

UnsignedByte value = new UnsignedByte(15);
UnsignedByte convert = new UnsignedByte(someIntValue);

I was wondering: is there any way to implement, on Eclipse, something like a "file pre-processor", in a way that it will automatically replace some pre-defined strings with other pre-defined strings before compiling the files? For example: replace U(x) with new UnsignedByte(x), so it would be possible to use:

UnsignedByte value = U(15);
UnsignedByte convert = U(someIntValue);

Yes, I could create a method called U(...) and use import static, but even then, it would be so much trouble doing it for every class that I would use my unsigned types.

I could write a simple Java program that would replace these expressions in a file, but the problem is: How could I integrate that on Eclipse, in a way that it would call/use it every time a Java file is compiled?

  • 1
    You might consider using Guava, which already has those libraries - and you don't need to represent an unsigned byte as a short, you can represent it with a signed byte if you do it properly. – Louis Wasserman Nov 27 '15 at 14:59
  • While I have provided an answer, I don't think you are going to like it. Anyway you are in Java now, java is verbose and the tools are there to help. That said, it is a good idea to use a factories, so other than the overly shortened name "U" there may be good argument to using it like a factory. Anyway, Ctrl-Space is your friend! – Jonah Graham Nov 27 '15 at 15:12

2 Answers2

0

I would recommend using Eclipse Templates for doing this instead. I know its not exactly what you ask for but its very simple and can be achieved out of the box.

When you write sysout in Eclipse and press Ctrl+Space it gives you an option to replace that with System.out.println();

You can find more information in the following link

How to add shortcut keys for java code in eclipse

Community
  • 1
  • 1
Akshay Gehi
  • 362
  • 1
  • 12
0

I can point you at how one project I know of does this, they have a set of Python scripts that generate a whole set of classes (java files) from a template base file. They run the script manually, as opposed to part of the build.

Have a look here for the specific example. In this code they have a class for operating on double, but from this class they want to generate code to operate on float, int, etc all in the same way.

There is, of course, a big debate about whether generated code should be checked in or not to source repository. I leave that issue aside and hope that the above example is good to get you going.

Jonah Graham
  • 7,890
  • 23
  • 55