Nullability fine-tuning around declaration inconsistencies

Issue: SPR-15720
Issue: SPR-15792
This commit is contained in:
Juergen Hoeller
2017-07-19 22:22:14 +02:00
parent 68e6b148cb
commit 46eba3dbfa
186 changed files with 986 additions and 619 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.CodeFlow;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.lang.Nullable;
/**
* Represents a DOT separated expression sequence, such as 'property1.property2.methodOne()'
@@ -91,7 +92,7 @@ public class CompoundExpression extends SpelNodeImpl {
}
@Override
public void setValue(ExpressionState state, Object value) throws EvaluationException {
public void setValue(ExpressionState state, @Nullable Object value) throws EvaluationException {
getValueRef(state).setValue(value);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,7 +25,8 @@ import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage;
import org.springframework.expression.spel.support.BooleanTypedValue;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* The operator 'instanceof' checks if an object is of the class specified in the right
@@ -36,8 +37,10 @@ import org.springframework.expression.spel.support.BooleanTypedValue;
*/
public class OperatorInstanceof extends Operator {
@Nullable
private Class<?> type;
public OperatorInstanceof(int pos, SpelNodeImpl... operands) {
super("instanceof", pos, operands);
}
@@ -47,8 +50,8 @@ public class OperatorInstanceof extends Operator {
* Compare the left operand to see it is an instance of the type specified as the
* right operand. The right operand must be a class.
* @param state the expression state
* @return true if the left operand is an instanceof of the right operand, otherwise
* false
* @return {@code true} if the left operand is an instanceof of the right operand,
* otherwise {@code false}
* @throws EvaluationException if there is a problem evaluating the expression
*/
@Override
@@ -58,7 +61,7 @@ public class OperatorInstanceof extends Operator {
TypedValue right = rightOperand.getValueInternal(state);
Object leftValue = left.getValue();
Object rightValue = right.getValue();
BooleanTypedValue result = null;
BooleanTypedValue result;
if (rightValue == null || !(rightValue instanceof Class)) {
throw new SpelEvaluationException(getRightOperand().getStartPosition(),
SpelMessage.INSTANCEOF_OPERATOR_NEEDS_CLASS_OPERAND,
@@ -89,6 +92,7 @@ public class OperatorInstanceof extends Operator {
public void generateCode(MethodVisitor mv, CodeFlow cf) {
getLeftOperand().generateCode(mv, cf);
CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor());
Assert.state(this.type != null, "No type available");
if (this.type.isPrimitive()) {
// always false - but left operand code always driven
// in case it had side effects
@@ -96,7 +100,7 @@ public class OperatorInstanceof extends Operator {
mv.visitInsn(ICONST_0); // value of false
}
else {
mv.visitTypeInsn(INSTANCEOF,Type.getInternalName(this.type));
mv.visitTypeInsn(INSTANCEOF, Type.getInternalName(this.type));
}
cf.pushDescriptor(this.exitTypeDescriptor);
}

View File

@@ -24,6 +24,7 @@ import org.springframework.expression.EvaluationException;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.CodeFlow;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
@@ -35,15 +36,16 @@ public class TypeReference extends SpelNodeImpl {
private final int dimensions;
@Nullable
private transient Class<?> type;
public TypeReference(int pos, SpelNodeImpl qualifiedId) {
this(pos,qualifiedId,0);
this(pos, qualifiedId, 0);
}
public TypeReference(int pos, SpelNodeImpl qualifiedId, int dims) {
super(pos,qualifiedId);
super(pos, qualifiedId);
this.dimensions = dims;
}
@@ -99,6 +101,7 @@ public class TypeReference extends SpelNodeImpl {
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
// TODO Future optimization - if followed by a static method call, skip generating code here
Assert.state(this.type != null, "No type available");
if (this.type.isPrimitive()) {
if (this.type == Integer.TYPE) {
mv.visitFieldInsn(GETSTATIC, "java/lang/Integer", "TYPE", "Ljava/lang/Class;");

View File

@@ -68,7 +68,7 @@ public interface ValueRef {
}
@Override
public void setValue(Object newValue) {
public void setValue(@Nullable Object newValue) {
// The exception position '0' isn't right but the overhead of creating
// instances of this per node (where the node is solely for error reporting)
// would be unfortunate.
@@ -102,7 +102,7 @@ public interface ValueRef {
}
@Override
public void setValue(Object newValue) {
public void setValue(@Nullable Object newValue) {
throw new SpelEvaluationException(this.node.pos, SpelMessage.NOT_ASSIGNABLE, this.node.toStringAST());
}

View File

@@ -157,6 +157,7 @@ public class StandardEvaluationContext implements EvaluationContext {
}
@Override
@Nullable
public BeanResolver getBeanResolver() {
return this.beanResolver;
}