Detect default value from factory method

If a field is initialized via a factory method taking a single argument,
we can relatively safely consider that said argument is a good
representation of its default value. This is typically the case for
Charset or MimeType instances.

We now make sure to detect such use case (i.e. method argument with a
single argument).

Closes gh-3482
This commit is contained in:
Stephane Nicoll
2015-07-14 14:55:40 +02:00
parent 5e4a450030
commit c4c24b1f44
4 changed files with 43 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
@@ -74,6 +74,10 @@ public abstract class AbstractFieldValuesProcessorTests {
assertThat(values.get("integerObject"), equalToObject(3));
assertThat(values.get("integerObjectNone"), nullValue());
assertThat(values.get("integerObjectConst"), equalToObject(4));
assertThat(values.get("charset"), equalToObject("US-ASCII"));
assertThat(values.get("charsetConst"), equalToObject("UTF-8"));
assertThat(values.get("mimeType"), equalToObject("text/html"));
assertThat(values.get("mimeTypeConst"), equalToObject("text/plain"));
assertThat(values.get("object"), equalToObject(123));
assertThat(values.get("objectNone"), nullValue());
assertThat(values.get("objectConst"), equalToObject("c"));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
@@ -16,7 +16,10 @@
package org.springframework.boot.configurationsample.fieldvalues;
import java.nio.charset.Charset;
import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.util.MimeType;
/**
* Sample object containing fields with initial values.
@@ -38,6 +41,10 @@ public class FieldValues {
private static final Integer INTEGER_OBJ_CONST = 4;
private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private static final MimeType DEFAULT_MIME_TYPE = MimeType.valueOf("text/plain");
private static final String[] STRING_ARRAY_CONST = new String[] { "OK", "KO" };
private String string = "1";
@@ -70,6 +77,14 @@ public class FieldValues {
private Integer integerObjectConst = INTEGER_OBJ_CONST;
private Charset charset = Charset.forName("US-ASCII");
private Charset charsetConst = DEFAULT_CHARSET;
private MimeType mimeType = MimeType.valueOf("text/html");
private MimeType mimeTypeConst = DEFAULT_MIME_TYPE;
private Object object = 123;
private Object objectNone;