Skip to main content



      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 14:38 Go to next message
Eclipse UserFriend
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 06:45 Go to previous messageGo to next message
Eclipse UserFriend
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 11:08 Go to previous message
Eclipse UserFriend
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: Sun Jul 06 11:03:58 EDT 2025

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

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

Back to the top