diff --git a/src/core/lombok/eclipse/handlers/HandleStandardException.java b/src/core/lombok/eclipse/handlers/HandleStandardException.java index def6e495e..4672555cf 100755 --- a/src/core/lombok/eclipse/handlers/HandleStandardException.java +++ b/src/core/lombok/eclipse/handlers/HandleStandardException.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 The Project Lombok Authors. + * Copyright (C) 2021-2024 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -70,8 +70,10 @@ private void generateNoArgsConstructor(EclipseNode typeNode, AccessLevel level, if (hasConstructor(typeNode) != MemberExistsResult.NOT_EXISTS) return; int pS = source.get().sourceStart, pE = source.get().sourceEnd; + Expression messageArgument = new CastExpression(new NullLiteral(pS, pE), generateQualifiedTypeRef(source.get(), TypeConstants.JAVA_LANG_STRING)); + Expression causeArgument = new CastExpression(new NullLiteral(pS, pE), generateQualifiedTypeRef(source.get(), TypeConstants.JAVA_LANG_THROWABLE)); ExplicitConstructorCall explicitCall = new ExplicitConstructorCall(ExplicitConstructorCall.This); - explicitCall.arguments = new Expression[] {new NullLiteral(pS, pE), new NullLiteral(pS, pE)}; + explicitCall.arguments = new Expression[] {messageArgument, causeArgument}; ConstructorDeclaration constructor = createConstructor(level, typeNode, false, false, source, explicitCall, null); injectMethod(typeNode, constructor); } @@ -81,8 +83,10 @@ private void generateMsgOnlyConstructor(EclipseNode typeNode, AccessLevel level, int pS = source.get().sourceStart, pE = source.get().sourceEnd; long p = (long) pS << 32 | pE; + Expression messageArgument = new SingleNameReference(MESSAGE, p); + Expression causeArgument = new CastExpression(new NullLiteral(pS, pE), generateQualifiedTypeRef(source.get(), TypeConstants.JAVA_LANG_THROWABLE)); ExplicitConstructorCall explicitCall = new ExplicitConstructorCall(ExplicitConstructorCall.This); - explicitCall.arguments = new Expression[] {new SingleNameReference(MESSAGE, p), new NullLiteral(pS, pE)}; + explicitCall.arguments = new Expression[] {messageArgument, causeArgument}; ConstructorDeclaration constructor = createConstructor(level, typeNode, true, false, source, explicitCall, null); injectMethod(typeNode, constructor); } diff --git a/src/core/lombok/javac/handlers/HandleStandardException.java b/src/core/lombok/javac/handlers/HandleStandardException.java index dd764233c..56bf8905e 100644 --- a/src/core/lombok/javac/handlers/HandleStandardException.java +++ b/src/core/lombok/javac/handlers/HandleStandardException.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 The Project Lombok Authors. + * Copyright (C) 2021-2024 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -81,7 +81,9 @@ private void generateNoArgsConstructor(JavacNode typeNode, AccessLevel level, Ja if (hasConstructor(typeNode) != MemberExistsResult.NOT_EXISTS) return; JavacTreeMaker maker = typeNode.getTreeMaker(); - List args = List.of(maker.Literal(CTC_BOT, null), maker.Literal(CTC_BOT, null)); + JCExpression stringArgument = maker.TypeCast(genJavaLangTypeRef(typeNode, "String"), maker.Literal(CTC_BOT, null)); + JCExpression throwableArgument = maker.TypeCast(genJavaLangTypeRef(typeNode, "Throwable"), maker.Literal(CTC_BOT, null)); + List args = List.of(stringArgument, throwableArgument); JCStatement thisCall = maker.Exec(maker.Apply(List.nil(), maker.Ident(typeNode.toName("this")), args)); JCMethodDecl constr = createConstructor(level, typeNode, false, false, source, List.of(thisCall)); injectMethod(typeNode, constr); @@ -91,7 +93,9 @@ private void generateMsgOnlyConstructor(JavacNode typeNode, AccessLevel level, J if (hasConstructor(typeNode, String.class) != MemberExistsResult.NOT_EXISTS) return; JavacTreeMaker maker = typeNode.getTreeMaker(); - List args = List.of(maker.Ident(typeNode.toName("message")), maker.Literal(CTC_BOT, null)); + JCExpression stringArgument = maker.Ident(typeNode.toName("message")); + JCExpression throwableArgument = maker.TypeCast(genJavaLangTypeRef(typeNode, "Throwable"), maker.Literal(CTC_BOT, null)); + List args = List.of(stringArgument, throwableArgument); JCStatement thisCall = maker.Exec(maker.Apply(List.nil(), maker.Ident(typeNode.toName("this")), args)); JCMethodDecl constr = createConstructor(level, typeNode, true, false, source, List.of(thisCall)); injectMethod(typeNode, constr); diff --git a/test/transform/resource/after-delombok/StandardExceptionWithConstructor.java b/test/transform/resource/after-delombok/StandardExceptionWithConstructor.java new file mode 100644 index 000000000..3e1cf3e43 --- /dev/null +++ b/test/transform/resource/after-delombok/StandardExceptionWithConstructor.java @@ -0,0 +1,25 @@ +public class StandardExceptionWithConstructor extends Exception { + public StandardExceptionWithConstructor(Integer x, Integer y) { + } + @java.lang.SuppressWarnings("all") + @lombok.Generated + public StandardExceptionWithConstructor() { + this((java.lang.String) null, (java.lang.Throwable) null); + } + @java.lang.SuppressWarnings("all") + @lombok.Generated + public StandardExceptionWithConstructor(final java.lang.String message) { + this(message, (java.lang.Throwable) null); + } + @java.lang.SuppressWarnings("all") + @lombok.Generated + public StandardExceptionWithConstructor(final java.lang.Throwable cause) { + this(cause != null ? cause.getMessage() : null, cause); + } + @java.lang.SuppressWarnings("all") + @lombok.Generated + public StandardExceptionWithConstructor(final java.lang.String message, final java.lang.Throwable cause) { + super(message); + if (cause != null) super.initCause(cause); + } +} diff --git a/test/transform/resource/after-delombok/StandardExceptions.java b/test/transform/resource/after-delombok/StandardExceptions.java index af2edf6b7..ecfabb417 100644 --- a/test/transform/resource/after-delombok/StandardExceptions.java +++ b/test/transform/resource/after-delombok/StandardExceptions.java @@ -2,12 +2,12 @@ class EmptyException extends Exception { @java.lang.SuppressWarnings("all") @lombok.Generated public EmptyException() { - this(null, null); + this((java.lang.String) null, (java.lang.Throwable) null); } @java.lang.SuppressWarnings("all") @lombok.Generated public EmptyException(final java.lang.String message) { - this(message, null); + this(message, (java.lang.Throwable) null); } @java.lang.SuppressWarnings("all") @lombok.Generated @@ -27,7 +27,7 @@ public NoArgsException() { @java.lang.SuppressWarnings("all") @lombok.Generated protected NoArgsException(final java.lang.String message) { - this(message, null); + this(message, (java.lang.Throwable) null); } @java.lang.SuppressWarnings("all") @lombok.Generated diff --git a/test/transform/resource/after-ecj/StandardExceptionWithConstructor.java b/test/transform/resource/after-ecj/StandardExceptionWithConstructor.java new file mode 100644 index 000000000..22f73e639 --- /dev/null +++ b/test/transform/resource/after-ecj/StandardExceptionWithConstructor.java @@ -0,0 +1,20 @@ +import lombok.experimental.StandardException; +public @StandardException class StandardExceptionWithConstructor extends Exception { + public StandardExceptionWithConstructor(Integer x, Integer y) { + super(); + } + public @java.lang.SuppressWarnings("all") @lombok.Generated StandardExceptionWithConstructor() { + this((java.lang.String) null, (java.lang.Throwable) null); + } + public @java.lang.SuppressWarnings("all") @lombok.Generated StandardExceptionWithConstructor(final java.lang.String message) { + this(message, (java.lang.Throwable) null); + } + public @java.lang.SuppressWarnings("all") @lombok.Generated StandardExceptionWithConstructor(final java.lang.Throwable cause) { + this(((cause != null) ? cause.getMessage() : null), cause); + } + public @java.lang.SuppressWarnings("all") @lombok.Generated StandardExceptionWithConstructor(final java.lang.String message, final java.lang.Throwable cause) { + super(message); + if ((cause != null)) + super.initCause(cause); + } +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/StandardExceptions.java b/test/transform/resource/after-ecj/StandardExceptions.java index b5e0f8580..2ba3c02ce 100644 --- a/test/transform/resource/after-ecj/StandardExceptions.java +++ b/test/transform/resource/after-ecj/StandardExceptions.java @@ -2,10 +2,10 @@ import lombok.experimental.StandardException; @StandardException class EmptyException extends Exception { public @java.lang.SuppressWarnings("all") @lombok.Generated EmptyException() { - this(null, null); + this((java.lang.String) null, (java.lang.Throwable) null); } public @java.lang.SuppressWarnings("all") @lombok.Generated EmptyException(final java.lang.String message) { - this(message, null); + this(message, (java.lang.Throwable) null); } public @java.lang.SuppressWarnings("all") @lombok.Generated EmptyException(final java.lang.Throwable cause) { this(((cause != null) ? cause.getMessage() : null), cause); @@ -21,7 +21,7 @@ public NoArgsException() { super(); } protected @java.lang.SuppressWarnings("all") @lombok.Generated NoArgsException(final java.lang.String message) { - this(message, null); + this(message, (java.lang.Throwable) null); } protected @java.lang.SuppressWarnings("all") @lombok.Generated NoArgsException(final java.lang.Throwable cause) { this(((cause != null) ? cause.getMessage() : null), cause); diff --git a/test/transform/resource/before/StandardExceptionWithConstructor.java b/test/transform/resource/before/StandardExceptionWithConstructor.java new file mode 100644 index 000000000..111cadd83 --- /dev/null +++ b/test/transform/resource/before/StandardExceptionWithConstructor.java @@ -0,0 +1,7 @@ +import lombok.experimental.StandardException; + +@StandardException +public class StandardExceptionWithConstructor extends Exception { + public StandardExceptionWithConstructor(Integer x, Integer y) { + } +} \ No newline at end of file diff --git a/test/transform/resource/messages-ecj/StandardExceptionWithConstructor.java.messages b/test/transform/resource/messages-ecj/StandardExceptionWithConstructor.java.messages new file mode 100644 index 000000000..0896e9cfc --- /dev/null +++ b/test/transform/resource/messages-ecj/StandardExceptionWithConstructor.java.messages @@ -0,0 +1 @@ +4 The serializable class StandardExceptionWithConstructor does not declare a static final serialVersionUID field of type long \ No newline at end of file