tipsy / j2html

Java to HTML generator. Enjoy typesafe HTML generation.

Home Page:https://j2html.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Refactor Tag classes to avoid code duplication

tipsy opened this issue · comments

commented

Currently ContainerTag and EmptyTag share the same 40 convenience methods, but they're manually included in each class. Attempts at refactoring this have been unsuccessful, as I can't seem to find a way to keep the type-information intact (this is important for the fluent-api chaining).

For example, if I move them to Tag (the abstract class they both extend), and do

div().withClass("class").withText(), I run into problems, because withText() should be available for ContainerTag objects, but not EmptyTag objects.

I'm sure there's a way though... any thoughts?

Hi!
I think I could refactor this situation using generics.
This weekend I'll work on this and I'll send you a PR to discuss.

commented

Cool, looking forward to it!

Take a look on this commit and please tell me what do you think about it.

commented

Ended up with this. Thanks for your help @snaketl, please let me know if there are any problems with the commit.

I'm glad to help you!