diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/ExpressionTree.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/ExpressionTree.java index 7ed1bbb63a1..69ddf5d17d1 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/ExpressionTree.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/ExpressionTree.java @@ -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; * 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 { 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 { 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 getArrayExpression() throws Exception { if (this.newArrayTreeType.isAssignableFrom(getInstance().getClass())) { List elements = (List) this.arrayValueMethod.invoke(getInstance()); diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/JavaCompilerFieldValuesParser.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/JavaCompilerFieldValuesParser.java index 818e9341136..0a66edc6acd 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/JavaCompilerFieldValuesParser.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/JavaCompilerFieldValuesParser.java @@ -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 * {@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 { if (literalValue != null) { return literalValue; } + Object factoryValue = expression.getFactoryValue(); + if (factoryValue != null) { + return factoryValue; + } List arrayValues = expression.getArrayExpression(); if (arrayValues != null) { Object[] result = new Object[arrayValues.size()]; diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/fieldvalues/AbstractFieldValuesProcessorTests.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/fieldvalues/AbstractFieldValuesProcessorTests.java index d571e1746ba..8cb0487e3f9 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/fieldvalues/AbstractFieldValuesProcessorTests.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/fieldvalues/AbstractFieldValuesProcessorTests.java @@ -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")); diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/fieldvalues/FieldValues.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/fieldvalues/FieldValues.java index 32e682cc96e..49357f6f3aa 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/fieldvalues/FieldValues.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/fieldvalues/FieldValues.java @@ -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;