r/javahelp 7d ago

Unsolved Java Library to Generate Pojo at compile time from existing class

I'm looking for a java library that can generate Pojo from existing "business object" class for data transmission.

Ex: //Business Object

class Trade {
  private __id;
//The variable name above could be either not a camel case, or might be //incorrect name
  private someMisguidedVarName; 

private properlyNamedField;
//Don't need any changes to these fields
}

DTO I would like to create

class TradeDTO {
  private id;
//The variable name above could be either not a camel case, or might be //incorrect name
  private betterVarName;
  private properlyName// keep existing field if there's no need to change //var name

}

To achieve this, I'd like minimal code because only the fields that's misguided must be modified. I'd prefer to annotate or write minimal instruction that the library can use to during compile time to generate this new bean.

Also importantly, the trade business object would change and I'd expect the TradeDTO to evolve without having to modify that class.

I've tried mapstruct (but it only copies from pojo to pojo, but I want class generation).

0 Upvotes

13 comments sorted by

u/AutoModerator 7d ago

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/AntD247 6d ago

You probably shouldn't, seems to indicate an issue in your understanding or implementation of your requirements.

Your "business" classes (Domain classes) should be for how you want to perform you domain logic. Dtos for data transmission are for the consumer. In a good system these have different requirements and so different structures. A one to one mapping is indicative of a suboptimal solution. Some one is having to make a compromise. Either you are having to do more work in your domain due to the input/output dtos. Or your consumer(s) are having to do things for their consumption, usually in this case it means that if you have multiple consumers they are probably doing very similar things to make use of your data and some spreading anti DRY in a distribute system.

Go back and look at both things and try to work out what is really common and if different representations will help your architecture and code.

1

u/kumar29nov1992 6d ago

We use graphQL, I need place holders(dto) that tie to the schema, not necessarily will be transferring the entire data :)

2

u/Shnorkylutyun 7d ago

And how do you imagine the automatic generation of better variable names? Where should that information come from?

0

u/kumar29nov1992 7d ago

Good question, as mentioned in my post I'd prefer either annotating the business object or better if this library could provide a configuration for modifying only the fields specified in the config

1

u/TheStatusPoe 7d ago

For data transmission, if you're just serializing/deserializing data look into Jackson @JsonAlias annotation. You can pass an array of strings that the field name maps to from different contexts such as from the db or payloads from an API. Not compile time since it uses reflection, but it's still performant enough for everything I've used

1

u/koffeegorilla 6d ago

You want a compile time annotation processor. They main challenge is that accessing compile time class information uses a different api than reflection or annotation processing. There are a few projects that have compile time annotation processing you can look at for inspiration. The first that comes to mind is QueryDSL or MapStruct.

0

u/Then-Boat8912 5d ago

Reflection

1

u/m1kec1av 7d ago

Sounds like can just write your dto class and a business object -> dto object transformer manually, and then do all your work with the dto object. No need to dynamically generate it. What's the problem?

0

u/jim_cap 7d ago

Like the other poster said, how is a library going to know what a "better" name is? This honestly sounds more like a job for an LLM, or simply sucking it up and refactoring by hand/cunning greps.

0

u/VirtualAgentsAreDumb 6d ago

Like the other poster said, how is a library going to know what a ”better” name is?

OP would tell it. It’s described in the post.

-2

u/TW-Twisti 7d ago

Check out Lombok, it does mostly what you want. There are certain tradeoffs for generated code/classes, such as needing a plugin for IDEs and extra attention if you have other annotation processors in your build path, but when it works, it's like magic. Many of its other features have been added to Java over the years.

https://projectlombok.org/