From ebd2ec57fc23e901d6b73ccb5fbca5ffb2514cd8 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 12 Feb 2020 17:58:26 +0100 Subject: [PATCH] Polishing --- .../annotation/ConfigurationClassParser.java | 16 +++++----------- .../annotation/DeferredImportSelector.java | 3 ++- .../context/annotation/ImportSelector.java | 3 ++- .../context/annotation/ImportSelectorTests.java | 5 +++-- .../datasource/init/DatabasePopulatorUtils.java | 8 ++++---- .../http/codec/json/AbstractJackson2Decoder.java | 10 ++++++---- .../http/codec/json/AbstractJackson2Encoder.java | 12 ++++++------ .../http/codec/json/Jackson2Tokenizer.java | 16 +++++++--------- .../springframework/web/client/RestTemplate.java | 3 ++- 9 files changed, 37 insertions(+), 39 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index 3b7be52f8b0..bee96e974f8 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -748,8 +748,7 @@ class ConfigurationClassParser { * @param importSelector the selector to handle */ public void handle(ConfigurationClass configClass, DeferredImportSelector importSelector) { - DeferredImportSelectorHolder holder = new DeferredImportSelectorHolder( - configClass, importSelector); + DeferredImportSelectorHolder holder = new DeferredImportSelectorHolder(configClass, importSelector); if (this.deferredImportSelectors == null) { DeferredImportSelectorGroupingHandler handler = new DeferredImportSelectorGroupingHandler(); handler.register(holder); @@ -775,7 +774,6 @@ class ConfigurationClassParser { this.deferredImportSelectors = new ArrayList<>(); } } - } @@ -786,8 +784,7 @@ class ConfigurationClassParser { private final Map configurationClasses = new HashMap<>(); public void register(DeferredImportSelectorHolder deferredImport) { - Class group = deferredImport.getImportSelector() - .getImportGroup(); + Class group = deferredImport.getImportSelector().getImportGroup(); DeferredImportSelectorGrouping grouping = this.groupings.computeIfAbsent( (group != null ? group : deferredImport), key -> new DeferredImportSelectorGrouping(createGroup(group))); @@ -799,8 +796,7 @@ class ConfigurationClassParser { public void processGroupImports() { for (DeferredImportSelectorGrouping grouping : this.groupings.values()) { grouping.getImports().forEach(entry -> { - ConfigurationClass configurationClass = this.configurationClasses.get( - entry.getMetadata()); + ConfigurationClass configurationClass = this.configurationClasses.get(entry.getMetadata()); try { processImports(configurationClass, asSourceClass(configurationClass), asSourceClasses(entry.getImportClassName()), false); @@ -818,8 +814,7 @@ class ConfigurationClassParser { } private Group createGroup(@Nullable Class type) { - Class effectiveType = (type != null ? type - : DefaultDeferredImportSelectorGroup.class); + Class effectiveType = (type != null ? type : DefaultDeferredImportSelectorGroup.class); Group group = BeanUtils.instantiateClass(effectiveType); ParserStrategyUtils.invokeAwareMethods(group, ConfigurationClassParser.this.environment, @@ -827,7 +822,6 @@ class ConfigurationClassParser { ConfigurationClassParser.this.registry); return group; } - } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/DeferredImportSelector.java b/spring-context/src/main/java/org/springframework/context/annotation/DeferredImportSelector.java index 6bca9156781..dedb068b649 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/DeferredImportSelector.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/DeferredImportSelector.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -51,6 +51,7 @@ public interface DeferredImportSelector extends ImportSelector { /** * Interface used to group results from different import selectors. + * @since 5.0 */ interface Group { diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ImportSelector.java b/spring-context/src/main/java/org/springframework/context/annotation/ImportSelector.java index 2e41305958d..e7305c2b63d 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ImportSelector.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ImportSelector.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2020 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. @@ -50,6 +50,7 @@ public interface ImportSelector { /** * Select and return the names of which class(es) should be imported based on * the {@link AnnotationMetadata} of the importing @{@link Configuration} class. + * @return the class names, or an empty array if none */ String[] selectImports(AnnotationMetadata importingClassMetadata); diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java index c7aec9cc585..66eba0278c1 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -530,9 +530,10 @@ public class ImportSelectorTests { static Map> allImports() { return TestImportGroup.imports.entrySet() .stream() - .collect(Collectors.toMap((entry) -> entry.getKey().getClassName(), + .collect(Collectors.toMap(entry -> entry.getKey().getClassName(), Map.Entry::getValue)); } + private final List instanceImports = new ArrayList<>(); @Override diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/DatabasePopulatorUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/DatabasePopulatorUtils.java index 534df4a0821..3861cb5e54b 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/DatabasePopulatorUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/DatabasePopulatorUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 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. @@ -52,10 +52,10 @@ public abstract class DatabasePopulatorUtils { DataSourceUtils.releaseConnection(connection, dataSource); } } + catch (ScriptException ex) { + throw ex; + } catch (Throwable ex) { - if (ex instanceof ScriptException) { - throw (ScriptException) ex; - } throw new UncategorizedScriptException("Failed to execute database script", ex); } } diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Decoder.java b/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Decoder.java index b0df7898245..98e22e3a055 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Decoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Decoder.java @@ -116,7 +116,7 @@ public abstract class AbstractJackson2Decoder extends Jackson2CodecSupport imple @Nullable MimeType mimeType, @Nullable Map hints) { boolean forceUseOfBigDecimal = getObjectMapper().isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); - if (elementType != null && BigDecimal.class.equals(elementType.getType())) { + if (BigDecimal.class.equals(elementType.getType())) { forceUseOfBigDecimal = true; } @@ -130,9 +130,10 @@ public abstract class AbstractJackson2Decoder extends Jackson2CodecSupport imple @Nullable MimeType mimeType, @Nullable Map hints) { boolean forceUseOfBigDecimal = getObjectMapper().isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); - if (elementType != null && BigDecimal.class.equals(elementType.getType())) { + if (BigDecimal.class.equals(elementType.getType())) { forceUseOfBigDecimal = true; } + Flux tokens = Jackson2Tokenizer.tokenize(Flux.from(input), this.jsonFactory, getObjectMapper(), false, forceUseOfBigDecimal, getMaxInMemorySize()); return decodeInternal(tokens, elementType, mimeType, hints).singleOrEmpty(); @@ -177,7 +178,7 @@ public abstract class AbstractJackson2Decoder extends Jackson2CodecSupport imple } - // HttpMessageDecoder... + // HttpMessageDecoder @Override public Map getDecodeHints(ResolvableType actualType, ResolvableType elementType, @@ -191,7 +192,8 @@ public abstract class AbstractJackson2Decoder extends Jackson2CodecSupport imple return getMimeTypes(); } - // Jackson2CodecSupport ... + + // Jackson2CodecSupport @Override protected A getAnnotation(MethodParameter parameter, Class annotType) { diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java b/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java index d7274fbeb42..621405cfd00 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java @@ -170,8 +170,7 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple throw new EncodingException("JSON encoding error: " + ex.getOriginalMessage(), ex); } catch (IOException ex) { - throw new IllegalStateException("Unexpected I/O error while writing to byte array builder", - ex); + throw new IllegalStateException("Unexpected I/O error while writing to byte array builder", ex); } byte[] bytes = byteBuilder.toByteArray(); @@ -197,8 +196,7 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple throw new EncodingException("JSON encoding error: " + ex.getOriginalMessage(), ex); } catch (IOException ex) { - throw new IllegalStateException("Unexpected I/O error while writing to byte array builder", - ex); + throw new IllegalStateException("Unexpected I/O error while writing to byte array builder", ex); } byte[] bytes = byteArrayBuilder.toByteArray(); @@ -233,6 +231,7 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple private ObjectWriter createObjectWriter(ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map hints) { + JavaType javaType = getJavaType(valueType.getType(), null); Class jsonView = (hints != null ? (Class) hints.get(Jackson2CodecSupport.JSON_VIEW_HINT) : null); ObjectWriter writer = (jsonView != null ? @@ -280,7 +279,7 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple } - // HttpMessageEncoder... + // HttpMessageEncoder @Override public List getEncodableMimeTypes() { @@ -299,7 +298,8 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple return (actualType != null ? getHints(actualType) : Hints.none()); } - // Jackson2CodecSupport ... + + // Jackson2CodecSupport @Override protected A getAnnotation(MethodParameter parameter, Class annotType) { diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2Tokenizer.java b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2Tokenizer.java index 4861ced5d18..4a173521fbd 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2Tokenizer.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2Tokenizer.java @@ -36,7 +36,6 @@ import org.springframework.core.codec.DecodingException; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferLimitException; import org.springframework.core.io.buffer.DataBufferUtils; -import org.springframework.lang.Nullable; /** * {@link Function} to transform a JSON stream of arbitrary size, byte array @@ -66,7 +65,6 @@ final class Jackson2Tokenizer { private int byteCount; - @Nullable // yet initialized by calling createToken() in the constructor private TokenBuffer tokenBuffer; @@ -84,7 +82,7 @@ final class Jackson2Tokenizer { this.forceUseOfBigDecimal = forceUseOfBigDecimal; this.inputFeeder = (ByteArrayFeeder) this.parser.getNonBlockingInputFeeder(); this.maxInMemorySize = maxInMemorySize; - createToken(); + this.tokenBuffer = createToken(); } @@ -174,9 +172,8 @@ final class Jackson2Tokenizer { if ((token.isStructEnd() || token.isScalarValue()) && this.objectDepth == 0 && this.arrayDepth == 0) { result.add(this.tokenBuffer); - createToken(); + this.tokenBuffer = createToken(); } - } private void processTokenArray(JsonToken token, List result) throws IOException { @@ -187,13 +184,14 @@ final class Jackson2Tokenizer { if (this.objectDepth == 0 && (this.arrayDepth == 0 || this.arrayDepth == 1) && (token == JsonToken.END_OBJECT || token.isScalarValue())) { result.add(this.tokenBuffer); - createToken(); + this.tokenBuffer = createToken(); } } - private void createToken() { - this.tokenBuffer = new TokenBuffer(this.parser, this.deserializationContext); - this.tokenBuffer.forceUseOfBigDecimal(this.forceUseOfBigDecimal); + private TokenBuffer createToken() { + TokenBuffer tokenBuffer = new TokenBuffer(this.parser, this.deserializationContext); + tokenBuffer.forceUseOfBigDecimal(this.forceUseOfBigDecimal); + return tokenBuffer; } private boolean isTopLevelArrayToken(JsonToken token) { diff --git a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java index 74ec11a14ef..321dcca570f 100644 --- a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java +++ b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java @@ -89,7 +89,7 @@ import org.springframework.web.util.UriTemplateHandler; */ public class RestTemplate extends InterceptingHttpAccessor implements RestOperations { - private static boolean romePresent; + private static final boolean romePresent; private static final boolean jaxb2Present; @@ -200,6 +200,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat this.uriTemplateHandler = initUriTemplateHandler(); } + private static DefaultUriBuilderFactory initUriTemplateHandler() { DefaultUriBuilderFactory uriFactory = new DefaultUriBuilderFactory(); uriFactory.setEncodingMode(EncodingMode.URI_COMPONENT); // for backwards compatibility..