Conversation
This commit adds: - EClassType.RECORD for record type declarations - JRecordComponent class to represent record components - _record() methods in IJClassContainer to create records - recordComponent() and recordComponentVararg() methods in JDefinedClass - compactConstructor() for validation-only constructors - isRecord() helper method - JDocComment.addParam(JRecordComponent) for javadoc support Tests cover: basic records, empty records, object/array/ varargs components, generics, annotations, compact and canonical constructors, methods, static members, nested records, and javadoc.
- Change from ValueEnforcer to explicit IllegalStateException throws - Fix missing semicolons in throw statements - Fix error message in recordComponentVararg() - Add tests for calling record methods on non-record classes - Add test for duplicate compact constructor
Add support for Java records (JEP 395, Java 16+)
|
The record components should be used as variables too.
JRecordComponent lo = rec.recordComponent (cm.INT, "lo");
JRecordComponent hi = rec.recordComponent (cm.INT, "hi");
…
compactCtor.body ()
._if (lo.gt (hi))Same on the next test methods Make JRecordComponent implement IJExpression ? Only default methods AFAIK, parent IJGenerable::generate already implemented since JRecordComponent implements IJGenerable. |
|
Yes, I've seen this too and tried making |
|
can't be assignment target, it's const. |
|
Inside the constructor it is assigned to |
|
Yeah, also in future "with" it will be. |
|
@glelouet better? |
|
What do you mean by inside the constructor through ? Ho I get it. Constructors of the record can set their own value, eg |
|
Syntactic sugar with fixed coding styleguide for getters and setters ;-) |
|
@phax The modified parts are better indeed, but the rest of the test is still to be changed ? (Not complaining, just stating) |
|
Feel free to complain ;-) Noted |
Yeah I like that refthis can take a record into account. Something required when doing, say, record constructors :P |
No point in complaining if that's not brought alongside an improvement effort. |
|
As a side note can we also have a throw() expression that takes a I just realize the ._throw (JExpr._new (cm.ref (IllegalArgumentException.class)));Could definitely be improved into ._throw (IllegalArgumentException.class);If JBlock had a sugar coating corresponding method. |
|
Will do it later - have other priorities atm |
|
There is no codemodel reference in the JBlock so only using AbstractJClass ; added second method in JBlock : @NonNull
public JThrow _throw (@NonNull final IJExpression aExpr)
{
return internalInsert (new JThrow (aExpr));
}
@NonNull
public JThrow _throw (@NonNull final AbstractJClass throwClass)
{
return _throw (JExpr._new( throwClass));
}Only saves a _new call, not sure if worth. |
|
@phax can you show me where it is allowed to assign values to the record fields ? I could not find a way in https://docs.oracle.com/en/java/javase/25/language/records.html#GUID-6699E26F-4A9B-4393-A08B-1E47D4B2D263 On the opposite, I found
Which implies that there is no assignment ? edit : okay it's in the cannonical constructor. this.length = length;
this.width = width; |
|
Took your proposal in - seems like a good compromise |
|
maybe could even add a varagar of params to pass to the _new call. Just an idea though. => (only second method changed) @NonNull
public JThrow _throw (@NonNull final IJExpression aExpr)
{
return internalInsert (new JThrow (aExpr));
}
@NonNull
public JThrow _throw (@NonNull final AbstractJClass throwClass, IJExpression... params)
{
JInvocation init = JExpr._new( throwClass);
if(params!=null )
{
for(IJExpression ije : params)
{
init.arg(ije);
}
}
return _throw (init);
} |
|
Good idea - done. But using a separate method |
|
@phax the null test was because the varargs was allowed null. |
|
you can't have enough null checks ;-) |
Based on #126