Pacerier
Pacerier

Reputation: 89743

Is there a tool to do the manual work of copying + pasting + replacing source code?

Good afternoon all, I am writing the code for a custom array list of primitive ints (to avoid the overhead of boxing), an array list of primitive bytes, an array list of primitive longs, an array list of primitive chars, and an array list of primitive doubles.

The code for these 5 classes are very similar. Basically the logic is (almost) identical. However because generics couldn't be used, there is no way to define common generic functions usable for all 5 classes (correct me if I'm wrong).

As a result, I have to duplicate the same code 5 times doing a string search replace each time. The problem comes when I have to edit the classes, because I have to do the same editing x5 times.

Now I was wondering what's the best way to maintain repeating boilerplate code like these?

Is there a tool that can do the manual work of copying + pasting + replacing?

(for example I could define a transforming function F() and tell the tool: for the source code of class mypackage.IntArrayList, apply the transformation F() onto the source code of classes mypackage.ByteArrayList, mypackage.LongArrayList, mypackage.CharArrayList, and mypackage.DoubleArrayList) ?

Upvotes: 1

Views: 90

Answers (3)

Chad Schultz
Chad Schultz

Reputation: 7860

Judging from the tag, it sounds like you're using Eclipse. You can do a Global search and replace, although it isn't obvious. Go to the Search menu, do a search as normal, and then when you see the search results pane, right-click there and click "replace all"

Upvotes: 1

TacticalCoder
TacticalCoder

Reputation: 6325

to avoid the overhead of casting

I think you meant (un)boxing, not casting ; )

Now I was wondering what's the best way to maintain repeating boilerplate code like these?

Now, I know this answer is going to be frowned upon by some but...

The way the Trove API does it is simple: custom and automatic code-generation tool.

Trove is an API ofter collections based on primitives that runs around the default Java collections when used with primitives. Comparing a Trove TIntIntHashMap with a (HashMap<Integer,Integer>* is not even fun. It's shameful for the default Java API...

And they've encountered exactly the problem you're having. And they solved it using code generation tool.

On a side-note note that Trove already has a TIntArrayList backed by primitives which may, or may not, already do what you're implementing now (if I understood your question correctly).

Upvotes: 3

Miserable Variable
Miserable Variable

Reputation: 28761

As defined, the task seems rather complex. I doubt you will find any existing tool to do something like this.

As an alternative, consider code-generation: define a template using a single variable to represent the primitive type and generate code for all types from that. For lifecycle, make changes to the template rather then generated code.

Upvotes: 2

Related Questions