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.
@@ -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<? extends ExpressionTree> getArrayExpression() throws Exception {
if (this.newArrayTreeType.isAssignableFrom(getInstance().getClass())) {
List<?> elements = (List<?>) this.arrayValueMethod.invoke(getInstance());

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.
@@ -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<? extends ExpressionTree> arrayValues = expression.getArrayExpression();
if (arrayValues != null) {
Object[] result = new Object[arrayValues.size()];