Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Custom Code Generation With Global Variables(Automatically create methods with global variables)
Custom Code Generation With Global Variables [message #1849740] Mon, 31 January 2022 19:38 Go to next message
Doug Lockard is currently offline Doug LockardFriend
Messages: 2
Registered: January 2022
Junior Member
Hi Everyone,

I am wondering if there is a way to generate code similarly to how the getters and setters can automatically be generated off global variables.

In my system, I have a set of read and write functions that are needed frequently for a variety of variables and it can be quite time-consuming to implement each line. For example:

//Global Variables

protected String strA;
protected double doubA;
protected int intA;

...

//Methods
protected void write(BinaryWriter out)
{
out.writeStr(strA);
out.writeDoub(doubA);
out.writeInt(intA);
}


Is there any way that I can implement this write method automatically using the global variables? I have looked into code templates a bit but I can't seem to find what I am looking for exactly. Is this even a possibility? Or can someone point me in another direction?

Thanks for any and all feedback.
Re: Custom Code Generation With Global Variables [message #1849747 is a reply to message #1849740] Tue, 01 February 2022 11:45 Go to previous messageGo to next message
Jay Arthanareeswaran is currently offline Jay ArthanareeswaranFriend
Messages: 128
Registered: July 2009
Senior Member
I don't find anything in the templates that will let us do this.

I can think of two options.
1) What the Eclipse Java Tooling itself does - That is, to parse the file and navigate the AST and create program elements or in this case, the method statements off the global variable.
2) Annotate the fields with custom annotations and write an annotation processor that will generate the method body. May be there are annotation based tools readily available, but I am not sure.

Both have pros and cons but I can imagine neither can be as simple as using a code template.
Re: Custom Code Generation With Global Variables [message #1849764 is a reply to message #1849747] Tue, 01 February 2022 16:08 Go to previous message
Doug Lockard is currently offline Doug LockardFriend
Messages: 2
Registered: January 2022
Junior Member
Thank you! I will take a look at these options.
Previous Topic:Bad path to andriod.jar in eclipse.
Next Topic:How to keep simple getters/setters on the same line?
Goto Forum:
  


Current Time: Sat Apr 27 00:00:52 GMT 2024

Powered by FUDForum. Page generated in 0.02993 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top