Skip to content

Commit 15b5dd9

Browse files
committed
Polishing
1 parent cf30603 commit 15b5dd9

File tree

12 files changed

+103
-79
lines changed

12 files changed

+103
-79
lines changed

spring-context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -256,8 +256,8 @@ private Collection<ApplicationListener<?>> retrieveApplicationListeners(
256256
* for the given event type
257257
*/
258258
protected boolean supportsEvent(Class<?> listenerType, ResolvableType eventType) {
259-
if (GenericApplicationListener.class.isAssignableFrom(listenerType)
260-
|| SmartApplicationListener.class.isAssignableFrom(listenerType)) {
259+
if (GenericApplicationListener.class.isAssignableFrom(listenerType) ||
260+
SmartApplicationListener.class.isAssignableFrom(listenerType)) {
261261
return true;
262262
}
263263
ResolvableType declaredEventType = GenericApplicationListenerAdapter.resolveDeclaredEventType(listenerType);

spring-context/src/main/java/org/springframework/context/event/ApplicationEventMulticaster.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -79,6 +79,7 @@ public interface ApplicationEventMulticaster {
7979
* based on the {@code event} instance.
8080
* @param event the event to multicast
8181
* @param eventType the type of event (can be null)
82+
* @since 4.2
8283
*/
8384
void multicastEvent(ApplicationEvent event, ResolvableType eventType);
8485

spring-context/src/main/java/org/springframework/context/event/EventListenerMethodProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -144,7 +144,7 @@ public EventListener inspect(Method method) {
144144
if (CollectionUtils.isEmpty(annotatedMethods)) {
145145
this.nonAnnotatedClasses.add(targetType);
146146
if (logger.isTraceEnabled()) {
147-
logger.trace("No @EventListener annotations found on bean class: " + targetType);
147+
logger.trace("No @EventListener annotations found on bean class: " + targetType.getName());
148148
}
149149
}
150150
else {

spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -27,8 +27,10 @@
2727
import java.util.Set;
2828
import java.util.concurrent.CountDownLatch;
2929
import java.util.concurrent.TimeUnit;
30+
import javax.annotation.PostConstruct;
3031

3132
import org.junit.After;
33+
import org.junit.Ignore;
3234
import org.junit.Rule;
3335
import org.junit.Test;
3436
import org.junit.rules.ExpectedException;
@@ -39,6 +41,7 @@
3941
import org.springframework.beans.factory.BeanInitializationException;
4042
import org.springframework.beans.factory.ObjectFactory;
4143
import org.springframework.beans.factory.annotation.Autowired;
44+
import org.springframework.context.ApplicationEventPublisher;
4245
import org.springframework.context.ConfigurableApplicationContext;
4346
import org.springframework.context.PayloadApplicationEvent;
4447
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -546,6 +549,14 @@ public void orderedListeners() {
546549
assertThat(listener.order, contains("first", "second", "third"));
547550
}
548551

552+
@Test @Ignore // SPR-15122
553+
public void listenersReceiveEarlyEvents() {
554+
load(EventOnPostConstruct.class, OrderedTestListener.class);
555+
OrderedTestListener listener = this.context.getBean(OrderedTestListener.class);
556+
557+
assertThat(listener.order, contains("first", "second", "third"));
558+
}
559+
549560

550561
private void load(Class<?>... classes) {
551562
List<Class<?>> allClasses = new ArrayList<>();
@@ -936,6 +947,18 @@ public void handleSecond(String payload) {
936947
}
937948

938949

950+
static class EventOnPostConstruct {
951+
952+
@Autowired
953+
ApplicationEventPublisher publisher;
954+
955+
@PostConstruct
956+
public void init() {
957+
this.publisher.publishEvent("earlyEvent");
958+
}
959+
}
960+
961+
939962
private static class CustomScope implements org.springframework.beans.factory.config.Scope {
940963

941964
public boolean active = true;

spring-web/src/main/java/org/springframework/http/converter/GenericHttpMessageConverter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -67,14 +67,14 @@ T read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
6767

6868
/**
6969
* Indicates whether the given class can be written by this converter.
70-
* This method should perform the same checks than
70+
* <p>This method should perform the same checks than
7171
* {@link HttpMessageConverter#canWrite(Class, MediaType)} with additional ones
7272
* related to the generic type.
73-
* @param type the (potentially generic) type to test for writability, can be
74-
* {@code null} if not specified.
73+
* @param type the (potentially generic) type to test for writability
74+
* (can be {@code null} if not specified)
7575
* @param clazz the source object class to test for writability
76-
* @param mediaType the media type to write, can be {@code null} if not specified.
77-
* Typically the value of an {@code Accept} header.
76+
* @param mediaType the media type to write (can be {@code null} if not specified);
77+
* typically the value of an {@code Accept} header.
7878
* @return {@code true} if writable; {@code false} otherwise
7979
* @since 4.2
8080
*/

spring-web/src/main/java/org/springframework/http/converter/HttpMessageConverter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -35,17 +35,17 @@ public interface HttpMessageConverter<T> {
3535
/**
3636
* Indicates whether the given class can be read by this converter.
3737
* @param clazz the class to test for readability
38-
* @param mediaType the media type to read, can be {@code null} if not specified.
39-
* Typically the value of a {@code Content-Type} header.
38+
* @param mediaType the media type to read (can be {@code null} if not specified);
39+
* typically the value of a {@code Content-Type} header.
4040
* @return {@code true} if readable; {@code false} otherwise
4141
*/
4242
boolean canRead(Class<?> clazz, MediaType mediaType);
4343

4444
/**
4545
* Indicates whether the given class can be written by this converter.
4646
* @param clazz the class to test for writability
47-
* @param mediaType the media type to write, can be {@code null} if not specified.
48-
* Typically the value of an {@code Accept} header.
47+
* @param mediaType the media type to write (can be {@code null} if not specified);
48+
* typically the value of an {@code Accept} header.
4949
* @return {@code true} if writable; {@code false} otherwise
5050
*/
5151
boolean canWrite(Class<?> clazz, MediaType mediaType);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Provides an HttpMessageConverter for the CBOR data format.
3+
*/
4+
package org.springframework.http.converter.cbor;

spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -50,17 +50,16 @@
5050
*
5151
* <p>Other formats can be supported with additional libraries:
5252
* <ul>
53-
* <li>{@code "application/json"} with the official library
54-
* {@code "com.google.protobuf:protobuf-java-util"}
55-
* <li>{@code "application/json"}, {@code "application/xml"} and {@code "text/html"} (write only)
56-
* can be supported with the 3rd party library
57-
* {@code "com.googlecode.protobuf-java-format:protobuf-java-format"}
53+
* <li>{@code "application/json"} with the official library
54+
* {@code "com.google.protobuf:protobuf-java-util"}
55+
* <li>{@code "application/json"}, {@code "application/xml"} and {@code "text/html"} (write only)
56+
* can be supported with the 3rd party library
57+
* {@code "com.googlecode.protobuf-java-format:protobuf-java-format"}
5858
* </ul>
5959
*
6060
* <p>To generate {@code Message} Java classes, you need to install the {@code protoc} binary.
6161
*
62-
* <p>Requires Protobuf 2.6 and Protobuf Java Format 1.4, as of Spring 4.3.
63-
* Supports up to Protobuf 3.0.0.
62+
* <p>Requires Protobuf 2.6 or 3.x and Protobuf Java Format 1.4 or higher, as of Spring 5.0.
6463
*
6564
* @author Alex Antonov
6665
* @author Brian Clozel
@@ -83,14 +82,15 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
8382
private static final boolean isProtobufJavaFormatPresent =
8483
ClassUtils.isPresent("com.googlecode.protobuf.format.JsonFormat", ProtobufHttpMessageConverter.class.getClassLoader());
8584

86-
private static final MediaType[] SUPPORTED_MEDIATYPES;
87-
88-
private final ProtobufFormatsSupport protobufFormatsSupport;
89-
9085
private static final ConcurrentHashMap<Class<?>, Method> methodCache = new ConcurrentHashMap<>();
9186

87+
private final ProtobufFormatSupport protobufFormatSupport;
88+
9289
private final ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
9390

91+
92+
private static final MediaType[] SUPPORTED_MEDIATYPES;
93+
9494
static {
9595
if (isProtobufJavaFormatPresent) {
9696
SUPPORTED_MEDIATYPES = new MediaType[] {PROTOBUF, MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML,
@@ -104,27 +104,28 @@ else if (isProtobufJavaUtilPresent) {
104104
}
105105
}
106106

107+
107108
/**
108-
* Construct a new instance.
109+
* Construct a new {@code ProtobufHttpMessageConverter}.
109110
*/
110111
public ProtobufHttpMessageConverter() {
111112
this(null);
112113
}
113114

114115
/**
115-
* Construct a new instance with an {@link ExtensionRegistryInitializer}
116-
* that allows the registration of message extensions.
116+
* Construct a new {@code ProtobufHttpMessageConverter} with an
117+
* initializer that allows the registration of message extensions.
117118
*/
118119
public ProtobufHttpMessageConverter(ExtensionRegistryInitializer registryInitializer) {
119120
super(SUPPORTED_MEDIATYPES);
120121
if (isProtobufJavaFormatPresent) {
121-
this.protobufFormatsSupport = new ProtobufJavaFormatSupport();
122+
this.protobufFormatSupport = new ProtobufJavaFormatSupport();
122123
}
123124
else if (isProtobufJavaUtilPresent) {
124-
this.protobufFormatsSupport = new ProtobufJavaUtilSupport();
125+
this.protobufFormatSupport = new ProtobufJavaUtilSupport();
125126
}
126127
else {
127-
this.protobufFormatsSupport = null;
128+
this.protobufFormatSupport = null;
128129
}
129130
if (registryInitializer != null) {
130131
registryInitializer.initializeExtensionRegistry(this.extensionRegistry);
@@ -165,7 +166,7 @@ else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
165166
TextFormat.merge(reader, this.extensionRegistry, builder);
166167
}
167168
else if (isProtobufJavaUtilPresent || isProtobufJavaFormatPresent) {
168-
this.protobufFormatsSupport.merge(inputMessage.getBody(), charset, contentType,
169+
this.protobufFormatSupport.merge(inputMessage.getBody(), charset, contentType,
169170
this.extensionRegistry, builder);
170171
}
171172
return builder.build();
@@ -211,7 +212,7 @@ else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
211212
outputMessage.getBody().flush();
212213
}
213214
else if (isProtobufJavaUtilPresent || isProtobufJavaFormatPresent) {
214-
this.protobufFormatsSupport.print(message, outputMessage.getBody(), contentType, charset);
215+
this.protobufFormatSupport.print(message, outputMessage.getBody(), contentType, charset);
215216
outputMessage.getBody().flush();
216217
}
217218
}
@@ -241,15 +242,17 @@ private static Message.Builder getMessageBuilder(Class<? extends Message> clazz)
241242
return (Message.Builder) method.invoke(clazz);
242243
}
243244

244-
private interface ProtobufFormatsSupport {
245245

246-
void merge(InputStream input, Charset cs, MediaType contentType, ExtensionRegistry extensionRegistry,
246+
private interface ProtobufFormatSupport {
247+
248+
void merge(InputStream input, Charset charset, MediaType contentType, ExtensionRegistry extensionRegistry,
247249
Message.Builder builder) throws IOException;
248250

249251
void print(Message message, OutputStream output, MediaType contentType, Charset cs) throws IOException;
250252
}
251253

252-
private class ProtobufJavaUtilSupport implements ProtobufFormatsSupport {
254+
255+
private class ProtobufJavaUtilSupport implements ProtobufFormatSupport {
253256

254257
private final com.google.protobuf.util.JsonFormat.Parser parser;
255258

@@ -261,16 +264,16 @@ public ProtobufJavaUtilSupport() {
261264
}
262265

263266
@Override
264-
public void merge(InputStream input, Charset cs, MediaType contentType,
267+
public void merge(InputStream input, Charset charset, MediaType contentType,
265268
ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException {
266269

267270
if (contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
268-
InputStreamReader reader = new InputStreamReader(input, cs);
271+
InputStreamReader reader = new InputStreamReader(input, charset);
269272
this.parser.merge(reader, builder);
270273
}
271274
else {
272-
throw new UnsupportedOperationException("com.googlecode.protobuf:protobuf-java-util does not support "
273-
+ contentType.toString() + " format");
275+
throw new IOException(
276+
"com.googlecode.protobuf:protobuf-java-util does not support " + contentType + " format");
274277
}
275278
}
276279

@@ -280,14 +283,14 @@ public void print(Message message, OutputStream output, MediaType contentType, C
280283
this.printer.appendTo(message, new OutputStreamWriter(output, cs));
281284
}
282285
else {
283-
throw new UnsupportedOperationException("com.googlecode.protobuf:protobuf-java-util does not support "
284-
+ contentType.toString() + " format");
286+
throw new IOException(
287+
"com.googlecode.protobuf:protobuf-java-util does not support " + contentType + " format");
285288
}
286289
}
287-
288290
}
289291

290-
private class ProtobufJavaFormatSupport implements ProtobufFormatsSupport {
292+
293+
private class ProtobufJavaFormatSupport implements ProtobufFormatSupport {
291294

292295
private final FormatFactory FORMAT_FACTORY;
293296

@@ -305,17 +308,17 @@ public ProtobufJavaFormatSupport() {
305308
}
306309

307310
@Override
308-
public void merge(InputStream input, Charset cs, MediaType contentType,
311+
public void merge(InputStream input, Charset charset, MediaType contentType,
309312
ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException {
313+
310314
if (contentType.isCompatibleWith(MediaType.APPLICATION_JSON)) {
311-
JSON_FORMATTER.merge(input, cs, extensionRegistry, builder);
315+
JSON_FORMATTER.merge(input, charset, extensionRegistry, builder);
312316
}
313317
else if (contentType.isCompatibleWith(MediaType.APPLICATION_XML)) {
314-
XML_FORMATTER.merge(input, cs, extensionRegistry, builder);
318+
XML_FORMATTER.merge(input, charset, extensionRegistry, builder);
315319
}
316320
else {
317-
throw new UnsupportedOperationException("com.google.protobuf.util does not support "
318-
+ contentType.toString() + " format");
321+
throw new IOException("com.google.protobuf.util does not support " + contentType + " format");
319322
}
320323
}
321324

@@ -331,8 +334,7 @@ else if (contentType.isCompatibleWith(MediaType.TEXT_HTML)) {
331334
HTML_FORMATTER.print(message, output, cs);
332335
}
333336
else {
334-
throw new UnsupportedOperationException("com.google.protobuf.util does not support "
335-
+ contentType.toString() + " format");
337+
throw new IOException("com.google.protobuf.util does not support " + contentType + " format");
336338
}
337339
}
338340
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Provides an HttpMessageConverter for the Smile data format ("binary JSON").
3+
*/
4+
package org.springframework.http.converter.smile;

0 commit comments

Comments
 (0)