Skip to content

Commit 0a78eaf

Browse files
committed
Do not build exception instances in advance
When no `cause` and no clean context of the exception thrown, the code place where an exception has been created is confusing. * Fix `SimpleMessageStore` to create an `out of capacity` exception in the `addMessagesToGroup()` exactly at the point where it is thrown * Fix `FunctionExpression` and `SupplierExpression` do not pre-create `readOnlyException`: it is unlikely these kind of expressions are going to be used in the SpEL `write` context, so we save some time and memory not creating extra object and the plain call `throw new` gives us a clean context what method call has ended up with such an exception
1 parent f8fff81 commit 0a78eaf

File tree

4 files changed

+91
-101
lines changed

4 files changed

+91
-101
lines changed

spring-integration-core/src/main/java/org/springframework/integration/expression/FunctionExpression.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,13 +57,9 @@ public class FunctionExpression<S> implements Expression {
5757

5858
private final EvaluationContext defaultContext = new StandardEvaluationContext();
5959

60-
private final EvaluationException readOnlyException;
61-
6260
public FunctionExpression(Function<S, ?> function) {
6361
Assert.notNull(function, "'function' must not be null.");
6462
this.function = function;
65-
this.readOnlyException = new EvaluationException(getExpressionString(),
66-
"FunctionExpression is a 'read only' Expression implementation");
6763
}
6864

6965
@Override
@@ -123,60 +119,65 @@ public <T> T getValue(EvaluationContext context, @Nullable Object rootObject, @N
123119

124120
@Override
125121
public Class<?> getValueType() throws EvaluationException {
126-
throw this.readOnlyException;
122+
throw readOnlyException();
127123
}
128124

129125
@Override
130126
public Class<?> getValueType(@Nullable Object rootObject) throws EvaluationException {
131-
throw this.readOnlyException;
127+
throw readOnlyException();
132128
}
133129

134130
@Override
135131
public Class<?> getValueType(EvaluationContext context) throws EvaluationException {
136-
throw this.readOnlyException;
132+
throw readOnlyException();
137133
}
138134

139135
@Override
140136
public Class<?> getValueType(EvaluationContext context, @Nullable Object rootObject) throws EvaluationException {
141-
throw this.readOnlyException;
137+
throw readOnlyException();
142138
}
143139

144140
@Override
145141
public TypeDescriptor getValueTypeDescriptor() throws EvaluationException {
146-
throw this.readOnlyException;
142+
throw readOnlyException();
147143
}
148144

149145
@Override
150146
public TypeDescriptor getValueTypeDescriptor(@Nullable Object rootObject) throws EvaluationException {
151-
throw this.readOnlyException;
147+
throw readOnlyException();
152148
}
153149

154150
@Override
155151
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) throws EvaluationException {
156-
throw this.readOnlyException;
152+
throw readOnlyException();
157153
}
158154

159155
@Override
160156
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, @Nullable Object rootObject)
161157
throws EvaluationException {
162-
throw this.readOnlyException;
158+
throw readOnlyException();
163159
}
164160

165161
@Override
166162
public void setValue(EvaluationContext context, @Nullable Object value) throws EvaluationException {
167-
throw this.readOnlyException;
163+
throw readOnlyException();
168164
}
169165

170166
@Override
171167
public void setValue(@Nullable Object rootObject, @Nullable Object value) throws EvaluationException {
172-
throw this.readOnlyException;
168+
throw readOnlyException();
173169
}
174170

175171
@Override
176172
public void setValue(EvaluationContext context, @Nullable Object rootObject, @Nullable Object value)
177173
throws EvaluationException {
178174

179-
throw this.readOnlyException;
175+
throw readOnlyException();
176+
}
177+
178+
private EvaluationException readOnlyException() {
179+
return new EvaluationException(getExpressionString(),
180+
"FunctionExpression is a 'read only' Expression implementation");
180181
}
181182

182183
@Override

spring-integration-core/src/main/java/org/springframework/integration/expression/SupplierExpression.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,6 +47,7 @@
4747
*
4848
* @author Artem Bilan
4949
* @author Gary Russell
50+
*
5051
* @since 5.0
5152
*/
5253
public class SupplierExpression<T> implements Expression {
@@ -55,13 +56,9 @@ public class SupplierExpression<T> implements Expression {
5556

5657
private final EvaluationContext defaultContext = new StandardEvaluationContext();
5758

58-
private final EvaluationException readOnlyException;
59-
6059
public SupplierExpression(Supplier<T> supplier) {
6160
Assert.notNull(supplier, "'function' must not be null.");
6261
this.supplier = supplier;
63-
this.readOnlyException = new EvaluationException(getExpressionString(),
64-
"SupplierExpression is a 'read only' Expression implementation");
6562
}
6663

6764
@Override
@@ -108,58 +105,63 @@ public <C> C getValue(EvaluationContext context, Object rootObject, Class<C> des
108105

109106
@Override
110107
public Class<?> getValueType() throws EvaluationException {
111-
throw this.readOnlyException;
108+
throw readOnlyException();
112109
}
113110

114111
@Override
115112
public Class<?> getValueType(Object rootObject) throws EvaluationException {
116-
throw this.readOnlyException;
113+
throw readOnlyException();
117114
}
118115

119116
@Override
120117
public Class<?> getValueType(EvaluationContext context) throws EvaluationException {
121-
throw this.readOnlyException;
118+
throw readOnlyException();
122119
}
123120

124121
@Override
125122
public Class<?> getValueType(EvaluationContext context, Object rootObject) throws EvaluationException {
126-
throw this.readOnlyException;
123+
throw readOnlyException();
127124
}
128125

129126
@Override
130127
public TypeDescriptor getValueTypeDescriptor() throws EvaluationException {
131-
throw this.readOnlyException;
128+
throw readOnlyException();
132129
}
133130

134131
@Override
135132
public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException {
136-
throw this.readOnlyException;
133+
throw readOnlyException();
137134
}
138135

139136
@Override
140137
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) throws EvaluationException {
141-
throw this.readOnlyException;
138+
throw readOnlyException();
142139
}
143140

144141
@Override
145142
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject)
146143
throws EvaluationException {
147-
throw this.readOnlyException;
144+
throw readOnlyException();
148145
}
149146

150147
@Override
151148
public void setValue(EvaluationContext context, Object value) throws EvaluationException {
152-
throw this.readOnlyException;
149+
throw readOnlyException();
153150
}
154151

155152
@Override
156153
public void setValue(Object rootObject, Object value) throws EvaluationException {
157-
throw this.readOnlyException;
154+
throw readOnlyException();
158155
}
159156

160157
@Override
161158
public void setValue(EvaluationContext context, Object rootObject, Object value) throws EvaluationException {
162-
throw this.readOnlyException;
159+
throw readOnlyException();
160+
}
161+
162+
private EvaluationException readOnlyException() {
163+
return new EvaluationException(getExpressionString(),
164+
"SupplierExpression is a 'read only' Expression implementation");
163165
}
164166

165167
@Override

spring-integration-core/src/main/java/org/springframework/integration/store/SimpleMessageStore.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -127,6 +127,7 @@ public SimpleMessageStore(int individualCapacity, int groupCapacity, LockRegistr
127127
*/
128128
public SimpleMessageStore(int individualCapacity, int groupCapacity, long upperBoundTimeout,
129129
LockRegistry lockRegistry) {
130+
130131
super(false);
131132
Assert.notNull(lockRegistry, "The LockRegistry cannot be null");
132133
this.individualUpperBound = new UpperBound(individualCapacity);
@@ -278,13 +279,11 @@ public void addMessagesToGroup(Object groupId, Message<?>... messages) {
278279
try {
279280
UpperBound upperBound;
280281
MessageGroup group = this.groupIdToMessageGroup.get(groupId);
281-
MessagingException outOfCapacityException =
282-
new MessagingException(getClass().getSimpleName() +
283-
" was out of capacity (" + this.groupCapacity + ") for group '" + groupId +
284-
"', try constructing it with a larger capacity.");
285282
if (group == null) {
286283
if (this.groupCapacity > 0 && messages.length > this.groupCapacity) {
287-
throw outOfCapacityException;
284+
throw new MessagingException(getClass().getSimpleName() +
285+
" was out of capacity (" + this.groupCapacity + ") for group '" + groupId +
286+
"', try constructing it with a larger number.");
288287
}
289288
group = getMessageGroupFactory().create(groupId);
290289
this.groupIdToMessageGroup.put(groupId, group);
@@ -302,7 +301,9 @@ public void addMessagesToGroup(Object groupId, Message<?>... messages) {
302301
lock.unlock();
303302
if (!upperBound.tryAcquire(this.upperBoundTimeout)) {
304303
unlocked = true;
305-
throw outOfCapacityException;
304+
throw new MessagingException(getClass().getSimpleName() +
305+
" was out of capacity (" + this.groupCapacity + ") for group '" + groupId +
306+
"', try constructing it with a larger number.");
306307
}
307308
lock.lockInterruptibly();
308309
group.add(message);

0 commit comments

Comments
 (0)