Skip to content

Commit

Permalink
Merge pull request #3674 from Rawi01/standardexception-fields
Browse files Browse the repository at this point in the history
Add cast for null arguments
  • Loading branch information
rzwitserloot authored Jun 27, 2024
2 parents c61a404 + 4c3ede2 commit da815a6
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 12 deletions.
10 changes: 7 additions & 3 deletions src/core/lombok/eclipse/handlers/HandleStandardException.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
10 changes: 7 additions & 3 deletions src/core/lombok/javac/handlers/HandleStandardException.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -81,7 +81,9 @@ private void generateNoArgsConstructor(JavacNode typeNode, AccessLevel level, Ja
if (hasConstructor(typeNode) != MemberExistsResult.NOT_EXISTS) return;
JavacTreeMaker maker = typeNode.getTreeMaker();

List<JCExpression> args = List.<JCExpression>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<JCExpression> args = List.<JCExpression>of(stringArgument, throwableArgument);
JCStatement thisCall = maker.Exec(maker.Apply(List.<JCExpression>nil(), maker.Ident(typeNode.toName("this")), args));
JCMethodDecl constr = createConstructor(level, typeNode, false, false, source, List.of(thisCall));
injectMethod(typeNode, constr);
Expand All @@ -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<JCExpression> args = List.<JCExpression>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<JCExpression> args = List.<JCExpression>of(stringArgument, throwableArgument);
JCStatement thisCall = maker.Exec(maker.Apply(List.<JCExpression>nil(), maker.Ident(typeNode.toName("this")), args));
JCMethodDecl constr = createConstructor(level, typeNode, true, false, source, List.of(thisCall));
injectMethod(typeNode, constr);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
6 changes: 3 additions & 3 deletions test/transform/resource/after-ecj/StandardExceptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import lombok.experimental.StandardException;

@StandardException
public class StandardExceptionWithConstructor extends Exception {
public StandardExceptionWithConstructor(Integer x, Integer y) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4 The serializable class StandardExceptionWithConstructor does not declare a static final serialVersionUID field of type long

0 comments on commit da815a6

Please sign in to comment.