Browse Source

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
pull/3497/head
Stephane Nicoll 11 years ago
parent
commit
c4c24b1f44
  1. 17
      spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/ExpressionTree.java
  2. 7
      spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/JavaCompilerFieldValuesParser.java
  3. 6
      spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/fieldvalues/AbstractFieldValuesProcessorTests.java
  4. 17
      spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/fieldvalues/FieldValues.java

17
spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/ExpressionTree.java

@ -1,5 +1,5 @@ @@ -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.
@ -24,6 +24,7 @@ import java.util.List; @@ -24,6 +24,7 @@ import java.util.List;
* Reflection based access to {@code com.sun.source.tree.ExpressionTree}.
*
* @author Phillip Webb
* @author Stephane Nicoll
* @since 1.2.0
*/
class ExpressionTree extends ReflectionWrapper {
@ -32,6 +33,10 @@ class ExpressionTree extends ReflectionWrapper { @@ -32,6 +33,10 @@ class ExpressionTree extends ReflectionWrapper {
private final Method literalValueMethod = findMethod(this.literalTreeType, "getValue");
private final Class<?> methodInvocationTreeType = findClass("com.sun.source.tree.MethodInvocationTree");
private final Method methodInvocationArgumentsMethod = findMethod(this.methodInvocationTreeType, "getArguments");
private final Class<?> newArrayTreeType = findClass("com.sun.source.tree.NewArrayTree");
private final Method arrayValueMethod = findMethod(this.newArrayTreeType,
@ -52,6 +57,16 @@ class ExpressionTree extends ReflectionWrapper { @@ -52,6 +57,16 @@ class ExpressionTree extends ReflectionWrapper {
return null;
}
public Object getFactoryValue() throws Exception {
if (this.methodInvocationTreeType.isAssignableFrom(getInstance().getClass())) {
List<?> arguments = (List<?>) this.methodInvocationArgumentsMethod.invoke(getInstance());
if (arguments.size() == 1) {
return new ExpressionTree(arguments.get(0)).getLiteralValue();
}
}
return null;
}
public List<? extends ExpressionTree> getArrayExpression() throws Exception {
if (this.newArrayTreeType.isAssignableFrom(getInstance().getClass())) {
List<?> elements = (List<?>) this.arrayValueMethod.invoke(getInstance());

7
spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/JavaCompilerFieldValuesParser.java

@ -1,5 +1,5 @@ @@ -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.
@ -32,6 +32,7 @@ import org.springframework.boot.configurationprocessor.fieldvalues.FieldValuesPa @@ -32,6 +32,7 @@ import org.springframework.boot.configurationprocessor.fieldvalues.FieldValuesPa
* {@link FieldValuesParser} implementation for the standard Java compiler.
*
* @author Phillip Webb
* @author Stephane Nicoll
* @since 1.2.0
*/
public class JavaCompilerFieldValuesParser implements FieldValuesParser {
@ -121,6 +122,10 @@ public class JavaCompilerFieldValuesParser implements FieldValuesParser { @@ -121,6 +122,10 @@ public class JavaCompilerFieldValuesParser implements FieldValuesParser {
if (literalValue != null) {
return literalValue;
}
Object factoryValue = expression.getFactoryValue();
if (factoryValue != null) {
return factoryValue;
}
List<? extends ExpressionTree> arrayValues = expression.getArrayExpression();
if (arrayValues != null) {
Object[] result = new Object[arrayValues.size()];

6
spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/fieldvalues/AbstractFieldValuesProcessorTests.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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"));

17
spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/fieldvalues/FieldValues.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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 { @@ -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 { @@ -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;

Loading…
Cancel
Save