From 2694605a4d610688ae31880df9e2bfab94162f01 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 10 Dec 2015 13:25:27 +0000 Subject: [PATCH] Polish --- .../MetricFilterAutoConfigurationTests.java | 3 +- ...figurationMetadataAnnotationProcessor.java | 47 +++++++++---------- ...ationMetadataAnnotationProcessorTests.java | 23 +++------ .../configurationprocessor/TestProject.java | 4 +- .../lombok/LombokInnerClassProperties.java | 2 + .../lombok/LombokSimpleDataProperties.java | 1 + .../lombok/LombokSimpleProperties.java | 1 + .../lombok/SimpleLombokPojo.java | 1 + .../boot/loader/LaunchedURLClassLoader.java | 2 +- .../springframework/boot/ansi/AnsiOutput.java | 12 ++--- 10 files changed, 41 insertions(+), 55 deletions(-) diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfigurationTests.java index bd498905a3..a2cfbb575d 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfigurationTests.java @@ -180,7 +180,8 @@ public class MetricFilterAutoConfigurationTests { @Test public void skipsFilterIfPropertyDisabled() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(context, "endpoints.metrics.filter.enabled:false"); + EnvironmentTestUtils.addEnvironment(context, + "endpoints.metrics.filter.enabled:false"); context.register(Config.class, MetricFilterAutoConfiguration.class); context.refresh(); assertThat(context.getBeansOfType(Filter.class).size(), equalTo(0)); diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java index 7207b411a8..d6901732b1 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java @@ -255,19 +255,8 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor String name = entry.getKey(); ExecutableElement getter = entry.getValue(); VariableElement field = members.getFields().get(name); - Element returnType = this.processingEnv.getTypeUtils() - .asElement(getter.getReturnType()); - AnnotationMirror annotation = getAnnotation(getter, - configurationPropertiesAnnotation()); - boolean isNested = isNested(returnType, field, element); - if (returnType != null && returnType instanceof TypeElement - && annotation == null && isNested) { - String nestedPrefix = ConfigurationMetadata.nestedPrefix(prefix, name); - this.metadataCollector.add(ItemMetadata.newGroup(nestedPrefix, - this.typeUtils.getType(returnType), - this.typeUtils.getType(element), getter.toString())); - processTypeElement(nestedPrefix, (TypeElement) returnType); - } + processNestedType(prefix, element, name, getter, field, + getter.getReturnType()); } } @@ -276,19 +265,8 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor for (Map.Entry entry : members.getFields().entrySet()) { String name = entry.getKey(); VariableElement field = entry.getValue(); - if (!isLombokField(field, element)) { - continue; - } - Element returnType = this.processingEnv.getTypeUtils() - .asElement(field.asType()); - boolean isNested = isNested(returnType, field, element); - if (returnType != null && returnType instanceof TypeElement - && isNested) { - String nestedPrefix = ConfigurationMetadata.nestedPrefix(prefix, name); - this.metadataCollector.add(ItemMetadata.newGroup(nestedPrefix, - this.typeUtils.getType(returnType), - this.typeUtils.getType(element), null)); - processTypeElement(nestedPrefix, (TypeElement) returnType); + if (isLombokField(field, element)) { + processNestedType(prefix, element, name, null, field, field.asType()); } } } @@ -306,6 +284,23 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor || hasAnnotation(element, LOMBOK_DATA_ANNOTATION)); } + private void processNestedType(String prefix, TypeElement element, String name, + ExecutableElement getter, VariableElement field, TypeMirror returnType) { + Element returnElement = this.processingEnv.getTypeUtils().asElement(returnType); + boolean isNested = isNested(returnElement, field, element); + AnnotationMirror annotation = getAnnotation(getter, + configurationPropertiesAnnotation()); + if (returnElement != null && returnElement instanceof TypeElement + && annotation == null && isNested) { + String nestedPrefix = ConfigurationMetadata.nestedPrefix(prefix, name); + this.metadataCollector.add(ItemMetadata.newGroup(nestedPrefix, + this.typeUtils.getType(returnElement), + this.typeUtils.getType(element), + (getter == null ? null : getter.toString()))); + processTypeElement(nestedPrefix, (TypeElement) returnElement); + } + } + private boolean isNested(Element returnType, VariableElement field, TypeElement element) { if (hasAnnotation(field, nestedConfigurationPropertyAnnotation())) { diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java index f35f02387d..9c83ea3de6 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java @@ -319,11 +319,12 @@ public class ConfigurationMetadataAnnotationProcessorTests { assertThat(metadata, containsProperty("config.second.bar.name")); assertThat(metadata, containsGroup("config.third").ofType(SimpleLombokPojo.class) .fromSource(LombokInnerClassProperties.class)); - // For some reason the annotation processor resolves a type for SimpleLombokPojo that - // is resolved (compiled) and the source annotations are gone. Because we don't see the - // @Data annotation anymore, no field is harvested. What is crazy is that a sample project - // works fine so this seem to be related to the unit test environment for some reason. - //assertThat(metadata, containsProperty("config.third.value")); + // For some reason the annotation processor resolves a type for SimpleLombokPojo + // that is resolved (compiled) and the source annotations are gone. Because we + // don't see the @Data annotation anymore, no field is harvested. What is crazy is + // that a sample project works fine so this seem to be related to the unit test + // environment for some reason. assertThat(metadata, + // containsProperty("config.third.value")); assertThat(metadata, containsProperty("config.fourth")); assertThat(metadata, not(containsGroup("config.fourth"))); } @@ -335,7 +336,6 @@ public class ConfigurationMetadataAnnotationProcessorTests { File additionalMetadataFile = new File(metaInfFolder, "additional-spring-configuration-metadata.json"); additionalMetadataFile.createNewFile(); - JSONObject property = new JSONObject(); property.put("name", "foo"); property.put("type", "java.lang.String"); @@ -347,11 +347,8 @@ public class ConfigurationMetadataAnnotationProcessorTests { FileWriter writer = new FileWriter(additionalMetadataFile); additionalMetadata.write(writer); writer.flush(); - ConfigurationMetadata metadata = compile(SimpleProperties.class); - assertThat(metadata, containsProperty("simple.comparator")); - assertThat(metadata, containsProperty("foo", String.class) .fromSource(AdditionalMetadata.class)); } @@ -361,29 +358,23 @@ public class ConfigurationMetadataAnnotationProcessorTests { TestProject project = new TestProject(this.temporaryFolder, FooProperties.class, BarProperties.class); assertFalse(project.getOutputFile(METADATA_PATH).exists()); - ConfigurationMetadata metadata = project.fullBuild(); assertTrue(project.getOutputFile(METADATA_PATH).exists()); - assertThat(metadata, containsProperty("foo.counter").fromSource(FooProperties.class)); assertThat(metadata, containsProperty("bar.counter").fromSource(BarProperties.class)); - metadata = project.incrementalBuild(BarProperties.class); - assertThat(metadata, containsProperty("foo.counter").fromSource(FooProperties.class)); assertThat(metadata, containsProperty("bar.counter").fromSource(BarProperties.class)); - project.addSourceCode(BarProperties.class, BarProperties.class.getResourceAsStream("BarProperties.snippet")); metadata = project.incrementalBuild(BarProperties.class); assertThat(metadata, containsProperty("bar.extra")); assertThat(metadata, containsProperty("foo.counter")); assertThat(metadata, containsProperty("bar.counter")); - project.revert(BarProperties.class); metadata = project.incrementalBuild(BarProperties.class); assertThat(metadata, not(containsProperty("bar.extra"))); @@ -398,7 +389,6 @@ public class ConfigurationMetadataAnnotationProcessorTests { ConfigurationMetadata metadata = project.fullBuild(); assertThat(metadata, containsProperty("foo.counter")); assertThat(metadata, containsProperty("bar.counter")); - project.replaceText(BarProperties.class, "@ConfigurationProperties", "//@ConfigurationProperties"); metadata = project.incrementalBuild(BarProperties.class); @@ -417,7 +407,6 @@ public class ConfigurationMetadataAnnotationProcessorTests { containsProperty("bar.counter").fromSource(BarProperties.class)); assertThat(metadata, not( containsProperty("bar.counter").fromSource(RenamedBarProperties.class))); - project.delete(BarProperties.class); project.add(RenamedBarProperties.class); metadata = project.incrementalBuild(RenamedBarProperties.class); diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestProject.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestProject.java index 6c9ca7fc7b..094280df77 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestProject.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestProject.java @@ -161,7 +161,7 @@ public class TestProject { /** * Restore source code of given class to its original contents. * @param type the class to revert - * @throws IOException + * @throws IOException on IO error */ public void revert(Class type) throws IOException { Assert.assertTrue(getSourceFile(type).exists()); @@ -171,7 +171,7 @@ public class TestProject { /** * Add source code of given class to this project. * @param type the class to add - * @throws IOException + * @throws IOException on IO error */ public void add(Class type) throws IOException { Assert.assertFalse(getSourceFile(type).exists()); diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokInnerClassProperties.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokInnerClassProperties.java index 9a2e1ae024..3294d8e514 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokInnerClassProperties.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokInnerClassProperties.java @@ -28,6 +28,7 @@ import org.springframework.boot.configurationsample.NestedConfigurationProperty; */ @Data @ConfigurationProperties(prefix = "config") +@SuppressWarnings("unused") public class LombokInnerClassProperties { private final Foo first = new Foo(); @@ -58,4 +59,5 @@ public class LombokInnerClassProperties { public enum Fourth { YES, NO } + } diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokSimpleDataProperties.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokSimpleDataProperties.java index 31000b16c5..6c7c610a26 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokSimpleDataProperties.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokSimpleDataProperties.java @@ -30,6 +30,7 @@ import org.springframework.boot.configurationsample.ConfigurationProperties; */ @Data @ConfigurationProperties(prefix = "data") +@SuppressWarnings("unused") public class LombokSimpleDataProperties { private final String id = "super-id"; diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokSimpleProperties.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokSimpleProperties.java index 899a0761d8..369482f71b 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokSimpleProperties.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/LombokSimpleProperties.java @@ -32,6 +32,7 @@ import org.springframework.boot.configurationsample.ConfigurationProperties; @Getter @Setter @ConfigurationProperties(prefix = "simple") +@SuppressWarnings("unused") public class LombokSimpleProperties { private final String id = "super-id"; diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/SimpleLombokPojo.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/SimpleLombokPojo.java index e5ae9ae884..c22e8a183a 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/SimpleLombokPojo.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/lombok/SimpleLombokPojo.java @@ -24,6 +24,7 @@ import lombok.Data; * @author Stephane Nicoll */ @Data +@SuppressWarnings("unused") public class SimpleLombokPojo { private int value; diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java index 28e5da3545..ceb04b1de4 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java @@ -291,4 +291,4 @@ public class LaunchedURLClassLoader extends URLClassLoader { } -} \ No newline at end of file +} diff --git a/spring-boot/src/main/java/org/springframework/boot/ansi/AnsiOutput.java b/spring-boot/src/main/java/org/springframework/boot/ansi/AnsiOutput.java index a35441b37a..f5a73df1ab 100644 --- a/spring-boot/src/main/java/org/springframework/boot/ansi/AnsiOutput.java +++ b/spring-boot/src/main/java/org/springframework/boot/ansi/AnsiOutput.java @@ -110,19 +110,15 @@ public abstract class AnsiOutput { private static boolean isEnabled() { if (enabled == Enabled.DETECT) { - return detectIfAnsiCapable(); + if (ansiCapable == null) { + ansiCapable = detectIfAnsiCapable(); + } + return ansiCapable; } return enabled == Enabled.ALWAYS; } private static boolean detectIfAnsiCapable() { - if (ansiCapable == null) { - ansiCapable = doDetectIfAnsiCapable(); - } - return ansiCapable; - } - - private static boolean doDetectIfAnsiCapable() { try { if (System.console() == null) { return false;