Skip to content

Commit

Permalink
Refine null-safety in spring-expression
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Mar 20, 2024
1 parent 592f236 commit 2e98a8a
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public TypeDescriptor getMapValueTypeDescriptor() {
* @see #narrow(Object)
*/
@Nullable
public TypeDescriptor getMapValueTypeDescriptor(Object mapValue) {
public TypeDescriptor getMapValueTypeDescriptor(@Nullable Object mapValue) {
return narrow(mapValue, getMapValueTypeDescriptor());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.expression;

import org.springframework.lang.Nullable;

/**
* Represent an exception that occurs during expression evaluation.
*
Expand All @@ -38,7 +40,7 @@ public EvaluationException(String message) {
* @param message description of the problem that occurred
* @param cause the underlying cause of this exception
*/
public EvaluationException(String message, Throwable cause) {
public EvaluationException(String message, @Nullable Throwable cause) {
super(message,cause);
}

Expand Down Expand Up @@ -66,7 +68,7 @@ public EvaluationException(String expressionString, String message) {
* @param message description of the problem that occurred
* @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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ExpressionException(String message) {
* @param message a descriptive message
* @param cause the underlying cause of this exception
*/
public ExpressionException(String message, Throwable cause) {
public ExpressionException(String message, @Nullable Throwable cause) {
super(message, cause);
this.expressionString = null;
this.position = 0;
Expand Down Expand Up @@ -95,7 +95,7 @@ public ExpressionException(int position, String message) {
* @param message a descriptive message
* @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);
this.expressionString = null;
this.position = position;
Expand Down Expand Up @@ -124,6 +124,7 @@ public final int getPosition() {
* @see java.lang.Throwable#getMessage()
*/
@Override
@Nullable
public String getMessage() {
return toDetailedString();
}
Expand All @@ -132,6 +133,7 @@ public String getMessage() {
* Return a detailed description of this exception, including the expression
* String and position (if available) as well as the actual exception message.
*/
@Nullable
public String toDetailedString() {
if (this.expressionString != null) {
StringBuilder output = new StringBuilder();
Expand All @@ -156,6 +158,7 @@ public String toDetailedString() {
* that caused the failure.
* @since 4.0
*/
@Nullable
public String getSimpleMessage() {
return super.getMessage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.expression;

import org.springframework.lang.Nullable;

/**
* This exception wraps (as cause) a checked exception thrown by some method that SpEL
* invokes. It differs from a SpelEvaluationException because this indicates the
Expand All @@ -28,7 +30,7 @@
@SuppressWarnings("serial")
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);
}

Expand All @@ -40,7 +42,7 @@ public ExpressionInvocationTargetException(String expressionString, String messa
super(expressionString, message);
}

public ExpressionInvocationTargetException(String message, Throwable cause) {
public ExpressionInvocationTargetException(String message, @Nullable Throwable cause) {
super(message, cause);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

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
* the wrapped exception is thrown as the real problem.
Expand All @@ -26,11 +28,12 @@
@SuppressWarnings("serial")
public class InternalParseException extends RuntimeException {

public InternalParseException(SpelParseException cause) {
public InternalParseException(@Nullable SpelParseException cause) {
super(cause);
}

@Override
@Nullable
public SpelParseException getCause() {
return (SpelParseException) super.getCause();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.expression.spel;

import org.springframework.expression.EvaluationException;
import org.springframework.lang.Nullable;

/**
* Root exception for Spring EL related exceptions.
Expand All @@ -35,28 +36,29 @@ public class SpelEvaluationException extends EvaluationException {

private final SpelMessage message;

@Nullable
private final Object[] inserts;


public SpelEvaluationException(SpelMessage message, Object... inserts) {
public SpelEvaluationException(SpelMessage message, @Nullable Object... inserts) {
super(message.formatMessage(inserts));
this.message = message;
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));
this.message = message;
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);
this.message = message;
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);
this.message = message;
this.inserts = inserts;
Expand All @@ -80,6 +82,7 @@ public SpelMessage getMessageCode() {
/**
* Return the message inserts.
*/
@Nullable
public Object[] getInserts() {
return this.inserts;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.text.MessageFormat;

import org.springframework.lang.Nullable;

/**
* Contains all the messages that can be produced by the Spring Expression Language.
*
Expand Down Expand Up @@ -313,7 +315,7 @@ public enum SpelMessage {
* @return a formatted message
* @since 4.3.5
*/
public String formatMessage(Object... inserts) {
public String formatMessage(@Nullable Object... inserts) {
StringBuilder formattedMessage = new StringBuilder();
formattedMessage.append("EL").append(this.code);
if (this.kind == Kind.ERROR) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public abstract class SpelNodeImpl implements SpelNode, Opcodes {
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.endPos = endPos;
if (!ObjectUtils.isEmpty(operands)) {
Expand Down

0 comments on commit 2e98a8a

Please sign in to comment.