You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Using @StandardException and @AllArgConstructor on a class with TWO extra fields creates ambiguous constructor
To Reproduce
Try compiling:
@StandardException
@AllArgsConstructor
public class Failure extends Exception {
Date today;
Integer row;
}
fails with the error message:
java: reference to Failure is ambiguous
both constructor Failure(java.lang.String,java.lang.Throwable) in com.example.demo.Failure and constructor Failure(java.util.Date,java.lang.Integer) in com.example.demo.Failure match
Order of explicit fields does not matter.
Defining only one or more than two fields compiles without issue:
@StandardException
@AllArgsConstructor
public class Failure extends Exception {
Date today;
}
and
@StandardException
@AllArgsConstructor
public class Failure extends Exception {
Date today;
Integer row;
byte[] data;
}
work as expected, allowing to write code such as
@StandardException
@AllArgsConstructor
@ToString
public class Failure extends Exception {
@With
Date today;
@With
Integer row;
@With
byte[] data;
}
[...]
throw new Failure("ouch", cause)
.withData(new byte[] {1,2,3})
.withToday(LocalDateTime.now())
.withRow(44);
NOTE: ToString generated code does not include the @StandardException fields message and cause.
Changing one or many fields to a primitive type (e.g. Integer -> int) also fixes the issue:
public class Failure extends RuntimeException {
@With
LocalDateTime today;
@With
int row;
}
Expected behavior
It is expected that a class annotated with both @StandardException and @AllArgsConstructor defining TWO non-primitive fields should successfully compile, similarly to a class defining one, three or more non-primitive parameters.
The following code:
@StandardException
@AllArgsConstructor
@Getter
@ToString
public class Failure extends RuntimeException {
@With
LocalDateTime today;
@With
Integer row;
}
should be converted to the equivalent (compiling) code:
@AllArgsConstructor
@Getter
@ToString
public class Failure extends RuntimeException {
@With
LocalDateTime today;
@With
Integer row;
final String message;
final Throwable cause;
public Failure() {
this(null, null);
}
public Failure(String message) {
this(message, null);
}
public Failure(Throwable cause) {
this(null, cause);
}
public Failure(String message, Throwable cause) {
this.message = message;
this.cause = cause;
}
}
Looking at it this way, my hypothesis is that both @ToString and @AllArgs ignore the added fields from @StandardException. I haven't looked at Lombok code in a long time but it seems that @StandardException should have priority into extending the class so that other annotations can pickup its additions and process them like regular code.
Version info (please complete the following information):
Lombok version: 1.18.30
Platform: javac 21.0.2
The text was updated successfully, but these errors were encountered:
Great bug report, can reproduce your problem. The compilation error occurs because the constructor generated by @StandardException invokes this(null, null) which is ambiguous.
Simply changing the handler order doesn't work because @StandardException doesn't add the message and cause fields, it uses the parent fields.
Yes, the @Builder support is related, both handlers use the same code to collect fields.
Describe the bug
Using @StandardException and @AllArgConstructor on a class with TWO extra fields creates ambiguous constructor
To Reproduce
Try compiling:
fails with the error message:
Order of explicit fields does not matter.
Defining only one or more than two fields compiles without issue:
and
work as expected, allowing to write code such as
NOTE:
ToString
generated code does not include the@StandardException
fieldsmessage
andcause
.Changing one or many fields to a primitive type (e.g. Integer -> int) also fixes the issue:
Expected behavior
It is expected that a class annotated with both
@StandardException
and@AllArgsConstructor
defining TWO non-primitive fields should successfully compile, similarly to a class defining one, three or more non-primitive parameters.The following code:
should be converted to the equivalent (compiling) code:
Looking at it this way, my hypothesis is that both
@ToString
and@AllArgs
ignore the added fields from@StandardException
. I haven't looked at Lombok code in a long time but it seems that@StandardException
should have priority into extending the class so that other annotations can pickup its additions and process them like regular code.Version info (please complete the following information):
The text was updated successfully, but these errors were encountered: