Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Update to support generate record from java 14 #981

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

zzBBc
Copy link

@zzBBc zzBBc commented Aug 30, 2023

Motivation:

Modifications:

  • From commit f10b510 still support and work completely in java 8, don't need to update java version if only purpose is generate record class
  • I also update some lib and fix typo in unit test to support java 17, you can separate and create another version to a long term support in commit dc02cf7

Can you please review and merge the PR @JakeWharton

@Duzhinsky
Copy link

I think it is the most desirable feature for javapoet, but unfortunately, the tool maintenance consists only of small fixes. PR #840 does the same, but still not merged. The future of javapoet is discussed here. You could use the FabricMC/javapoet fork If you want to use records

pom.xml Outdated Show resolved Hide resolved
pom.xml Outdated Show resolved Hide resolved
@lenis0012
Copy link

lenis0012 commented Sep 10, 2023

+1 on this.

In the mean time for those who dont want to use a fork, there is another workaround for inner records (not top-level).
I am simply doing a string replace after saving the fie:

return MethodSpec.methodBuilder("Record__%sEvent".formatted(toTypeName(name)))
    .addModifiers(Modifier.PUBLIC)
    // ....

This creates a public void Record__MyEvent(String a, int b, boolean c) {}
Which I then replace:

try(
    OutputStreamWriter outputStream = new OutputStreamWriter(sourceFile.openOutputStream());
    StringWriter writer = new StringWriter()
) {
    JavaFile.builder(element.getQualifiedName().toString(), result)
        .indent("    ")
        .build()
        .writeTo(writer);
    outputStream.write(writer.toString().replace("void Record__", "record "));
}

Not the most elegant solution but it works.
I am using inner records but you can do the same for top-level records using classes. Although it would be a bit more painful.

@Jonpez2
Copy link

Jonpez2 commented Nov 14, 2023

Is this likely to land at any point? Records are extremely nice, just like javapoet!

Randgalt added a commit to Randgalt/java-composer that referenced this pull request Mar 24, 2024
Randgalt added a commit to Randgalt/java-composer that referenced this pull request Mar 24, 2024
Randgalt added a commit to Randgalt/java-composer that referenced this pull request Mar 24, 2024
@Randgalt
Copy link
Contributor

I'm planning on publishing this as a separate repo. See here: https://github.com/Randgalt/java-composer. Unless @JakeWharton/Square objects.

Randgalt added a commit to Randgalt/java-composer that referenced this pull request Mar 24, 2024
Randgalt added a commit to Randgalt/java-composer that referenced this pull request Mar 24, 2024
Randgalt added a commit to Randgalt/java-composer that referenced this pull request Mar 26, 2024
@Randgalt
Copy link
Contributor

FYI - I've released v 1.0 of Java Composer with this change in it as well as support for sealed classes: https://github.com/Randgalt/java-composer

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants