Skip to content

Commit 8c83807

Browse files
committed
Polishing (backported from master)
1 parent b64d752 commit 8c83807

File tree

13 files changed

+237
-213
lines changed

13 files changed

+237
-213
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -445,15 +445,14 @@ public boolean isCompilable() {
445445
public void generateCode(MethodVisitor mv, CodeFlow cf) {
446446
ReflectiveConstructorExecutor executor = ((ReflectiveConstructorExecutor) this.cachedExecutor);
447447
Constructor<?> constructor = executor.getConstructor();
448-
String classSlashedDescriptor = constructor.getDeclaringClass().getName().replace('.', '/');
449-
mv.visitTypeInsn(NEW, classSlashedDescriptor);
448+
String classDesc = constructor.getDeclaringClass().getName().replace('.', '/');
449+
mv.visitTypeInsn(NEW, classDesc);
450450
mv.visitInsn(DUP);
451451
// children[0] is the type of the constructor, don't want to include that in argument processing
452-
SpelNodeImpl[] arguments = new SpelNodeImpl[children.length-1];
453-
System.arraycopy(children, 1, arguments, 0, children.length-1);
452+
SpelNodeImpl[] arguments = new SpelNodeImpl[children.length - 1];
453+
System.arraycopy(children, 1, arguments, 0, children.length - 1);
454454
generateCodeForArguments(mv, cf, constructor, arguments);
455-
mv.visitMethodInsn(INVOKESPECIAL, classSlashedDescriptor, "<init>",
456-
CodeFlow.createSignatureDescriptor(constructor), false);
455+
mv.visitMethodInsn(INVOKESPECIAL, classDesc, "<init>", CodeFlow.createSignatureDescriptor(constructor), false);
457456
cf.pushDescriptor(this.exitTypeDescriptor);
458457
}
459458

spring-expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -33,15 +33,15 @@
3333
import org.springframework.util.ReflectionUtils;
3434

3535
/**
36-
* A function reference is of the form "#someFunction(a,b,c)". Functions may be defined in
37-
* the context prior to the expression being evaluated or within the expression itself
36+
* A function reference is of the form "#someFunction(a,b,c)". Functions may be defined
37+
* in the context prior to the expression being evaluated or within the expression itself
3838
* using a lambda function definition. For example: Lambda function definition in an
3939
* expression: "(#max = {|x,y|$x>$y?$x:$y};max(2,3))" Calling context defined function:
4040
* "#isEven(37)". Functions may also be static java methods, registered in the context
4141
* prior to invocation of the expression.
4242
*
43-
* <p>Functions are very simplistic, the arguments are not part of the definition (right
44-
* now), so the names must be unique.
43+
* <p>Functions are very simplistic, the arguments are not part of the definition
44+
* (right now), so the names must be unique.
4545
*
4646
* @author Andy Clement
4747
* @since 3.0
@@ -72,7 +72,8 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep
7272

7373
// Two possibilities: a lambda function or a Java static method registered as a function
7474
if (!(value.getValue() instanceof Method)) {
75-
throw new SpelEvaluationException(SpelMessage.FUNCTION_REFERENCE_CANNOT_BE_INVOKED, this.name, value.getClass());
75+
throw new SpelEvaluationException(
76+
SpelMessage.FUNCTION_REFERENCE_CANNOT_BE_INVOKED, this.name, value.getClass());
7677
}
7778

7879
try {
@@ -113,7 +114,8 @@ private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method
113114
argumentConversionOccurred = ReflectionHelper.convertAllArguments(converter, functionArgs, method);
114115
}
115116
if (method.isVarArgs()) {
116-
functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation(method.getParameterTypes(), functionArgs);
117+
functionArgs =
118+
ReflectionHelper.setupArgumentsForVarargsInvocation(method.getParameterTypes(), functionArgs);
117119
}
118120

119121
try {
@@ -160,13 +162,12 @@ private Object[] getArguments(ExpressionState state) throws EvaluationException
160162

161163
@Override
162164
public boolean isCompilable() {
163-
if (this.method == null || argumentConversionOccurred) {
165+
if (this.method == null || this.argumentConversionOccurred) {
164166
return false;
165167
}
166168
int methodModifiers = this.method.getModifiers();
167-
if (!Modifier.isStatic(methodModifiers) ||
168-
!Modifier.isPublic(methodModifiers) ||
169-
!Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
169+
if (!Modifier.isStatic(methodModifiers) || !Modifier.isPublic(methodModifiers) ||
170+
!Modifier.isPublic(this.method.getDeclaringClass().getModifiers())) {
170171
return false;
171172
}
172173
for (SpelNodeImpl child : this.children) {
@@ -179,9 +180,9 @@ public boolean isCompilable() {
179180

180181
@Override
181182
public void generateCode(MethodVisitor mv,CodeFlow cf) {
182-
String methodDeclaringClassSlashedDescriptor = this.method.getDeclaringClass().getName().replace('.', '/');
183-
generateCodeForArguments(mv, cf, method, this.children);
184-
mv.visitMethodInsn(INVOKESTATIC, methodDeclaringClassSlashedDescriptor, this.method.getName(),
183+
String classDesc = this.method.getDeclaringClass().getName().replace('.', '/');
184+
generateCodeForArguments(mv, cf, this.method, this.children);
185+
mv.visitMethodInsn(INVOKESTATIC, classDesc, this.method.getName(),
185186
CodeFlow.createSignatureDescriptor(this.method), false);
186187
cf.pushDescriptor(this.exitTypeDescriptor);
187188
}

spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -248,13 +248,15 @@ else if ("C".equals(this.exitTypeDescriptor)) {
248248
cf.exitCompilationScope();
249249
mv.visitInsn(insn);
250250
}
251+
251252
else if (this.indexedType == IndexedType.LIST) {
252253
mv.visitTypeInsn(CHECKCAST, "java/util/List");
253254
cf.enterCompilationScope();
254255
this.children[0].generateCode(mv, cf);
255256
cf.exitCompilationScope();
256257
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;", true);
257258
}
259+
258260
else if (this.indexedType == IndexedType.MAP) {
259261
mv.visitTypeInsn(CHECKCAST, "java/util/Map");
260262
// Special case when the key is an unquoted string literal that will be parsed as
@@ -271,27 +273,30 @@ else if (this.indexedType == IndexedType.MAP) {
271273
}
272274
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
273275
}
276+
274277
else if (this.indexedType == IndexedType.OBJECT) {
275278
ReflectivePropertyAccessor.OptimalPropertyAccessor accessor =
276279
(ReflectivePropertyAccessor.OptimalPropertyAccessor) this.cachedReadAccessor;
277280
Member member = accessor.member;
278281
boolean isStatic = Modifier.isStatic(member.getModifiers());
279-
String memberDeclaringClassSlashedDescriptor = member.getDeclaringClass().getName().replace('.', '/');
282+
String classDesc = member.getDeclaringClass().getName().replace('.', '/');
283+
280284
if (!isStatic) {
281285
if (descriptor == null) {
282286
cf.loadTarget(mv);
283287
}
284-
if (descriptor == null || !memberDeclaringClassSlashedDescriptor.equals(descriptor.substring(1))) {
285-
mv.visitTypeInsn(CHECKCAST, memberDeclaringClassSlashedDescriptor);
288+
if (descriptor == null || !classDesc.equals(descriptor.substring(1))) {
289+
mv.visitTypeInsn(CHECKCAST, classDesc);
286290
}
287291
}
288-
if (member instanceof Field) {
289-
mv.visitFieldInsn(isStatic ? GETSTATIC : GETFIELD, memberDeclaringClassSlashedDescriptor,
290-
member.getName(), CodeFlow.toJvmDescriptor(((Field) member).getType()));
292+
293+
if (member instanceof Method) {
294+
mv.visitMethodInsn((isStatic? INVOKESTATIC : INVOKEVIRTUAL), classDesc, member.getName(),
295+
CodeFlow.createSignatureDescriptor((Method) member), false);
291296
}
292297
else {
293-
mv.visitMethodInsn(isStatic? INVOKESTATIC : INVOKEVIRTUAL, memberDeclaringClassSlashedDescriptor,
294-
member.getName(), CodeFlow.createSignatureDescriptor((Method) member), false);
298+
mv.visitFieldInsn((isStatic ? GETSTATIC : GETFIELD), classDesc, member.getName(),
299+
CodeFlow.toJvmDescriptor(((Field) member).getType()));
295300
}
296301
}
297302

@@ -555,12 +560,8 @@ public TypedValue getValue() {
555560
ReflectivePropertyAccessor.OptimalPropertyAccessor optimalAccessor =
556561
(ReflectivePropertyAccessor.OptimalPropertyAccessor) accessor;
557562
Member member = optimalAccessor.member;
558-
if (member instanceof Field) {
559-
Indexer.this.exitTypeDescriptor = CodeFlow.toDescriptor(((Field)member).getType());
560-
}
561-
else {
562-
Indexer.this.exitTypeDescriptor = CodeFlow.toDescriptor(((Method)member).getReturnType());
563-
}
563+
Indexer.this.exitTypeDescriptor = CodeFlow.toDescriptor(member instanceof Method ?
564+
((Method) member).getReturnType() : ((Field) member).getType());
564565
}
565566
return accessor.read(this.evaluationContext, this.targetObject, this.name);
566567
}

spring-expression/src/main/java/org/springframework/expression/spel/ast/SpelNodeImpl.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,15 @@ protected static void generateCodeForArguments(MethodVisitor mv, CodeFlow cf, Me
239239
// The final parameter may or may not need packaging into an array, or nothing may
240240
// have been passed to satisfy the varargs and so something needs to be built.
241241
int p = 0; // Current supplied argument being processed
242-
int childcount = arguments.length;
242+
int childCount = arguments.length;
243243

244244
// Fulfill all the parameter requirements except the last one
245-
for (p = 0; p < paramDescriptors.length-1;p++) {
245+
for (p = 0; p < paramDescriptors.length - 1; p++) {
246246
generateCodeForArgument(mv, cf, arguments[p], paramDescriptors[p]);
247247
}
248248

249-
SpelNodeImpl lastchild = (childcount == 0 ? null : arguments[childcount-1]);
250-
String arraytype = paramDescriptors[paramDescriptors.length-1];
249+
SpelNodeImpl lastchild = (childCount == 0 ? null : arguments[childCount - 1]);
250+
String arraytype = paramDescriptors[paramDescriptors.length - 1];
251251
// Determine if the final passed argument is already suitably packaged in array
252252
// form to be passed to the method
253253
if (lastchild != null && lastchild.getExitDescriptor().equals(arraytype)) {
@@ -256,10 +256,10 @@ protected static void generateCodeForArguments(MethodVisitor mv, CodeFlow cf, Me
256256
else {
257257
arraytype = arraytype.substring(1); // trim the leading '[', may leave other '['
258258
// build array big enough to hold remaining arguments
259-
CodeFlow.insertNewArrayCode(mv, childcount-p, arraytype);
259+
CodeFlow.insertNewArrayCode(mv, childCount - p, arraytype);
260260
// Package up the remaining arguments into the array
261261
int arrayindex = 0;
262-
while (p < childcount) {
262+
while (p < childCount) {
263263
SpelNodeImpl child = arguments[p];
264264
mv.visitInsn(DUP);
265265
CodeFlow.insertOptimalLoad(mv, arrayindex++);
@@ -280,20 +280,20 @@ protected static void generateCodeForArguments(MethodVisitor mv, CodeFlow cf, Me
280280
* Ask an argument to generate its bytecode and then follow it up
281281
* with any boxing/unboxing/checkcasting to ensure it matches the expected parameter descriptor.
282282
*/
283-
protected static void generateCodeForArgument(MethodVisitor mv, CodeFlow cf, SpelNodeImpl argument, String paramDescriptor) {
283+
protected static void generateCodeForArgument(MethodVisitor mv, CodeFlow cf, SpelNodeImpl argument, String paramDesc) {
284284
cf.enterCompilationScope();
285285
argument.generateCode(mv, cf);
286286
boolean primitiveOnStack = CodeFlow.isPrimitive(cf.lastDescriptor());
287287
// Check if need to box it for the method reference?
288-
if (primitiveOnStack && paramDescriptor.charAt(0) == 'L') {
288+
if (primitiveOnStack && paramDesc.charAt(0) == 'L') {
289289
CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor().charAt(0));
290290
}
291-
else if (paramDescriptor.length() == 1 && !primitiveOnStack) {
292-
CodeFlow.insertUnboxInsns(mv, paramDescriptor.charAt(0), cf.lastDescriptor());
291+
else if (paramDesc.length() == 1 && !primitiveOnStack) {
292+
CodeFlow.insertUnboxInsns(mv, paramDesc.charAt(0), cf.lastDescriptor());
293293
}
294-
else if (!cf.lastDescriptor().equals(paramDescriptor)) {
294+
else if (!cf.lastDescriptor().equals(paramDesc)) {
295295
// This would be unnecessary in the case of subtyping (e.g. method takes Number but Integer passed in)
296-
CodeFlow.insertCheckCast(mv, paramDescriptor);
296+
CodeFlow.insertCheckCast(mv, paramDesc);
297297
}
298298
cf.exitCompilationScope();
299299
}

spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,13 @@ else if (typeConverter.canConvert(suppliedArg, TypeDescriptor.valueOf(varargsPar
238238
* @return true if some kind of conversion occurred on the argument
239239
* @throws SpelEvaluationException if there is a problem with conversion
240240
*/
241-
public static boolean convertAllArguments(TypeConverter converter, Object[] arguments, Method method) throws SpelEvaluationException {
242-
Integer varargsPosition = method.isVarArgs() ? method.getParameterTypes().length-1:null;
241+
public static boolean convertAllArguments(TypeConverter converter, Object[] arguments, Method method)
242+
throws SpelEvaluationException {
243+
244+
Integer varargsPosition = (method.isVarArgs() ? method.getParameterTypes().length - 1 : null);
243245
return convertArguments(converter, arguments, method, varargsPosition);
244246
}
245-
247+
246248
/**
247249
* Takes an input set of argument values and converts them to the types specified as the
248250
* required parameter types. The arguments are converted 'in-place' in the input array.

spring-messaging/src/main/java/org/springframework/messaging/tcp/reactor/AbstractPromiseToListenableFutureAdapter.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,20 @@ public void accept(S result) {
5656
try {
5757
registry.success(adapt(result));
5858
}
59-
catch (Throwable t) {
60-
registry.failure(t);
59+
catch (Throwable ex) {
60+
registry.failure(ex);
6161
}
6262
}
6363
});
6464

6565
this.promise.onError(new Consumer<Throwable>() {
6666
@Override
67-
public void accept(Throwable t) {
68-
registry.failure(t);
67+
public void accept(Throwable ex) {
68+
registry.failure(ex);
6969
}
7070
});
7171
}
7272

73-
protected abstract T adapt(S result);
7473

7574
@Override
7675
public T get() throws InterruptedException {
@@ -113,4 +112,7 @@ public void addCallback(SuccessCallback<? super T> successCallback, FailureCallb
113112
this.registry.addFailureCallback(failureCallback);
114113
}
115114

115+
116+
protected abstract T adapt(S result);
117+
116118
}

spring-messaging/src/main/java/org/springframework/messaging/tcp/reactor/Reactor11TcpClient.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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,7 +47,6 @@
4747
import org.springframework.util.Assert;
4848
import org.springframework.util.concurrent.ListenableFuture;
4949

50-
5150
/**
5251
* An implementation of {@link org.springframework.messaging.tcp.TcpOperations}
5352
* based on the TCP client support of the Reactor project.
@@ -70,33 +69,23 @@ public class Reactor11TcpClient<P> implements TcpOperations<P> {
7069
* A constructor that creates a {@link reactor.net.netty.tcp.NettyTcpClient} with
7170
* a {@link reactor.event.dispatch.SynchronousDispatcher} as a result of which
7271
* network I/O is handled in Netty threads.
73-
*
7472
* <p>Also see the constructor accepting a pre-configured Reactor
7573
* {@link reactor.net.tcp.TcpClient}.
76-
*
7774
* @param host the host to connect to
7875
* @param port the port to connect to
7976
* @param codec the codec to use for encoding and decoding the TCP stream
8077
*/
8178
public Reactor11TcpClient(String host, int port, Codec<Buffer, Message<P>, Message<P>> codec) {
82-
83-
// Revisit in 1.1: is Environment still required w/ sync dispatcher?
8479
this.environment = new Environment(new SynchronousDispatcherConfigReader());
85-
8680
this.tcpClient = new TcpClientSpec<Message<P>, Message<P>>(REACTOR_TCP_CLIENT_TYPE)
87-
.env(this.environment)
88-
.codec(codec)
89-
.connect(host, port)
90-
.get();
81+
.env(this.environment).codec(codec).connect(host, port).get();
9182
}
9283

9384
/**
9485
* A constructor with a pre-configured {@link reactor.net.tcp.TcpClient}.
95-
*
9686
* <p><strong>NOTE:</strong> if the client is configured with a thread-creating
9787
* dispatcher, you are responsible for shutting down the {@link reactor.core.Environment}
9888
* instance with which the client is configured.
99-
*
10089
* @param tcpClient the TcpClient to use
10190
*/
10291
public Reactor11TcpClient(TcpClient<Message<P>, Message<P>> tcpClient) {
@@ -108,7 +97,6 @@ public Reactor11TcpClient(TcpClient<Message<P>, Message<P>> tcpClient) {
10897

10998
@Override
11099
public ListenableFuture<Void> connect(TcpConnectionHandler<P> connectionHandler) {
111-
112100
Promise<NetChannel<Message<P>, Message<P>>> promise = this.tcpClient.open();
113101
composeConnectionHandling(promise, connectionHandler);
114102

@@ -125,7 +113,6 @@ public ListenableFuture<Void> connect(final TcpConnectionHandler<P> connectionHa
125113
final ReconnectStrategy reconnectStrategy) {
126114

127115
Assert.notNull(reconnectStrategy, "ReconnectStrategy must not be null");
128-
129116
Reconnect reconnect = new Reconnect() {
130117
@Override
131118
public Tuple2<InetSocketAddress, Long> reconnect(InetSocketAddress address, int attempt) {
@@ -162,8 +149,8 @@ public void accept(NetChannel<Message<P>, Message<P>> connection) {
162149
connection
163150
.when(Throwable.class, new Consumer<Throwable>() {
164151
@Override
165-
public void accept(Throwable t) {
166-
connectionHandler.handleFailure(t);
152+
public void accept(Throwable ex) {
153+
connectionHandler.handleFailure(ex);
167154
}
168155
})
169156
.consume(new Consumer<Message<P>>() {
@@ -201,9 +188,9 @@ protected Boolean adapt(Boolean result) {
201188
}
202189
}
203190

191+
204192
/**
205193
* A ConfigurationReader that enforces the use of a SynchronousDispatcher.
206-
*
207194
* <p>The {@link reactor.core.configuration.PropertiesConfigurationReader} used by
208195
* default automatically creates other dispatchers with thread pools that are
209196
* not needed.

0 commit comments

Comments
 (0)