mbien commented on code in PR #7286:
URL: https://github.com/apache/netbeans/pull/7286#discussion_r1574066559
##########
ide/languages.hcl/src/org/netbeans/modules/languages/hcl/ast/HCLResolveOperation.java:
##########
@@ -28,22 +28,30 @@ public sealed interface HCLResolveOperation extends
HCLExpression {
HCLExpression base();
+
default List<? extends HCLExpression> elements() {
return List.of(base());
}
public record Attribute(HCLExpression base, HCLIdentifier attr) implements
HCLResolveOperation {
+
+ public Attribute {
+ if (base == null) {
+ throw new NullPointerException("base cannot be null");
+ }
+ }
+
@Override
public String asString() {
- return base.asString() + "." + attr;
+ return HCLExpression.asString(base) + "." +
HCLExpression.asString(attr);
Review Comment:
can use `base.asString()` since base can't be null due to constructor
validation. (3x more occurrences in this class)
##########
ide/languages.hcl/src/org/netbeans/modules/languages/hcl/ast/HCLResolveOperation.java:
##########
@@ -28,22 +28,30 @@ public sealed interface HCLResolveOperation extends
HCLExpression {
HCLExpression base();
+
default List<? extends HCLExpression> elements() {
return List.of(base());
}
public record Attribute(HCLExpression base, HCLIdentifier attr) implements
HCLResolveOperation {
+
+ public Attribute {
+ if (base == null) {
+ throw new NullPointerException("base cannot be null");
+ }
Review Comment:
-> `Objects.requireNonNull(base, "base cannot be null");`
##########
ide/languages.hcl/src/org/netbeans/modules/languages/hcl/ast/HCLExpression.java:
##########
@@ -36,17 +36,18 @@ public sealed interface HCLExpression extends HCLElement
permits
HCLConditionalOperation,
HCLForExpression,
HCLFunction,
+ HCLIdentifier,
HCLLiteral,
HCLResolveOperation,
HCLTemplate,
HCLVariable {
- public static HCLExpression parse(String expr) {
- HCLLexer lexer = new HCLLexer(CharStreams.fromString(expr));
- HCLParser parser = new HCLParser(new CommonTokenStream(lexer));
- return new HCLExpressionFactory().process(parser.expression());
+
+ public static String asString(HCLExpression expr) {
+ return expr != null ? expr.asString() : "";
}
Review Comment:
is it too late to change `asString()` to `toString()`?
This method would be then inlined as `Objects.toString(expr, "<null>")`
where `expr` can be null.
Or is the separation of `toString` and `asString` by design? There is a risk
that someone could call `toString` where `asString` is meant and bugs like this
could be hard to spot.
note: one disadvantage of using `toString` here would be that an interface
hierarchy can't provide default impls for it since interfaces can't override
methods.
##########
ide/languages.hcl/src/org/netbeans/modules/languages/hcl/ast/HCLFunction.java:
##########
@@ -34,8 +34,8 @@ public record HCLFunction(HCLIdentifier name,
List<HCLExpression> args, boolean
@Override
public String asString() {
StringJoiner sargs = new StringJoiner(",", "(", expand ? "...)" : ")");
- args.forEach((arg) -> sargs.add(arg.toString()));
- return name + sargs.toString();
+ args.forEach((arg) -> sargs.add(HCLExpression.asString(arg)));
+ return HCLExpression.asString(name) + sargs.toString();
}
Review Comment:
can the name of a function be null? Cases like this could be validated in
the constructor. Or is it intended for the data structure to hold partially
valid trees?
btw `copyOf` is just like `of` and will throw NPEs if something is null -
this may or may not be intended.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists