-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
java 16 record ? #98
Comments
No plans on my side - feel free :) |
ok so.
Use Case : // simple call that should be used most of the time
var record = cm.class("RecordTest").record("Person").withParam(String.class, "name").withParam(String.class, "address");
//more complex as we add a new constructor
var cons2 = record.construct();
var namep = cons2.param(String.class, "name");
cons2.body().add(JExpr.invoke("this").arg(namep).arg(Jexpr.null)); should produce class RecordTest {
public record Person(String name, String address){
public Person(String name) {
this(name, null);
}
}
} |
There also is the base code to be added to the constructor, example public Person {
Object.requireNonNull(name);
} I think this should be a JBlock accessible with _init() in the Jrecord ? |
okay so I think I was wrong, I can create a record as a class so maybe no need to rewrite the whole thing. The goal is therefore to add the type to the existing EClassType, and then… import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public record Person(String name, String address) {
public Person {
Objects.requireNonNull(name);
}
public Person(String name) {
this(name, null);
}
public String familyName() {
return Stream.of(name.split(" ")).skip(1).collect(Collectors.joining(" "));
}
public String firstName() {
return name.split(" ")[0];
}
// singleton
private static Person nobody = new Person("nobody");
public static Person nobody() {
return nobody;
}
} It compiles on my computer so being able to describe it programatically seems a good start ? |
I must admit I haven't look at the official specs yet, and just adding to |
They are final for java 16. preview in java 15 which I used. |
ok so I think these are the required steps :
|
The example test should provide an executable class, either with a main() or by implementing Runnable, so that it can actually be ran with the in-memory compiler and return a value that would be tested with Assert.assertEqual. This test should also provide the same result when called as a java12 and therefore the test class should make both calls. (with record as a feature and with record as final immutable) IMO the enableRecord flag should be present in the JCodemodel, default false untill JCM uses java 16 (in maven) |
https://github.com/glelouet/JCM-test-records/blob/main/src/main/java/com/helger/jcodemodel/target/record/Person.java |
Usage of records : records manipulation in JCM would allow to automatically generate immutable class. In my case, it would be useful as the key type of a cached remote server resource modeling. https://github.com/guiguilechat/JCELechat/blob/master/model/esi/JCESI-compiled/src/generated/java/fr/guiguilechat/jcelechat/model/jcesi/compiler/compiled/connected/Characters.java#L200 Later, because java16 is not here yet :P |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
are they already present ? If not, are they to be implemented ? if yes, are you working on it or can I work on it ?
The text was updated successfully, but these errors were encountered: