Refine null-safety in spring-expression

See gh-32475
This commit is contained in:
Sébastien Deleuze 2024-03-20 09:56:53 +01:00
parent 592f23674d
commit 2e98a8a2a4
8 changed files with 29 additions and 14 deletions

View File

@ -463,7 +463,7 @@ public class TypeDescriptor implements Serializable {
* @see #narrow(Object) * @see #narrow(Object)
*/ */
@Nullable @Nullable
public TypeDescriptor getMapValueTypeDescriptor(Object mapValue) { public TypeDescriptor getMapValueTypeDescriptor(@Nullable Object mapValue) {
return narrow(mapValue, getMapValueTypeDescriptor()); return narrow(mapValue, getMapValueTypeDescriptor());
} }

View File

@ -16,6 +16,8 @@
package org.springframework.expression; package org.springframework.expression;
import org.springframework.lang.Nullable;
/** /**
* Represent an exception that occurs during expression evaluation. * Represent an exception that occurs during expression evaluation.
* *
@ -38,7 +40,7 @@ public class EvaluationException extends ExpressionException {
* @param message description of the problem that occurred * @param message description of the problem that occurred
* @param cause the underlying cause of this exception * @param cause the underlying cause of this exception
*/ */
public EvaluationException(String message, Throwable cause) { public EvaluationException(String message, @Nullable Throwable cause) {
super(message,cause); super(message,cause);
} }
@ -66,7 +68,7 @@ public class EvaluationException extends ExpressionException {
* @param message description of the problem that occurred * @param message description of the problem that occurred
* @param cause the underlying cause of this exception * @param cause the underlying cause of this exception
*/ */
public EvaluationException(int position, String message, Throwable cause) { public EvaluationException(int position, String message, @Nullable Throwable cause) {
super(position, message, cause); super(position, message, cause);
} }

View File

@ -49,7 +49,7 @@ public class ExpressionException extends RuntimeException {
* @param message a descriptive message * @param message a descriptive message
* @param cause the underlying cause of this exception * @param cause the underlying cause of this exception
*/ */
public ExpressionException(String message, Throwable cause) { public ExpressionException(String message, @Nullable Throwable cause) {
super(message, cause); super(message, cause);
this.expressionString = null; this.expressionString = null;
this.position = 0; this.position = 0;
@ -95,7 +95,7 @@ public class ExpressionException extends RuntimeException {
* @param message a descriptive message * @param message a descriptive message
* @param cause the underlying cause of this exception * @param cause the underlying cause of this exception
*/ */
public ExpressionException(int position, String message, Throwable cause) { public ExpressionException(int position, String message, @Nullable Throwable cause) {
super(message, cause); super(message, cause);
this.expressionString = null; this.expressionString = null;
this.position = position; this.position = position;
@ -124,6 +124,7 @@ public class ExpressionException extends RuntimeException {
* @see java.lang.Throwable#getMessage() * @see java.lang.Throwable#getMessage()
*/ */
@Override @Override
@Nullable
public String getMessage() { public String getMessage() {
return toDetailedString(); return toDetailedString();
} }
@ -132,6 +133,7 @@ public class ExpressionException extends RuntimeException {
* Return a detailed description of this exception, including the expression * Return a detailed description of this exception, including the expression
* String and position (if available) as well as the actual exception message. * String and position (if available) as well as the actual exception message.
*/ */
@Nullable
public String toDetailedString() { public String toDetailedString() {
if (this.expressionString != null) { if (this.expressionString != null) {
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
@ -156,6 +158,7 @@ public class ExpressionException extends RuntimeException {
* that caused the failure. * that caused the failure.
* @since 4.0 * @since 4.0
*/ */
@Nullable
public String getSimpleMessage() { public String getSimpleMessage() {
return super.getMessage(); return super.getMessage();
} }

View File

@ -16,6 +16,8 @@
package org.springframework.expression; package org.springframework.expression;
import org.springframework.lang.Nullable;
/** /**
* This exception wraps (as cause) a checked exception thrown by some method that SpEL * This exception wraps (as cause) a checked exception thrown by some method that SpEL
* invokes. It differs from a SpelEvaluationException because this indicates the * invokes. It differs from a SpelEvaluationException because this indicates the
@ -28,7 +30,7 @@ package org.springframework.expression;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class ExpressionInvocationTargetException extends EvaluationException { public class ExpressionInvocationTargetException extends EvaluationException {
public ExpressionInvocationTargetException(int position, String message, Throwable cause) { public ExpressionInvocationTargetException(int position, String message, @Nullable Throwable cause) {
super(position, message, cause); super(position, message, cause);
} }
@ -40,7 +42,7 @@ public class ExpressionInvocationTargetException extends EvaluationException {
super(expressionString, message); super(expressionString, message);
} }
public ExpressionInvocationTargetException(String message, Throwable cause) { public ExpressionInvocationTargetException(String message, @Nullable Throwable cause) {
super(message, cause); super(message, cause);
} }

View File

@ -16,6 +16,8 @@
package org.springframework.expression.spel; package org.springframework.expression.spel;
import org.springframework.lang.Nullable;
/** /**
* Wraps a real parse exception. This exception flows to the top parse method and then * Wraps a real parse exception. This exception flows to the top parse method and then
* the wrapped exception is thrown as the real problem. * the wrapped exception is thrown as the real problem.
@ -26,11 +28,12 @@ package org.springframework.expression.spel;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class InternalParseException extends RuntimeException { public class InternalParseException extends RuntimeException {
public InternalParseException(SpelParseException cause) { public InternalParseException(@Nullable SpelParseException cause) {
super(cause); super(cause);
} }
@Override @Override
@Nullable
public SpelParseException getCause() { public SpelParseException getCause() {
return (SpelParseException) super.getCause(); return (SpelParseException) super.getCause();
} }

View File

@ -17,6 +17,7 @@
package org.springframework.expression.spel; package org.springframework.expression.spel;
import org.springframework.expression.EvaluationException; import org.springframework.expression.EvaluationException;
import org.springframework.lang.Nullable;
/** /**
* Root exception for Spring EL related exceptions. * Root exception for Spring EL related exceptions.
@ -35,28 +36,29 @@ public class SpelEvaluationException extends EvaluationException {
private final SpelMessage message; private final SpelMessage message;
@Nullable
private final Object[] inserts; private final Object[] inserts;
public SpelEvaluationException(SpelMessage message, Object... inserts) { public SpelEvaluationException(SpelMessage message, @Nullable Object... inserts) {
super(message.formatMessage(inserts)); super(message.formatMessage(inserts));
this.message = message; this.message = message;
this.inserts = inserts; this.inserts = inserts;
} }
public SpelEvaluationException(int position, SpelMessage message, Object... inserts) { public SpelEvaluationException(int position, SpelMessage message, @Nullable Object... inserts) {
super(position, message.formatMessage(inserts)); super(position, message.formatMessage(inserts));
this.message = message; this.message = message;
this.inserts = inserts; this.inserts = inserts;
} }
public SpelEvaluationException(int position, Throwable cause, SpelMessage message, Object... inserts) { public SpelEvaluationException(int position, @Nullable Throwable cause, SpelMessage message, @Nullable Object... inserts) {
super(position, message.formatMessage(inserts), cause); super(position, message.formatMessage(inserts), cause);
this.message = message; this.message = message;
this.inserts = inserts; this.inserts = inserts;
} }
public SpelEvaluationException(Throwable cause, SpelMessage message, Object... inserts) { public SpelEvaluationException(@Nullable Throwable cause, SpelMessage message, @Nullable Object... inserts) {
super(message.formatMessage(inserts), cause); super(message.formatMessage(inserts), cause);
this.message = message; this.message = message;
this.inserts = inserts; this.inserts = inserts;
@ -80,6 +82,7 @@ public class SpelEvaluationException extends EvaluationException {
/** /**
* Return the message inserts. * Return the message inserts.
*/ */
@Nullable
public Object[] getInserts() { public Object[] getInserts() {
return this.inserts; return this.inserts;
} }

View File

@ -18,6 +18,8 @@ package org.springframework.expression.spel;
import java.text.MessageFormat; import java.text.MessageFormat;
import org.springframework.lang.Nullable;
/** /**
* Contains all the messages that can be produced by the Spring Expression Language. * Contains all the messages that can be produced by the Spring Expression Language.
* *
@ -313,7 +315,7 @@ public enum SpelMessage {
* @return a formatted message * @return a formatted message
* @since 4.3.5 * @since 4.3.5
*/ */
public String formatMessage(Object... inserts) { public String formatMessage(@Nullable Object... inserts) {
StringBuilder formattedMessage = new StringBuilder(); StringBuilder formattedMessage = new StringBuilder();
formattedMessage.append("EL").append(this.code); formattedMessage.append("EL").append(this.code);
if (this.kind == Kind.ERROR) { if (this.kind == Kind.ERROR) {

View File

@ -71,7 +71,7 @@ public abstract class SpelNodeImpl implements SpelNode, Opcodes {
protected volatile String exitTypeDescriptor; protected volatile String exitTypeDescriptor;
public SpelNodeImpl(int startPos, int endPos, SpelNodeImpl... operands) { public SpelNodeImpl(int startPos, int endPos, @Nullable SpelNodeImpl... operands) {
this.startPos = startPos; this.startPos = startPos;
this.endPos = endPos; this.endPos = endPos;
if (!ObjectUtils.isEmpty(operands)) { if (!ObjectUtils.isEmpty(operands)) {