Relocate projects to spring-boot-project

Move projects to better reflect the way that Spring Boot is released.

The following projects are under `spring-boot-project`:

  - `spring-boot`
  - `spring-boot-autoconfigure`
  - `spring-boot-tools`
  - `spring-boot-starters`
  - `spring-boot-actuator`
  - `spring-boot-actuator-autoconfigure`
  - `spring-boot-test`
  - `spring-boot-test-autoconfigure`
  - `spring-boot-devtools`
  - `spring-boot-cli`
  - `spring-boot-docs`

See gh-9316
This commit is contained in:
Phillip Webb
2017-09-19 14:29:46 -07:00
parent 0419d42b7c
commit 0ba4830b4f
4023 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,579 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic.Kind;
import org.springframework.boot.configurationprocessor.fieldvalues.FieldValuesParser;
import org.springframework.boot.configurationprocessor.fieldvalues.javac.JavaCompilerFieldValuesParser;
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
import org.springframework.boot.configurationprocessor.metadata.InvalidConfigurationMetadataException;
import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
/**
* Annotation {@link Processor} that writes meta-data file for
* {@code @ConfigurationProperties}.
*
* @author Stephane Nicoll
* @author Phillip Webb
* @author Kris De Volder
* @since 1.2.0
*/
@SupportedAnnotationTypes({ "*" })
public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor {
static final String CONFIGURATION_PROPERTIES_ANNOTATION = "org.springframework.boot."
+ "context.properties.ConfigurationProperties";
static final String NESTED_CONFIGURATION_PROPERTY_ANNOTATION = "org.springframework.boot."
+ "context.properties.NestedConfigurationProperty";
static final String DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION = "org.springframework.boot."
+ "context.properties.DeprecatedConfigurationProperty";
static final String ENDPOINT_ANNOTATION = "org.springframework.boot.actuate."
+ "endpoint.annotation.Endpoint";
static final String LOMBOK_DATA_ANNOTATION = "lombok.Data";
static final String LOMBOK_GETTER_ANNOTATION = "lombok.Getter";
static final String LOMBOK_SETTER_ANNOTATION = "lombok.Setter";
private MetadataStore metadataStore;
private MetadataCollector metadataCollector;
private TypeUtils typeUtils;
private FieldValuesParser fieldValuesParser;
private TypeExcludeFilter typeExcludeFilter = new TypeExcludeFilter();
protected String configurationPropertiesAnnotation() {
return CONFIGURATION_PROPERTIES_ANNOTATION;
}
protected String nestedConfigurationPropertyAnnotation() {
return NESTED_CONFIGURATION_PROPERTY_ANNOTATION;
}
protected String deprecatedConfigurationPropertyAnnotation() {
return DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION;
}
protected String endpointAnnotation() {
return ENDPOINT_ANNOTATION;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
@Override
public synchronized void init(ProcessingEnvironment env) {
super.init(env);
this.typeUtils = new TypeUtils(env);
this.metadataStore = new MetadataStore(env);
this.metadataCollector = new MetadataCollector(env,
this.metadataStore.readMetadata());
try {
this.fieldValuesParser = new JavaCompilerFieldValuesParser(env);
}
catch (Throwable ex) {
this.fieldValuesParser = FieldValuesParser.NONE;
logWarning("Field value processing of @ConfigurationProperty meta-data is "
+ "not supported");
}
}
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
this.metadataCollector.processing(roundEnv);
Elements elementUtils = this.processingEnv.getElementUtils();
TypeElement annotationType = elementUtils
.getTypeElement(configurationPropertiesAnnotation());
if (annotationType != null) { // Is @ConfigurationProperties available
for (Element element : roundEnv.getElementsAnnotatedWith(annotationType)) {
processElement(element);
}
}
TypeElement endpointType = elementUtils.getTypeElement(endpointAnnotation());
if (endpointType != null) { // Is @Endpoint available
for (Element element : roundEnv.getElementsAnnotatedWith(endpointType)) {
processEndpoint(element);
}
}
if (roundEnv.processingOver()) {
try {
writeMetaData();
}
catch (Exception ex) {
throw new IllegalStateException("Failed to write metadata", ex);
}
}
return false;
}
private void processElement(Element element) {
try {
AnnotationMirror annotation = getAnnotation(element,
configurationPropertiesAnnotation());
if (annotation != null) {
String prefix = getPrefix(annotation);
if (element instanceof TypeElement) {
processAnnotatedTypeElement(prefix, (TypeElement) element);
}
else if (element instanceof ExecutableElement) {
processExecutableElement(prefix, (ExecutableElement) element);
}
}
}
catch (Exception ex) {
throw new IllegalStateException(
"Error processing configuration meta-data on " + element, ex);
}
}
private void processAnnotatedTypeElement(String prefix, TypeElement element) {
String type = this.typeUtils.getQualifiedName(element);
this.metadataCollector.add(ItemMetadata.newGroup(prefix, type, type, null));
processTypeElement(prefix, element, null);
}
private void processExecutableElement(String prefix, ExecutableElement element) {
if (element.getModifiers().contains(Modifier.PUBLIC)
&& (TypeKind.VOID != element.getReturnType().getKind())) {
Element returns = this.processingEnv.getTypeUtils()
.asElement(element.getReturnType());
if (returns instanceof TypeElement) {
ItemMetadata group = ItemMetadata.newGroup(prefix,
this.typeUtils.getQualifiedName(returns),
this.typeUtils.getQualifiedName(element.getEnclosingElement()),
element.toString());
if (this.metadataCollector.hasSimilarGroup(group)) {
this.processingEnv.getMessager().printMessage(Kind.ERROR,
"Duplicate `@ConfigurationProperties` definition for prefix '"
+ prefix + "'",
element);
}
else {
this.metadataCollector.add(group);
processTypeElement(prefix, (TypeElement) returns, element);
}
}
}
}
private void processTypeElement(String prefix, TypeElement element,
ExecutableElement source) {
TypeElementMembers members = new TypeElementMembers(this.processingEnv,
this.fieldValuesParser, element);
Map<String, Object> fieldValues = members.getFieldValues();
processSimpleTypes(prefix, element, source, members, fieldValues);
processSimpleLombokTypes(prefix, element, source, members, fieldValues);
processNestedTypes(prefix, element, source, members);
processNestedLombokTypes(prefix, element, source, members);
}
private void processSimpleTypes(String prefix, TypeElement element,
ExecutableElement source, TypeElementMembers members,
Map<String, Object> fieldValues) {
for (Map.Entry<String, ExecutableElement> entry : members.getPublicGetters()
.entrySet()) {
String name = entry.getKey();
ExecutableElement getter = entry.getValue();
TypeMirror returnType = getter.getReturnType();
ExecutableElement setter = members.getPublicSetter(name, returnType);
VariableElement field = members.getFields().get(name);
Element returnTypeElement = this.processingEnv.getTypeUtils()
.asElement(returnType);
boolean isExcluded = this.typeExcludeFilter.isExcluded(returnType);
boolean isNested = isNested(returnTypeElement, field, element);
boolean isCollection = this.typeUtils.isCollectionOrMap(returnType);
if (!isExcluded && !isNested && (setter != null || isCollection)) {
String dataType = this.typeUtils.getType(returnType);
String sourceType = this.typeUtils.getQualifiedName(element);
String description = this.typeUtils.getJavaDoc(field);
Object defaultValue = fieldValues.get(name);
boolean deprecated = isDeprecated(getter) || isDeprecated(setter)
|| isDeprecated(source);
this.metadataCollector.add(ItemMetadata.newProperty(prefix, name,
dataType, sourceType, null, description, defaultValue,
(deprecated ? getItemDeprecation(getter) : null)));
}
}
}
private ItemDeprecation getItemDeprecation(ExecutableElement getter) {
AnnotationMirror annotation = getAnnotation(getter,
deprecatedConfigurationPropertyAnnotation());
String reason = null;
String replacement = null;
if (annotation != null) {
Map<String, Object> elementValues = getAnnotationElementValues(annotation);
reason = (String) elementValues.get("reason");
replacement = (String) elementValues.get("replacement");
}
return new ItemDeprecation(("".equals(reason) ? null : reason),
("".equals(replacement) ? null : replacement));
}
private void processSimpleLombokTypes(String prefix, TypeElement element,
ExecutableElement source, TypeElementMembers members,
Map<String, Object> fieldValues) {
for (Map.Entry<String, VariableElement> entry : members.getFields().entrySet()) {
String name = entry.getKey();
VariableElement field = entry.getValue();
if (!isLombokField(field, element)) {
continue;
}
TypeMirror returnType = field.asType();
Element returnTypeElement = this.processingEnv.getTypeUtils()
.asElement(returnType);
boolean isExcluded = this.typeExcludeFilter.isExcluded(returnType);
boolean isNested = isNested(returnTypeElement, field, element);
boolean isCollection = this.typeUtils.isCollectionOrMap(returnType);
boolean hasSetter = hasLombokSetter(field, element);
if (!isExcluded && !isNested && (hasSetter || isCollection)) {
String dataType = this.typeUtils.getType(returnType);
String sourceType = this.typeUtils.getQualifiedName(element);
String description = this.typeUtils.getJavaDoc(field);
Object defaultValue = fieldValues.get(name);
boolean deprecated = isDeprecated(field) || isDeprecated(source);
this.metadataCollector.add(ItemMetadata.newProperty(prefix, name,
dataType, sourceType, null, description, defaultValue,
(deprecated ? new ItemDeprecation() : null)));
}
}
}
private void processNestedTypes(String prefix, TypeElement element,
ExecutableElement source, TypeElementMembers members) {
for (Map.Entry<String, ExecutableElement> entry : members.getPublicGetters()
.entrySet()) {
String name = entry.getKey();
ExecutableElement getter = entry.getValue();
VariableElement field = members.getFields().get(name);
processNestedType(prefix, element, source, name, getter, field,
getter.getReturnType());
}
}
private void processNestedLombokTypes(String prefix, TypeElement element,
ExecutableElement source, TypeElementMembers members) {
for (Map.Entry<String, VariableElement> entry : members.getFields().entrySet()) {
String name = entry.getKey();
VariableElement field = entry.getValue();
if (isLombokField(field, element)) {
ExecutableElement getter = members.getPublicGetter(name, field.asType());
processNestedType(prefix, element, source, name, getter, field,
field.asType());
}
}
}
private boolean isLombokField(VariableElement field, TypeElement element) {
return hasAnnotation(field, LOMBOK_GETTER_ANNOTATION)
|| hasAnnotation(element, LOMBOK_GETTER_ANNOTATION)
|| hasAnnotation(element, LOMBOK_DATA_ANNOTATION);
}
private boolean hasLombokSetter(VariableElement field, TypeElement element) {
return !field.getModifiers().contains(Modifier.FINAL)
&& (hasAnnotation(field, LOMBOK_SETTER_ANNOTATION)
|| hasAnnotation(element, LOMBOK_SETTER_ANNOTATION)
|| hasAnnotation(element, LOMBOK_DATA_ANNOTATION));
}
private void processNestedType(String prefix, TypeElement element,
ExecutableElement source, 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 instanceof TypeElement && annotation == null && isNested) {
String nestedPrefix = ConfigurationMetadata.nestedPrefix(prefix, name);
this.metadataCollector.add(ItemMetadata.newGroup(nestedPrefix,
this.typeUtils.getQualifiedName(returnElement),
this.typeUtils.getQualifiedName(element),
(getter == null ? null : getter.toString())));
processTypeElement(nestedPrefix, (TypeElement) returnElement, source);
}
}
private void processEndpoint(Element element) {
try {
AnnotationMirror annotation = getAnnotation(element, endpointAnnotation());
if (element instanceof TypeElement) {
processEndpoint(annotation, (TypeElement) element);
}
}
catch (Exception ex) {
throw new IllegalStateException(
"Error processing configuration meta-data on " + element, ex);
}
}
private void processEndpoint(AnnotationMirror annotation, TypeElement element) {
Map<String, Object> elementValues = getAnnotationElementValues(annotation);
String endpointId = (String) elementValues.get("id");
if (endpointId == null || "".equals(endpointId)) {
return; // Can't process that endpoint
}
Boolean enabledByDefault = determineEnabledByDefault(
elementValues.get("defaultEnablement"));
String type = this.typeUtils.getQualifiedName(element);
this.metadataCollector
.add(ItemMetadata.newGroup(endpointKey(endpointId), type, type, null));
this.metadataCollector.add(ItemMetadata.newProperty(endpointKey(endpointId),
"enabled", Boolean.class.getName(), type, null,
String.format("Enable the %s endpoint.", endpointId), enabledByDefault,
null));
this.metadataCollector.add(ItemMetadata.newProperty(endpointKey(endpointId),
"cache.time-to-live", Long.class.getName(), type, null,
"Maximum time in milliseconds that a response can be cached.", 0, null));
EndpointExposure endpointTypes = EndpointExposure
.parse(elementValues.get("exposure"));
if (endpointTypes.hasJmx()) {
this.metadataCollector.add(ItemMetadata.newProperty(
endpointKey(endpointId + ".jmx"), "enabled", Boolean.class.getName(),
type, null,
String.format("Expose the %s endpoint as a JMX MBean.", endpointId),
enabledByDefault, null));
}
if (endpointTypes.hasWeb()) {
this.metadataCollector.add(ItemMetadata.newProperty(
endpointKey(endpointId + ".web"), "enabled", Boolean.class.getName(),
type, null, String.format("Expose the %s endpoint as a Web endpoint.",
endpointId),
enabledByDefault, null));
}
}
private Boolean determineEnabledByDefault(Object defaultEnablement) {
if (defaultEnablement != null) {
String value = String.valueOf(defaultEnablement);
if ("ENABLED".equals(value)) {
return true;
}
if ("DISABLED".equals(value)) {
return false;
}
}
return null;
}
private String endpointKey(String suffix) {
return "endpoints." + suffix;
}
private boolean isNested(Element returnType, VariableElement field,
TypeElement element) {
if (hasAnnotation(field, nestedConfigurationPropertyAnnotation())) {
return true;
}
return (isParentTheSame(returnType, element))
&& returnType.getKind() != ElementKind.ENUM;
}
private boolean isParentTheSame(Element returnType, TypeElement element) {
if (returnType == null || element == null) {
return false;
}
return getTopLevelType(returnType).equals(getTopLevelType(element));
}
private Element getTopLevelType(Element element) {
if (!(element.getEnclosingElement() instanceof TypeElement)) {
return element;
}
return getTopLevelType(element.getEnclosingElement());
}
private boolean isDeprecated(Element element) {
if (isElementDeprecated(element)) {
return true;
}
if (element instanceof VariableElement || element instanceof ExecutableElement) {
return isElementDeprecated(element.getEnclosingElement());
}
return false;
}
private boolean isElementDeprecated(Element element) {
return hasAnnotation(element, "java.lang.Deprecated")
|| hasAnnotation(element, deprecatedConfigurationPropertyAnnotation());
}
private boolean hasAnnotation(Element element, String type) {
return getAnnotation(element, type) != null;
}
private AnnotationMirror getAnnotation(Element element, String type) {
if (element != null) {
for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
if (type.equals(annotation.getAnnotationType().toString())) {
return annotation;
}
}
}
return null;
}
private String getPrefix(AnnotationMirror annotation) {
Map<String, Object> elementValues = getAnnotationElementValues(annotation);
Object prefix = elementValues.get("prefix");
if (prefix != null && !"".equals(prefix)) {
return (String) prefix;
}
Object value = elementValues.get("value");
if (value != null && !"".equals(value)) {
return (String) value;
}
return null;
}
private Map<String, Object> getAnnotationElementValues(AnnotationMirror annotation) {
Map<String, Object> values = new LinkedHashMap<>();
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotation
.getElementValues().entrySet()) {
values.put(entry.getKey().getSimpleName().toString(),
entry.getValue().getValue());
}
return values;
}
protected ConfigurationMetadata writeMetaData() throws Exception {
ConfigurationMetadata metadata = this.metadataCollector.getMetadata();
metadata = mergeAdditionalMetadata(metadata);
if (!metadata.getItems().isEmpty()) {
this.metadataStore.writeMetadata(metadata);
return metadata;
}
return null;
}
private ConfigurationMetadata mergeAdditionalMetadata(
ConfigurationMetadata metadata) {
try {
ConfigurationMetadata merged = new ConfigurationMetadata(metadata);
merged.merge(this.metadataStore.readAdditionalMetadata());
return merged;
}
catch (FileNotFoundException ex) {
// No additional metadata
}
catch (InvalidConfigurationMetadataException ex) {
log(ex.getKind(), ex.getMessage());
}
catch (Exception ex) {
logWarning("Unable to merge additional metadata");
logWarning(getStackTrace(ex));
}
return metadata;
}
private String getStackTrace(Exception ex) {
StringWriter writer = new StringWriter();
ex.printStackTrace(new PrintWriter(writer, true));
return writer.toString();
}
private void logWarning(String msg) {
log(Kind.WARNING, msg);
}
private void log(Kind kind, String msg) {
this.processingEnv.getMessager().printMessage(kind, msg);
}
private static class EndpointExposure {
private static final List<String> ALL = Arrays.asList("JMX", "WEB");
private final List<String> types;
EndpointExposure(List<String> types) {
this.types = types;
}
static EndpointExposure parse(Object exposureAttribute) {
List<AnnotationValue> values = asAnnotationValues(exposureAttribute);
if (values.isEmpty()) {
return new EndpointExposure(ALL);
}
return new EndpointExposure(
values.stream().map(EndpointExposure::getValueAttribute)
.collect(Collectors.toList()));
}
@SuppressWarnings("unchecked")
private static List<AnnotationValue> asAnnotationValues(Object typesAttribute) {
if (!(typesAttribute instanceof List)) {
return Collections.emptyList();
}
return (List<AnnotationValue>) typesAttribute;
}
private static String getValueAttribute(AnnotationValue value) {
return ((VariableElement) value.getValue()).getSimpleName().toString();
}
public boolean hasJmx() {
return this.types.contains("JMX");
}
public boolean hasWeb() {
return this.types.contains("WEB");
}
}
}

View File

@@ -0,0 +1,125 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
/**
* Used by {@link ConfigurationMetadataAnnotationProcessor} to collect
* {@link ConfigurationMetadata}.
*
* @author Andy Wilkinson
* @author Kris De Volder
* @since 1.2.2
*/
public class MetadataCollector {
private final Set<ItemMetadata> metadataItems = new LinkedHashSet<>();
private final ProcessingEnvironment processingEnvironment;
private final ConfigurationMetadata previousMetadata;
private final TypeUtils typeUtils;
private final Set<String> processedSourceTypes = new HashSet<>();
/**
* Creates a new {@code MetadataProcessor} instance.
* @param processingEnvironment The processing environment of the build
* @param previousMetadata Any previous metadata or {@code null}
*/
public MetadataCollector(ProcessingEnvironment processingEnvironment,
ConfigurationMetadata previousMetadata) {
this.processingEnvironment = processingEnvironment;
this.previousMetadata = previousMetadata;
this.typeUtils = new TypeUtils(processingEnvironment);
}
public void processing(RoundEnvironment roundEnv) {
for (Element element : roundEnv.getRootElements()) {
markAsProcessed(element);
}
}
private void markAsProcessed(Element element) {
if (element instanceof TypeElement) {
this.processedSourceTypes.add(this.typeUtils.getQualifiedName(element));
}
}
public void add(ItemMetadata metadata) {
this.metadataItems.add(metadata);
}
public boolean hasSimilarGroup(ItemMetadata metadata) {
if (!metadata.isOfItemType(ItemMetadata.ItemType.GROUP)) {
throw new IllegalStateException("item " + metadata + " must be a group");
}
for (ItemMetadata existing : this.metadataItems) {
if (existing.isOfItemType(ItemMetadata.ItemType.GROUP)
&& existing.getName().equals(metadata.getName())
&& existing.getType().equals(metadata.getType())) {
return true;
}
}
return false;
}
public ConfigurationMetadata getMetadata() {
ConfigurationMetadata metadata = new ConfigurationMetadata();
for (ItemMetadata item : this.metadataItems) {
metadata.add(item);
}
if (this.previousMetadata != null) {
List<ItemMetadata> items = this.previousMetadata.getItems();
for (ItemMetadata item : items) {
if (shouldBeMerged(item)) {
metadata.add(item);
}
}
}
return metadata;
}
private boolean shouldBeMerged(ItemMetadata itemMetadata) {
String sourceType = itemMetadata.getSourceType();
return (sourceType != null && !deletedInCurrentBuild(sourceType)
&& !processedInCurrentBuild(sourceType));
}
private boolean deletedInCurrentBuild(String sourceType) {
return this.processingEnvironment.getElementUtils()
.getTypeElement(sourceType) == null;
}
private boolean processedInCurrentBuild(String sourceType) {
return this.processedSourceTypes.contains(sourceType);
}
}

View File

@@ -0,0 +1,140 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
import org.springframework.boot.configurationprocessor.metadata.InvalidConfigurationMetadataException;
import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller;
/**
* A {@code MetadataStore} is responsible for the storage of metadata on the filesystem.
*
* @author Andy Wilkinson
* @since 1.2.2
*/
public class MetadataStore {
static final String METADATA_PATH = "META-INF/spring-configuration-metadata.json";
private static final String ADDITIONAL_METADATA_PATH = "META-INF/additional-spring-configuration-metadata.json";
private static final String RESOURCES_FOLDER = "resources";
private static final String CLASSES_FOLDER = "classes";
private final ProcessingEnvironment environment;
public MetadataStore(ProcessingEnvironment environment) {
this.environment = environment;
}
public ConfigurationMetadata readMetadata() {
try {
return readMetadata(getMetadataResource().openInputStream());
}
catch (IOException ex) {
return null;
}
}
public void writeMetadata(ConfigurationMetadata metadata) throws IOException {
if (!metadata.getItems().isEmpty()) {
try (OutputStream outputStream = createMetadataResource()
.openOutputStream()) {
new JsonMarshaller().write(metadata, outputStream);
}
}
}
public ConfigurationMetadata readAdditionalMetadata() throws IOException {
return readMetadata(getAdditionalMetadataStream());
}
private ConfigurationMetadata readMetadata(InputStream in) throws IOException {
try {
return new JsonMarshaller().read(in);
}
catch (IOException ex) {
return null;
}
catch (Exception ex) {
throw new InvalidConfigurationMetadataException(
"Invalid additional meta-data in '" + METADATA_PATH + "': "
+ ex.getMessage(),
Diagnostic.Kind.ERROR);
}
finally {
in.close();
}
}
private FileObject getMetadataResource() throws IOException {
FileObject resource = this.environment.getFiler()
.getResource(StandardLocation.CLASS_OUTPUT, "", METADATA_PATH);
return resource;
}
private FileObject createMetadataResource() throws IOException {
FileObject resource = this.environment.getFiler()
.createResource(StandardLocation.CLASS_OUTPUT, "", METADATA_PATH);
return resource;
}
private InputStream getAdditionalMetadataStream() throws IOException {
// Most build systems will have copied the file to the class output location
FileObject fileObject = this.environment.getFiler()
.getResource(StandardLocation.CLASS_OUTPUT, "", ADDITIONAL_METADATA_PATH);
File file = locateAdditionalMetadataFile(new File(fileObject.toUri()));
return (file.exists() ? new FileInputStream(file)
: fileObject.toUri().toURL().openStream());
}
File locateAdditionalMetadataFile(File standardLocation) throws IOException {
if (standardLocation.exists()) {
return standardLocation;
}
return new File(locateGradleResourcesFolder(standardLocation),
ADDITIONAL_METADATA_PATH);
}
private File locateGradleResourcesFolder(File standardAdditionalMetadataLocation)
throws FileNotFoundException {
String path = standardAdditionalMetadataLocation.getPath();
int index = path.lastIndexOf(CLASSES_FOLDER);
if (index < 0) {
throw new FileNotFoundException();
}
String buildFolderPath = path.substring(0, index);
File classOutputLocation = standardAdditionalMetadataLocation.getParentFile()
.getParentFile();
return new File(buildFolderPath,
RESOURCES_FOLDER + '/' + classOutputLocation.getName());
}
}

View File

@@ -0,0 +1,209 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
import org.springframework.boot.configurationprocessor.fieldvalues.FieldValuesParser;
/**
* Provides access to relevant {@link TypeElement} members.
*
* @author Stephane Nicoll
* @author Phillip Webb
* @since 1.2.0
*/
class TypeElementMembers {
private static final String OBJECT_CLASS_NAME = Object.class.getName();
private final ProcessingEnvironment env;
private final TypeUtils typeUtils;
private final Map<String, VariableElement> fields = new LinkedHashMap<>();
private final Map<String, ExecutableElement> publicGetters = new LinkedHashMap<>();
private final Map<String, List<ExecutableElement>> publicSetters = new LinkedHashMap<>();
private final Map<String, Object> fieldValues = new LinkedHashMap<>();
private final FieldValuesParser fieldValuesParser;
TypeElementMembers(ProcessingEnvironment env, FieldValuesParser fieldValuesParser,
TypeElement element) {
this.env = env;
this.typeUtils = new TypeUtils(this.env);
this.fieldValuesParser = fieldValuesParser;
process(element);
}
private void process(TypeElement element) {
for (ExecutableElement method : ElementFilter
.methodsIn(element.getEnclosedElements())) {
processMethod(method);
}
for (VariableElement field : ElementFilter
.fieldsIn(element.getEnclosedElements())) {
processField(field);
}
try {
Map<String, Object> fieldValues = this.fieldValuesParser
.getFieldValues(element);
for (Map.Entry<String, Object> entry : fieldValues.entrySet()) {
if (!this.fieldValues.containsKey(entry.getKey())) {
this.fieldValues.put(entry.getKey(), entry.getValue());
}
}
}
catch (Exception ex) {
// continue
}
Element superType = this.env.getTypeUtils().asElement(element.getSuperclass());
if (superType != null && superType instanceof TypeElement
&& !OBJECT_CLASS_NAME.equals(superType.toString())) {
process((TypeElement) superType);
}
}
private void processMethod(ExecutableElement method) {
if (method.getModifiers().contains(Modifier.PUBLIC)) {
String name = method.getSimpleName().toString();
if (isGetter(method) && !this.publicGetters.containsKey(name)) {
this.publicGetters.put(getAccessorName(name), method);
}
else if (isSetter(method)) {
String propertyName = getAccessorName(name);
List<ExecutableElement> matchingSetters = this.publicSetters
.get(propertyName);
if (matchingSetters == null) {
matchingSetters = new ArrayList<>();
this.publicSetters.put(propertyName, matchingSetters);
}
TypeMirror paramType = method.getParameters().get(0).asType();
if (getMatchingSetter(matchingSetters, paramType) == null) {
matchingSetters.add(method);
}
}
}
}
private ExecutableElement getMatchingSetter(List<ExecutableElement> candidates,
TypeMirror type) {
for (ExecutableElement candidate : candidates) {
TypeMirror paramType = candidate.getParameters().get(0).asType();
if (this.env.getTypeUtils().isSameType(paramType, type)) {
return candidate;
}
}
return null;
}
private boolean isGetter(ExecutableElement method) {
String name = method.getSimpleName().toString();
return ((name.startsWith("get") && name.length() > 3)
|| (name.startsWith("is") && name.length() > 2))
&& method.getParameters().isEmpty()
&& (TypeKind.VOID != method.getReturnType().getKind());
}
private boolean isSetter(ExecutableElement method) {
final String name = method.getSimpleName().toString();
return (name.startsWith("set") && name.length() > 3
&& method.getParameters().size() == 1 && isSetterReturnType(method));
}
private boolean isSetterReturnType(ExecutableElement method) {
TypeMirror returnType = method.getReturnType();
return (TypeKind.VOID == returnType.getKind() || this.env.getTypeUtils()
.isSameType(method.getEnclosingElement().asType(), returnType));
}
private String getAccessorName(String methodName) {
String name = methodName.startsWith("is") ? methodName.substring(2)
: methodName.substring(3);
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
return name;
}
private void processField(VariableElement field) {
String name = field.getSimpleName().toString();
if (!this.fields.containsKey(name)) {
this.fields.put(name, field);
}
}
public Map<String, VariableElement> getFields() {
return Collections.unmodifiableMap(this.fields);
}
public Map<String, ExecutableElement> getPublicGetters() {
return Collections.unmodifiableMap(this.publicGetters);
}
public ExecutableElement getPublicGetter(String name, TypeMirror type) {
ExecutableElement candidate = this.publicGetters.get(name);
if (candidate != null) {
TypeMirror returnType = candidate.getReturnType();
if (this.env.getTypeUtils().isSameType(returnType, type)) {
return candidate;
}
TypeMirror alternative = this.typeUtils.getWrapperOrPrimitiveFor(type);
if (alternative != null
&& this.env.getTypeUtils().isSameType(returnType, alternative)) {
return candidate;
}
}
return null;
}
public ExecutableElement getPublicSetter(String name, TypeMirror type) {
List<ExecutableElement> candidates = this.publicSetters.get(name);
if (candidates != null) {
ExecutableElement matching = getMatchingSetter(candidates, type);
if (matching != null) {
return matching;
}
TypeMirror alternative = this.typeUtils.getWrapperOrPrimitiveFor(type);
if (alternative != null) {
return getMatchingSetter(candidates, alternative);
}
}
return null;
}
public Map<String, Object> getFieldValues() {
return Collections.unmodifiableMap(this.fieldValues);
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.util.HashSet;
import java.util.Set;
import javax.lang.model.type.TypeMirror;
/**
* Filter to exclude elements that don't make sense to process.
*
* @author Stephane Nicoll
* @author Andy Wilkinson
* @since 1.2.0
*/
class TypeExcludeFilter {
private final Set<String> excludes = new HashSet<>();
TypeExcludeFilter() {
add("com.zaxxer.hikari.IConnectionCustomizer");
add("groovy.text.markup.MarkupTemplateEngine");
add("java.io.Writer");
add("java.io.PrintWriter");
add("java.lang.ClassLoader");
add("java.util.concurrent.ThreadFactory");
add("javax.jms.XAConnectionFactory");
add("javax.sql.DataSource");
add("javax.sql.XADataSource");
add("org.apache.tomcat.jdbc.pool.PoolConfiguration");
add("org.apache.tomcat.jdbc.pool.Validator");
add("org.flywaydb.core.api.callback.FlywayCallback");
add("org.flywaydb.core.api.resolver.MigrationResolver");
add("org.springframework.http.MediaType");
}
private void add(String className) {
this.excludes.add(className);
}
public boolean isExcluded(TypeMirror type) {
if (type == null) {
return false;
}
String typeName = type.toString();
if (typeName.endsWith("[]")) {
typeName = typeName.substring(0, typeName.length() - 2);
}
return this.excludes.contains(typeName);
}
}

View File

@@ -0,0 +1,197 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Types;
/**
* Type Utilities.
*
* @author Stephane Nicoll
* @author Phillip Webb
* @since 1.2.0
*/
class TypeUtils {
private static final Map<TypeKind, Class<?>> PRIMITIVE_WRAPPERS;
static {
Map<TypeKind, Class<?>> wrappers = new HashMap<>();
wrappers.put(TypeKind.BOOLEAN, Boolean.class);
wrappers.put(TypeKind.BYTE, Byte.class);
wrappers.put(TypeKind.CHAR, Character.class);
wrappers.put(TypeKind.DOUBLE, Double.class);
wrappers.put(TypeKind.FLOAT, Float.class);
wrappers.put(TypeKind.INT, Integer.class);
wrappers.put(TypeKind.LONG, Long.class);
wrappers.put(TypeKind.SHORT, Short.class);
PRIMITIVE_WRAPPERS = Collections.unmodifiableMap(wrappers);
}
private static final Map<String, TypeKind> WRAPPER_TO_PRIMITIVE;
static {
Map<String, TypeKind> primitives = new HashMap<>();
for (Map.Entry<TypeKind, Class<?>> entry : PRIMITIVE_WRAPPERS.entrySet()) {
primitives.put(entry.getValue().getName(), entry.getKey());
}
WRAPPER_TO_PRIMITIVE = primitives;
}
private final ProcessingEnvironment env;
private final TypeMirror collectionType;
private final TypeMirror mapType;
TypeUtils(ProcessingEnvironment env) {
this.env = env;
Types types = env.getTypeUtils();
this.collectionType = getDeclaredType(types, Collection.class, 1);
this.mapType = getDeclaredType(types, Map.class, 2);
}
private TypeMirror getDeclaredType(Types types, Class<?> typeClass,
int numberOfTypeArgs) {
TypeMirror[] typeArgs = new TypeMirror[numberOfTypeArgs];
for (int i = 0; i < typeArgs.length; i++) {
typeArgs[i] = types.getWildcardType(null, null);
}
TypeElement typeElement = this.env.getElementUtils()
.getTypeElement(typeClass.getName());
try {
return types.getDeclaredType(typeElement, typeArgs);
}
catch (IllegalArgumentException ex) {
// Try again without generics for older Java versions
return types.getDeclaredType(typeElement);
}
}
/**
* Return the qualified name of the specified element.
* @param element the element to handle
* @return the fully qualified name of the element, suitable for a call to
* {@link Class#forName(String)}
*/
public String getQualifiedName(Element element) {
if (element == null) {
return null;
}
TypeElement enclosingElement = getEnclosingTypeElement(element.asType());
if (enclosingElement != null) {
return getQualifiedName(enclosingElement) + "$"
+ ((DeclaredType) element.asType()).asElement().getSimpleName()
.toString();
}
if (element instanceof TypeElement) {
return ((TypeElement) element).getQualifiedName().toString();
}
throw new IllegalStateException(
"Could not extract qualified name from " + element);
}
/**
* Return the type of the specified {@link TypeMirror} including all its generic
* information.
* @param type the type to handle
* @return a representation of the type including all its generic information
*/
public String getType(TypeMirror type) {
if (type == null) {
return null;
}
Class<?> wrapper = getWrapperFor(type);
if (wrapper != null) {
return wrapper.getName();
}
TypeElement enclosingElement = getEnclosingTypeElement(type);
if (enclosingElement != null) {
return getQualifiedName(enclosingElement) + "$"
+ ((DeclaredType) type).asElement().getSimpleName().toString();
}
return type.toString();
}
private TypeElement getEnclosingTypeElement(TypeMirror type) {
if (type instanceof DeclaredType) {
DeclaredType declaredType = (DeclaredType) type;
Element enclosingElement = declaredType.asElement().getEnclosingElement();
if (enclosingElement != null && enclosingElement instanceof TypeElement) {
return (TypeElement) enclosingElement;
}
}
return null;
}
public boolean isCollectionOrMap(TypeMirror type) {
return this.env.getTypeUtils().isAssignable(type, this.collectionType)
|| this.env.getTypeUtils().isAssignable(type, this.mapType);
}
public boolean isEnclosedIn(Element candidate, TypeElement element) {
if (candidate == null || element == null) {
return false;
}
if (candidate.equals(element)) {
return true;
}
return isEnclosedIn(candidate.getEnclosingElement(), element);
}
public String getJavaDoc(Element element) {
String javadoc = (element == null ? null
: this.env.getElementUtils().getDocComment(element));
if (javadoc != null) {
javadoc = javadoc.trim();
}
return ("".equals(javadoc) ? null : javadoc);
}
public TypeMirror getWrapperOrPrimitiveFor(TypeMirror typeMirror) {
Class<?> candidate = getWrapperFor(typeMirror);
if (candidate != null) {
return this.env.getElementUtils().getTypeElement(candidate.getName())
.asType();
}
TypeKind primitiveKind = getPrimitiveFor(typeMirror);
if (primitiveKind != null) {
return this.env.getTypeUtils().getPrimitiveType(primitiveKind);
}
return null;
}
private Class<?> getWrapperFor(TypeMirror type) {
return PRIMITIVE_WRAPPERS.get(type.getKind());
}
private TypeKind getPrimitiveFor(TypeMirror type) {
return WRAPPER_TO_PRIMITIVE.get(type.toString());
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.fieldvalues;
import java.util.Collections;
import java.util.Map;
import javax.lang.model.element.TypeElement;
import org.springframework.boot.configurationprocessor.fieldvalues.javac.JavaCompilerFieldValuesParser;
/**
* Parser which can be used to obtain the field values from an {@link TypeElement}.
*
* @author Phillip Webb
* @since 1.1.2
* @see JavaCompilerFieldValuesParser
*/
@FunctionalInterface
public interface FieldValuesParser {
/**
* Implementation of {@link FieldValuesParser} that always returns an empty result.
*/
FieldValuesParser NONE = (element) -> Collections.emptyMap();
/**
* Return the field values for the given element.
* @param element the element to inspect
* @return a map of field names to values.
* @throws Exception if the values cannot be extracted
*/
Map<String, Object> getFieldValues(TypeElement element) throws Exception;
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.fieldvalues.javac;
import java.lang.reflect.Method;
import java.util.ArrayList;
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 {
private final Class<?> literalTreeType = findClass("com.sun.source.tree.LiteralTree");
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,
"getInitializers");
ExpressionTree(Object instance) {
super("com.sun.source.tree.ExpressionTree", instance);
}
public String getKind() throws Exception {
return findMethod("getKind").invoke(getInstance()).toString();
}
public Object getLiteralValue() throws Exception {
if (this.literalTreeType.isAssignableFrom(getInstance().getClass())) {
return this.literalValueMethod.invoke(getInstance());
}
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());
List<ExpressionTree> result = new ArrayList<>();
if (elements == null) {
return result;
}
for (Object element : elements) {
result.add(new ExpressionTree(element));
}
return result;
}
return null;
}
}

View File

@@ -0,0 +1,162 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.fieldvalues.javac;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import org.springframework.boot.configurationprocessor.fieldvalues.FieldValuesParser;
/**
* {@link FieldValuesParser} implementation for the standard Java compiler.
*
* @author Phillip Webb
* @author Stephane Nicoll
* @since 1.2.0
*/
public class JavaCompilerFieldValuesParser implements FieldValuesParser {
private final Trees trees;
public JavaCompilerFieldValuesParser(ProcessingEnvironment env) throws Exception {
this.trees = Trees.instance(env);
}
@Override
public Map<String, Object> getFieldValues(TypeElement element) throws Exception {
Tree tree = this.trees.getTree(element);
if (tree != null) {
FieldCollector fieldCollector = new FieldCollector();
tree.accept(fieldCollector);
return fieldCollector.getFieldValues();
}
return Collections.emptyMap();
}
/**
* {@link TreeVisitor} to collect fields.
*/
private static class FieldCollector implements TreeVisitor {
private static final Map<String, Class<?>> WRAPPER_TYPES;
static {
Map<String, Class<?>> types = new HashMap<>();
types.put("boolean", Boolean.class);
types.put(Boolean.class.getName(), Boolean.class);
types.put("byte", Byte.class);
types.put(Byte.class.getName(), Byte.class);
types.put("short", Short.class);
types.put(Short.class.getName(), Short.class);
types.put("int", Integer.class);
types.put(Integer.class.getName(), Integer.class);
types.put("long", Long.class);
types.put(Long.class.getName(), Long.class);
WRAPPER_TYPES = Collections.unmodifiableMap(types);
}
private static final Map<Class<?>, Object> defaultTypeValues;
static {
Map<Class<?>, Object> values = new HashMap<>();
values.put(Boolean.class, false);
values.put(Byte.class, (byte) 0);
values.put(Short.class, (short) 0);
values.put(Integer.class, 0);
values.put(Long.class, (long) 0);
defaultTypeValues = Collections.unmodifiableMap(values);
}
private static final Map<String, Object> wellKnownStaticFinals;
static {
Map<String, Object> values = new HashMap<>();
values.put("Boolean.TRUE", true);
values.put("Boolean.FALSE", false);
wellKnownStaticFinals = Collections.unmodifiableMap(values);
}
private final Map<String, Object> fieldValues = new HashMap<>();
private final Map<String, Object> staticFinals = new HashMap<>();
@Override
public void visitVariable(VariableTree variable) throws Exception {
Set<Modifier> flags = variable.getModifierFlags();
if (flags.contains(Modifier.STATIC) && flags.contains(Modifier.FINAL)) {
this.staticFinals.put(variable.getName(), getValue(variable));
}
if (!flags.contains(Modifier.FINAL)) {
this.fieldValues.put(variable.getName(), getValue(variable));
}
}
private Object getValue(VariableTree variable) throws Exception {
ExpressionTree initializer = variable.getInitializer();
Class<?> wrapperType = WRAPPER_TYPES.get(variable.getType());
Object defaultValue = defaultTypeValues.get(wrapperType);
if (initializer != null) {
return getValue(initializer, defaultValue);
}
return defaultValue;
}
private Object getValue(ExpressionTree expression, Object defaultValue)
throws Exception {
Object literalValue = expression.getLiteralValue();
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()];
for (int i = 0; i < arrayValues.size(); i++) {
Object value = getValue(arrayValues.get(i), null);
if (value == null) { // One of the elements could not be resolved
return defaultValue;
}
result[i] = value;
}
return result;
}
if (expression.getKind().equals("IDENTIFIER")) {
return this.staticFinals.get(expression.toString());
}
if (expression.getKind().equals("MEMBER_SELECT")) {
return wellKnownStaticFinals.get(expression.toString());
}
return defaultValue;
}
public Map<String, Object> getFieldValues() {
return this.fieldValues;
}
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.fieldvalues.javac;
import java.lang.reflect.Method;
/**
* Base class for reflection based wrappers. Used to access internal Java classes without
* needing tools.jar on the classpath.
*
* @author Phillip Webb
* @since 1.2.0
*/
class ReflectionWrapper {
private final Class<?> type;
private final Object instance;
ReflectionWrapper(String type, Object instance) {
this.type = findClass(instance.getClass().getClassLoader(), type);
this.instance = this.type.cast(instance);
}
protected final Object getInstance() {
return this.instance;
}
@Override
public String toString() {
return this.instance.toString();
}
protected Class<?> findClass(String name) {
return findClass(getInstance().getClass().getClassLoader(), name);
}
protected Method findMethod(String name, Class<?>... parameterTypes) {
return findMethod(this.type, name, parameterTypes);
}
protected static Class<?> findClass(ClassLoader classLoader, String name) {
try {
return classLoader.loadClass(name);
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(ex);
}
}
protected static Method findMethod(Class<?> type, String name,
Class<?>... parameterTypes) {
try {
return type.getMethod(name, parameterTypes);
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.fieldvalues.javac;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* Reflection based access to {@code com.sun.source.tree.Tree}.
*
* @author Phillip Webb
* @since 1.2.0
*/
class Tree extends ReflectionWrapper {
private final Class<?> treeVisitorType = findClass("com.sun.source.tree.TreeVisitor");
private final Method acceptMethod = findMethod("accept", this.treeVisitorType,
Object.class);
private final Method GET_CLASS_TREE_MEMBERS = findMethod(
findClass("com.sun.source.tree.ClassTree"), "getMembers");
Tree(Object instance) {
super("com.sun.source.tree.Tree", instance);
}
public void accept(TreeVisitor visitor) throws Exception {
this.acceptMethod.invoke(getInstance(),
Proxy.newProxyInstance(getInstance().getClass().getClassLoader(),
new Class<?>[] { this.treeVisitorType },
new TreeVisitorInvocationHandler(visitor)),
0);
}
/**
* {@link InvocationHandler} to call the {@link TreeVisitor}.
*/
private class TreeVisitorInvocationHandler implements InvocationHandler {
private TreeVisitor treeVisitor;
TreeVisitorInvocationHandler(TreeVisitor treeVisitor) {
this.treeVisitor = treeVisitor;
}
@Override
@SuppressWarnings("rawtypes")
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if (method.getName().equals("visitClass")) {
if ((Integer) args[1] == 0) {
Iterable members = (Iterable) Tree.this.GET_CLASS_TREE_MEMBERS
.invoke(args[0]);
for (Object member : members) {
if (member != null) {
Tree.this.acceptMethod.invoke(member, proxy,
((Integer) args[1]) + 1);
}
}
}
}
if (method.getName().equals("visitVariable")) {
this.treeVisitor.visitVariable(new VariableTree(args[0]));
}
return null;
}
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.fieldvalues.javac;
/**
* Reflection base alternative for {@code com.sun.source.tree.TreeVisitor}.
*
* @author Phillip Webb
* @since 1.2.0
*/
interface TreeVisitor {
void visitVariable(VariableTree variable) throws Exception;
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.fieldvalues.javac;
import java.lang.reflect.Method;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
/**
* Reflection based access to {@code com.sun.source.util.Trees}.
*
* @author Phillip Webb
* @since 1.2.0
*/
final class Trees extends ReflectionWrapper {
private Trees(Object instance) {
super("com.sun.source.util.Trees", instance);
}
public Tree getTree(Element element) throws Exception {
Object tree = findMethod("getTree", Element.class).invoke(getInstance(), element);
return (tree == null ? null : new Tree(tree));
}
public static Trees instance(ProcessingEnvironment env) throws Exception {
ClassLoader classLoader = env.getClass().getClassLoader();
Class<?> type = findClass(classLoader, "com.sun.source.util.Trees");
Method method = findMethod(type, "instance", ProcessingEnvironment.class);
return new Trees(method.invoke(null, env));
}
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.fieldvalues.javac;
import java.util.Collections;
import java.util.Set;
import javax.lang.model.element.Modifier;
/**
* Reflection based access to {@code com.sun.source.tree.VariableTree}.
*
* @author Phillip Webb
* @since 1.2.0
*/
class VariableTree extends ReflectionWrapper {
VariableTree(Object instance) {
super("com.sun.source.tree.VariableTree", instance);
}
public String getName() throws Exception {
return findMethod("getName").invoke(getInstance()).toString();
}
public String getType() throws Exception {
return findMethod("getType").invoke(getInstance()).toString();
}
public ExpressionTree getInitializer() throws Exception {
Object instance = findMethod("getInitializer").invoke(getInstance());
return (instance == null ? null : new ExpressionTree(instance));
}
@SuppressWarnings("unchecked")
public Set<Modifier> getModifierFlags() throws Exception {
Object modifiers = findMethod("getModifiers").invoke(getInstance());
if (modifiers == null) {
return Collections.emptySet();
}
return (Set<Modifier>) findMethod(findClass("com.sun.source.tree.ModifiersTree"),
"getFlags").invoke(modifiers);
}
}

View File

@@ -0,0 +1,215 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.metadata;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Configuration meta-data.
*
* @author Stephane Nicoll
* @author Phillip Webb
* @since 1.2.0
* @see ItemMetadata
*/
public class ConfigurationMetadata {
private static final Set<Character> SEPARATORS;
static {
List<Character> chars = Arrays.asList('-', '_');
SEPARATORS = Collections.unmodifiableSet(new HashSet<>(chars));
}
private final Map<String, List<ItemMetadata>> items;
private final Map<String, List<ItemHint>> hints;
public ConfigurationMetadata() {
this.items = new LinkedHashMap<>();
this.hints = new LinkedHashMap<>();
}
public ConfigurationMetadata(ConfigurationMetadata metadata) {
this.items = new LinkedHashMap<>(metadata.items);
this.hints = new LinkedHashMap<>(metadata.hints);
}
/**
* Add item meta-data.
* @param itemMetadata the meta-data to add
*/
public void add(ItemMetadata itemMetadata) {
add(this.items, itemMetadata.getName(), itemMetadata);
}
/**
* Add item hint.
* @param itemHint the item hint to add
*/
public void add(ItemHint itemHint) {
add(this.hints, itemHint.getName(), itemHint);
}
/**
* Merge the content from another {@link ConfigurationMetadata}.
* @param metadata the {@link ConfigurationMetadata} instance to merge
*/
public void merge(ConfigurationMetadata metadata) {
for (ItemMetadata additionalItem : metadata.getItems()) {
mergeItemMetadata(additionalItem);
}
for (ItemHint itemHint : metadata.getHints()) {
add(itemHint);
}
}
/**
* Return item meta-data.
* @return the items
*/
public List<ItemMetadata> getItems() {
return flattenValues(this.items);
}
/**
* Return hint meta-data.
* @return the hints
*/
public List<ItemHint> getHints() {
return flattenValues(this.hints);
}
protected void mergeItemMetadata(ItemMetadata metadata) {
ItemMetadata matching = findMatchingItemMetadata(metadata);
if (matching != null) {
if (metadata.getDescription() != null) {
matching.setDescription(metadata.getDescription());
}
if (metadata.getDefaultValue() != null) {
matching.setDefaultValue(metadata.getDefaultValue());
}
ItemDeprecation deprecation = metadata.getDeprecation();
ItemDeprecation matchingDeprecation = matching.getDeprecation();
if (deprecation != null) {
if (matchingDeprecation == null) {
matching.setDeprecation(deprecation);
}
else {
if (deprecation.getReason() != null) {
matchingDeprecation.setReason(deprecation.getReason());
}
if (deprecation.getReplacement() != null) {
matchingDeprecation.setReplacement(deprecation.getReplacement());
}
if (deprecation.getLevel() != null) {
matchingDeprecation.setLevel(deprecation.getLevel());
}
}
}
}
else {
add(this.items, metadata.getName(), metadata);
}
}
private <K, V> void add(Map<K, List<V>> map, K key, V value) {
List<V> values = map.get(key);
if (values == null) {
values = new ArrayList<>();
map.put(key, values);
}
values.add(value);
}
private ItemMetadata findMatchingItemMetadata(ItemMetadata metadata) {
List<ItemMetadata> candidates = this.items.get(metadata.getName());
if (candidates == null || candidates.isEmpty()) {
return null;
}
candidates.removeIf((itemMetadata) -> !itemMetadata.hasSameType(metadata));
if (candidates.size() == 1) {
return candidates.get(0);
}
for (ItemMetadata candidate : candidates) {
if (nullSafeEquals(candidate.getSourceType(), metadata.getSourceType())) {
return candidate;
}
}
return null;
}
private boolean nullSafeEquals(Object o1, Object o2) {
if (o1 == o2) {
return true;
}
return o1 != null && o2 != null && o1.equals(o2);
}
public static String nestedPrefix(String prefix, String name) {
String nestedPrefix = (prefix == null ? "" : prefix);
String dashedName = toDashedCase(name);
nestedPrefix += ("".equals(nestedPrefix) ? dashedName : "." + dashedName);
return nestedPrefix;
}
static String toDashedCase(String name) {
StringBuilder dashed = new StringBuilder();
Character previous = null;
for (char current : name.toCharArray()) {
if (SEPARATORS.contains(current)) {
dashed.append("-");
}
else if (Character.isUpperCase(current) && previous != null
&& !SEPARATORS.contains(previous)) {
dashed.append("-").append(current);
}
else {
dashed.append(current);
}
previous = current;
}
return dashed.toString().toLowerCase();
}
private static <T extends Comparable<T>> List<T> flattenValues(Map<?, List<T>> map) {
List<T> content = new ArrayList<>();
for (List<T> values : map.values()) {
content.addAll(values);
}
Collections.sort(content);
return content;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append(String.format("items: %n"));
this.items.values().forEach((itemMetadata) -> result.append("\t")
.append(String.format("%s%n", itemMetadata)));
return result.toString();
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.metadata;
import javax.tools.Diagnostic;
/**
* Thrown to indicate that some meta-data is invalid. Define the severity to determine
* whether it has to fail the build.
*
* @author Stephane Nicoll
* @since 1.3.0
*/
@SuppressWarnings("serial")
public class InvalidConfigurationMetadataException extends RuntimeException {
private final Diagnostic.Kind kind;
public InvalidConfigurationMetadataException(String message, Diagnostic.Kind kind) {
super(message);
this.kind = kind;
}
public Diagnostic.Kind getKind() {
return this.kind;
}
}

View File

@@ -0,0 +1,114 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.metadata;
/**
* Describe an item deprecation.
*
* @author Stephane Nicoll
* @since 1.3.0
*/
public class ItemDeprecation {
private String reason;
private String replacement;
private String level;
public ItemDeprecation() {
this(null, null);
}
public ItemDeprecation(String reason, String replacement) {
this(reason, replacement, null);
}
public ItemDeprecation(String reason, String replacement, String level) {
this.reason = reason;
this.replacement = replacement;
this.level = level;
}
public String getReason() {
return this.reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public String getReplacement() {
return this.replacement;
}
public void setReplacement(String replacement) {
this.replacement = replacement;
}
public String getLevel() {
return this.level;
}
public void setLevel(String level) {
this.level = level;
}
@Override
public String toString() {
return "ItemDeprecation{" + "reason='" + this.reason + '\'' + ", "
+ "replacement='" + this.replacement + '\'' + ", " + "level='"
+ this.level + '\'' + '}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ItemDeprecation other = (ItemDeprecation) o;
return nullSafeEquals(this.reason, other.reason)
&& nullSafeEquals(this.replacement, other.replacement)
&& nullSafeEquals(this.level, other.level);
}
@Override
public int hashCode() {
int result = nullSafeHashCode(this.reason);
result = 31 * result + nullSafeHashCode(this.replacement);
result = 31 * result + nullSafeHashCode(this.level);
return result;
}
private boolean nullSafeEquals(Object o1, Object o2) {
if (o1 == o2) {
return true;
}
if (o1 == null || o2 == null) {
return false;
}
return o1.equals(o2);
}
private int nullSafeHashCode(Object o) {
return (o == null ? 0 : o.hashCode());
}
}

View File

@@ -0,0 +1,150 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.metadata;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Provide hints on an {@link ItemMetadata}. Defines the list of possible values for a
* particular item as {@link ItemHint.ValueHint} instances.
* <p>
* The {@code name} of the hint is the name of the related property with one major
* exception for map types as both the keys and values of the map can have hints. In such
* a case, the hint should be suffixed by ".keys" or ".values" respectively. Creating a
* hint for a map using its property name is therefore invalid.
*
* @author Stephane Nicoll
* @since 1.3.0
*/
public class ItemHint implements Comparable<ItemHint> {
private final String name;
private final List<ValueHint> values;
private final List<ValueProvider> providers;
public ItemHint(String name, List<ValueHint> values, List<ValueProvider> providers) {
this.name = toCanonicalName(name);
this.values = (values != null ? new ArrayList<>(values) : new ArrayList<>());
this.providers = (providers != null ? new ArrayList<>(providers)
: new ArrayList<>());
}
private String toCanonicalName(String name) {
int dot = name.lastIndexOf('.');
if (dot != -1) {
String prefix = name.substring(0, dot);
String originalName = name.substring(dot);
return prefix + ConfigurationMetadata.toDashedCase(originalName);
}
return ConfigurationMetadata.toDashedCase(name);
}
public String getName() {
return this.name;
}
public List<ValueHint> getValues() {
return Collections.unmodifiableList(this.values);
}
public List<ValueProvider> getProviders() {
return Collections.unmodifiableList(this.providers);
}
@Override
public int compareTo(ItemHint other) {
return getName().compareTo(other.getName());
}
public static ItemHint newHint(String name, ValueHint... values) {
return new ItemHint(name, Arrays.asList(values),
Collections.<ValueProvider>emptyList());
}
@Override
public String toString() {
return "ItemHint{" + "name='" + this.name + "', values=" + this.values
+ ", providers=" + this.providers + '}';
}
/**
* A hint for a value.
*/
public static class ValueHint {
private final Object value;
private final String description;
public ValueHint(Object value, String description) {
this.value = value;
this.description = description;
}
public Object getValue() {
return this.value;
}
public String getDescription() {
return this.description;
}
@Override
public String toString() {
return "ValueHint{" + "value=" + this.value + ", description='"
+ this.description + '\'' + '}';
}
}
/**
* A value provider.
*/
public static class ValueProvider {
private final String name;
private final Map<String, Object> parameters;
public ValueProvider(String name, Map<String, Object> parameters) {
this.name = name;
this.parameters = parameters;
}
public String getName() {
return this.name;
}
public Map<String, Object> getParameters() {
return this.parameters;
}
@Override
public String toString() {
return "ValueProvider{" + "name='" + this.name + "', parameters="
+ this.parameters + '}';
}
}
}

View File

@@ -0,0 +1,236 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.metadata;
/**
* A group or property meta-data item from some {@link ConfigurationMetadata}.
*
* @author Stephane Nicoll
* @author Phillip Webb
* @since 1.2.0
* @see ConfigurationMetadata
*/
public final class ItemMetadata implements Comparable<ItemMetadata> {
private ItemType itemType;
private String name;
private String type;
private String description;
private String sourceType;
private String sourceMethod;
private Object defaultValue;
private ItemDeprecation deprecation;
ItemMetadata(ItemType itemType, String prefix, String name, String type,
String sourceType, String sourceMethod, String description,
Object defaultValue, ItemDeprecation deprecation) {
super();
this.itemType = itemType;
this.name = buildName(prefix, name);
this.type = type;
this.sourceType = sourceType;
this.sourceMethod = sourceMethod;
this.description = description;
this.defaultValue = defaultValue;
this.deprecation = deprecation;
}
private String buildName(String prefix, String name) {
while (prefix != null && prefix.endsWith(".")) {
prefix = prefix.substring(0, prefix.length() - 1);
}
StringBuilder fullName = new StringBuilder(prefix == null ? "" : prefix);
if (fullName.length() > 0 && name != null) {
fullName.append(".");
}
fullName.append(name == null ? "" : ConfigurationMetadata.toDashedCase(name));
return fullName.toString();
}
public boolean isOfItemType(ItemType itemType) {
return this.itemType == itemType;
}
public boolean hasSameType(ItemMetadata metadata) {
return this.itemType == metadata.itemType;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String getSourceType() {
return this.sourceType;
}
public void setSourceType(String sourceType) {
this.sourceType = sourceType;
}
public String getSourceMethod() {
return this.sourceMethod;
}
public void setSourceMethod(String sourceMethod) {
this.sourceMethod = sourceMethod;
}
public Object getDefaultValue() {
return this.defaultValue;
}
public void setDefaultValue(Object defaultValue) {
this.defaultValue = defaultValue;
}
public ItemDeprecation getDeprecation() {
return this.deprecation;
}
public void setDeprecation(ItemDeprecation deprecation) {
this.deprecation = deprecation;
}
@Override
public String toString() {
StringBuilder string = new StringBuilder(this.name);
buildToStringProperty(string, "type", this.type);
buildToStringProperty(string, "sourceType", this.sourceType);
buildToStringProperty(string, "description", this.description);
buildToStringProperty(string, "defaultValue", this.defaultValue);
buildToStringProperty(string, "deprecation", this.deprecation);
return string.toString();
}
protected void buildToStringProperty(StringBuilder string, String property,
Object value) {
if (value != null) {
string.append(" ").append(property).append(":").append(value);
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ItemMetadata other = (ItemMetadata) o;
boolean result = true;
result = result && nullSafeEquals(this.itemType, other.itemType);
result = result && nullSafeEquals(this.name, other.name);
result = result && nullSafeEquals(this.type, other.type);
result = result && nullSafeEquals(this.description, other.description);
result = result && nullSafeEquals(this.sourceType, other.sourceType);
result = result && nullSafeEquals(this.sourceMethod, other.sourceMethod);
result = result && nullSafeEquals(this.defaultValue, other.defaultValue);
result = result && nullSafeEquals(this.deprecation, other.deprecation);
return result;
}
@Override
public int hashCode() {
int result = nullSafeHashCode(this.itemType);
result = 31 * result + nullSafeHashCode(this.name);
result = 31 * result + nullSafeHashCode(this.type);
result = 31 * result + nullSafeHashCode(this.description);
result = 31 * result + nullSafeHashCode(this.sourceType);
result = 31 * result + nullSafeHashCode(this.sourceMethod);
result = 31 * result + nullSafeHashCode(this.defaultValue);
result = 31 * result + nullSafeHashCode(this.deprecation);
return result;
}
private boolean nullSafeEquals(Object o1, Object o2) {
if (o1 == o2) {
return true;
}
if (o1 == null || o2 == null) {
return false;
}
return o1.equals(o2);
}
private int nullSafeHashCode(Object o) {
return (o == null ? 0 : o.hashCode());
}
@Override
public int compareTo(ItemMetadata o) {
return getName().compareTo(o.getName());
}
public static ItemMetadata newGroup(String name, String type, String sourceType,
String sourceMethod) {
return new ItemMetadata(ItemType.GROUP, name, null, type, sourceType,
sourceMethod, null, null, null);
}
public static ItemMetadata newProperty(String prefix, String name, String type,
String sourceType, String sourceMethod, String description,
Object defaultValue, ItemDeprecation deprecation) {
return new ItemMetadata(ItemType.PROPERTY, prefix, name, type, sourceType,
sourceMethod, description, defaultValue, deprecation);
}
/**
* The item type.
*/
public enum ItemType {
/**
* Group item type.
*/
GROUP,
/**
* Property item type.
*/
PROPERTY
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.metadata;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Extension to {@link JSONObject} that remembers the order of inserts.
*
* @author Stephane Nicoll
* @author Phillip Webb
*/
@SuppressWarnings("rawtypes")
class JSONOrderedObject extends JSONObject {
private Set<String> keys = new LinkedHashSet<>();
@Override
public JSONObject put(String key, Object value) throws JSONException {
this.keys.add(key);
return super.put(key, value);
}
@Override
public Iterator keys() {
return this.keys.iterator();
}
}

View File

@@ -0,0 +1,164 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.metadata;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
/**
* Converter to change meta-data objects into JSON objects.
*
* @author Stephane Nicoll
* @author Phillip Webb
*/
class JsonConverter {
public JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType)
throws Exception {
JSONArray jsonArray = new JSONArray();
for (ItemMetadata item : metadata.getItems()) {
if (item.isOfItemType(itemType)) {
jsonArray.put(toJsonObject(item));
}
}
return jsonArray;
}
public JSONArray toJsonArray(Collection<ItemHint> hints) throws Exception {
JSONArray jsonArray = new JSONArray();
for (ItemHint hint : hints) {
jsonArray.put(toJsonObject(hint));
}
return jsonArray;
}
public JSONObject toJsonObject(ItemMetadata item) throws Exception {
JSONObject jsonObject = new JSONOrderedObject();
jsonObject.put("name", item.getName());
putIfPresent(jsonObject, "type", item.getType());
putIfPresent(jsonObject, "description", item.getDescription());
putIfPresent(jsonObject, "sourceType", item.getSourceType());
putIfPresent(jsonObject, "sourceMethod", item.getSourceMethod());
Object defaultValue = item.getDefaultValue();
if (defaultValue != null) {
putDefaultValue(jsonObject, defaultValue);
}
ItemDeprecation deprecation = item.getDeprecation();
if (deprecation != null) {
jsonObject.put("deprecated", true); // backward compatibility
JSONObject deprecationJsonObject = new JSONObject();
if (deprecation.getLevel() != null) {
deprecationJsonObject.put("level", deprecation.getLevel());
}
if (deprecation.getReason() != null) {
deprecationJsonObject.put("reason", deprecation.getReason());
}
if (deprecation.getReplacement() != null) {
deprecationJsonObject.put("replacement", deprecation.getReplacement());
}
jsonObject.put("deprecation", deprecationJsonObject);
}
return jsonObject;
}
private JSONObject toJsonObject(ItemHint hint) throws Exception {
JSONObject jsonObject = new JSONOrderedObject();
jsonObject.put("name", hint.getName());
if (!hint.getValues().isEmpty()) {
jsonObject.put("values", getItemHintValues(hint));
}
if (!hint.getProviders().isEmpty()) {
jsonObject.put("providers", getItemHintProviders(hint));
}
return jsonObject;
}
private JSONArray getItemHintValues(ItemHint hint) throws Exception {
JSONArray values = new JSONArray();
for (ItemHint.ValueHint value : hint.getValues()) {
values.put(getItemHintValue(value));
}
return values;
}
private JSONObject getItemHintValue(ItemHint.ValueHint value) throws Exception {
JSONObject result = new JSONOrderedObject();
putHintValue(result, value.getValue());
putIfPresent(result, "description", value.getDescription());
return result;
}
private JSONArray getItemHintProviders(ItemHint hint) throws Exception {
JSONArray providers = new JSONArray();
for (ItemHint.ValueProvider provider : hint.getProviders()) {
providers.put(getItemHintProvider(provider));
}
return providers;
}
private JSONObject getItemHintProvider(ItemHint.ValueProvider provider)
throws Exception {
JSONObject result = new JSONOrderedObject();
result.put("name", provider.getName());
if (provider.getParameters() != null && !provider.getParameters().isEmpty()) {
JSONObject parameters = new JSONOrderedObject();
for (Map.Entry<String, Object> entry : provider.getParameters().entrySet()) {
parameters.put(entry.getKey(), extractItemValue(entry.getValue()));
}
result.put("parameters", parameters);
}
return result;
}
private void putIfPresent(JSONObject jsonObject, String name, Object value)
throws Exception {
if (value != null) {
jsonObject.put(name, value);
}
}
private void putHintValue(JSONObject jsonObject, Object value) throws Exception {
Object hintValue = extractItemValue(value);
jsonObject.put("value", hintValue);
}
private void putDefaultValue(JSONObject jsonObject, Object value) throws Exception {
Object defaultValue = extractItemValue(value);
jsonObject.put("defaultValue", defaultValue);
}
private Object extractItemValue(Object value) {
Object defaultValue = value;
if (value.getClass().isArray()) {
JSONArray array = new JSONArray();
int length = Array.getLength(value);
for (int i = 0; i < length; i++) {
array.put(Array.get(value, i));
}
defaultValue = array;
}
return defaultValue;
}
}

View File

@@ -0,0 +1,182 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.metadata;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
/**
* Marshaller to write {@link ConfigurationMetadata} as JSON.
*
* @author Stephane Nicoll
* @author Phillip Webb
* @since 1.2.0
*/
public class JsonMarshaller {
private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final int BUFFER_SIZE = 4098;
public void write(ConfigurationMetadata metadata, OutputStream outputStream)
throws IOException {
try {
JSONObject object = new JSONOrderedObject();
JsonConverter converter = new JsonConverter();
object.put("groups", converter.toJsonArray(metadata, ItemType.GROUP));
object.put("properties", converter.toJsonArray(metadata, ItemType.PROPERTY));
object.put("hints", converter.toJsonArray(metadata.getHints()));
outputStream.write(object.toString(2).getBytes(UTF_8));
}
catch (Exception ex) {
if (ex instanceof IOException) {
throw (IOException) ex;
}
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
throw new IllegalStateException(ex);
}
}
public ConfigurationMetadata read(InputStream inputStream) throws Exception {
ConfigurationMetadata metadata = new ConfigurationMetadata();
JSONObject object = new JSONObject(toString(inputStream));
JSONArray groups = object.optJSONArray("groups");
if (groups != null) {
for (int i = 0; i < groups.length(); i++) {
metadata.add(toItemMetadata((JSONObject) groups.get(i), ItemType.GROUP));
}
}
JSONArray properties = object.optJSONArray("properties");
if (properties != null) {
for (int i = 0; i < properties.length(); i++) {
metadata.add(toItemMetadata((JSONObject) properties.get(i),
ItemType.PROPERTY));
}
}
JSONArray hints = object.optJSONArray("hints");
if (hints != null) {
for (int i = 0; i < hints.length(); i++) {
metadata.add(toItemHint((JSONObject) hints.get(i)));
}
}
return metadata;
}
private ItemMetadata toItemMetadata(JSONObject object, ItemType itemType)
throws Exception {
String name = object.getString("name");
String type = object.optString("type", null);
String description = object.optString("description", null);
String sourceType = object.optString("sourceType", null);
String sourceMethod = object.optString("sourceMethod", null);
Object defaultValue = readItemValue(object.opt("defaultValue"));
ItemDeprecation deprecation = toItemDeprecation(object);
return new ItemMetadata(itemType, name, null, type, sourceType, sourceMethod,
description, defaultValue, deprecation);
}
private ItemDeprecation toItemDeprecation(JSONObject object) throws Exception {
if (object.has("deprecation")) {
JSONObject deprecationJsonObject = object.getJSONObject("deprecation");
ItemDeprecation deprecation = new ItemDeprecation();
deprecation.setLevel(deprecationJsonObject.optString("level", null));
deprecation.setReason(deprecationJsonObject.optString("reason", null));
deprecation
.setReplacement(deprecationJsonObject.optString("replacement", null));
return deprecation;
}
return (object.optBoolean("deprecated") ? new ItemDeprecation() : null);
}
private ItemHint toItemHint(JSONObject object) throws Exception {
String name = object.getString("name");
List<ItemHint.ValueHint> values = new ArrayList<>();
if (object.has("values")) {
JSONArray valuesArray = object.getJSONArray("values");
for (int i = 0; i < valuesArray.length(); i++) {
values.add(toValueHint((JSONObject) valuesArray.get(i)));
}
}
List<ItemHint.ValueProvider> providers = new ArrayList<>();
if (object.has("providers")) {
JSONArray providersObject = object.getJSONArray("providers");
for (int i = 0; i < providersObject.length(); i++) {
providers.add(toValueProvider((JSONObject) providersObject.get(i)));
}
}
return new ItemHint(name, values, providers);
}
private ItemHint.ValueHint toValueHint(JSONObject object) throws Exception {
Object value = readItemValue(object.get("value"));
String description = object.optString("description", null);
return new ItemHint.ValueHint(value, description);
}
private ItemHint.ValueProvider toValueProvider(JSONObject object) throws Exception {
String name = object.getString("name");
Map<String, Object> parameters = new HashMap<>();
if (object.has("parameters")) {
JSONObject parametersObject = object.getJSONObject("parameters");
for (Iterator<?> iterator = parametersObject.keys(); iterator.hasNext();) {
String key = (String) iterator.next();
Object value = readItemValue(parametersObject.get(key));
parameters.put(key, value);
}
}
return new ItemHint.ValueProvider(name, parameters);
}
private Object readItemValue(Object value) throws Exception {
if (value instanceof JSONArray) {
JSONArray array = (JSONArray) value;
Object[] content = new Object[array.length()];
for (int i = 0; i < array.length(); i++) {
content[i] = array.get(i);
}
return content;
}
return value;
}
private String toString(InputStream inputStream) throws IOException {
StringBuilder out = new StringBuilder();
InputStreamReader reader = new InputStreamReader(inputStream, UTF_8);
char[] buffer = new char[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = reader.read(buffer)) != -1) {
out.append(buffer, 0, bytesRead);
}
return out.toString();
}
}

View File

@@ -0,0 +1 @@
org.springframework.boot.configurationprocessor.ConfigurationMetadataAnnotationProcessor

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.io.File;
import java.io.IOException;
import javax.annotation.processing.ProcessingEnvironment;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link MetadataStore}.
*
* @author Andy Wilkinson
*/
public class MetadataStoreTests {
@Rule
public final TemporaryFolder temp = new TemporaryFolder();
private final MetadataStore metadataStore = new MetadataStore(
mock(ProcessingEnvironment.class));
@Test
public void additionalMetadataIsLocatedInMavenBuild() throws IOException {
File app = this.temp.newFolder("app");
File classesLocation = new File(app, "target/classes");
File metaInf = new File(classesLocation, "META-INF");
metaInf.mkdirs();
File additionalMetadata = new File(metaInf,
"additional-spring-configuration-metadata.json");
additionalMetadata.createNewFile();
assertThat(
this.metadataStore.locateAdditionalMetadataFile(new File(classesLocation,
"META-INF/additional-spring-configuration-metadata.json")))
.isEqualTo(additionalMetadata);
}
@Test
public void additionalMetadataIsLocatedInGradle3Build() throws IOException {
File app = this.temp.newFolder("app");
File classesLocation = new File(app, "build/classes/main");
File resourcesLocation = new File(app, "build/resources/main");
File metaInf = new File(resourcesLocation, "META-INF");
metaInf.mkdirs();
File additionalMetadata = new File(metaInf,
"additional-spring-configuration-metadata.json");
additionalMetadata.createNewFile();
assertThat(
this.metadataStore.locateAdditionalMetadataFile(new File(classesLocation,
"META-INF/additional-spring-configuration-metadata.json")))
.isEqualTo(additionalMetadata);
}
@Test
public void additionalMetadataIsLocatedInGradle4Build() throws IOException {
File app = this.temp.newFolder("app");
File classesLocation = new File(app, "build/classes/java/main");
File resourcesLocation = new File(app, "build/resources/main");
File metaInf = new File(resourcesLocation, "META-INF");
metaInf.mkdirs();
File additionalMetadata = new File(metaInf,
"additional-spring-configuration-metadata.json");
additionalMetadata.createNewFile();
assertThat(
this.metadataStore.locateAdditionalMetadataFile(new File(classesLocation,
"META-INF/additional-spring-configuration-metadata.json")))
.isEqualTo(additionalMetadata);
}
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller;
/**
* Test {@link ConfigurationMetadataAnnotationProcessor}.
*
* @author Stephane Nicoll
* @author Phillip Webb
* @author Andy Wilkinson
* @author Kris De Volder
*/
@SupportedAnnotationTypes({ "*" })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class TestConfigurationMetadataAnnotationProcessor
extends ConfigurationMetadataAnnotationProcessor {
static final String CONFIGURATION_PROPERTIES_ANNOTATION = "org.springframework.boot.configurationsample.ConfigurationProperties";
static final String NESTED_CONFIGURATION_PROPERTY_ANNOTATION = "org.springframework.boot.configurationsample.NestedConfigurationProperty";
static final String DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION = "org.springframework.boot.configurationsample.DeprecatedConfigurationProperty";
static final String ENDPOINT_ANNOTATION = "org.springframework.boot.configurationsample.Endpoint";
private ConfigurationMetadata metadata;
private final File outputLocation;
public TestConfigurationMetadataAnnotationProcessor(File outputLocation) {
this.outputLocation = outputLocation;
}
@Override
protected String configurationPropertiesAnnotation() {
return CONFIGURATION_PROPERTIES_ANNOTATION;
}
@Override
protected String nestedConfigurationPropertyAnnotation() {
return NESTED_CONFIGURATION_PROPERTY_ANNOTATION;
}
@Override
protected String deprecatedConfigurationPropertyAnnotation() {
return DEPRECATED_CONFIGURATION_PROPERTY_ANNOTATION;
}
@Override
protected String endpointAnnotation() {
return ENDPOINT_ANNOTATION;
}
@Override
protected ConfigurationMetadata writeMetaData() throws Exception {
super.writeMetaData();
try {
File metadataFile = new File(this.outputLocation,
"META-INF/spring-configuration-metadata.json");
if (metadataFile.isFile()) {
this.metadata = new JsonMarshaller()
.read(new FileInputStream(metadataFile));
}
else {
this.metadata = new ConfigurationMetadata();
}
return this.metadata;
}
catch (IOException e) {
throw new RuntimeException("Failed to read metadata from disk", e);
}
}
public ConfigurationMetadata getMetadata() {
return this.metadata;
}
}

View File

@@ -0,0 +1,205 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import org.junit.Assert;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.boot.configurationsample.NestedConfigurationProperty;
import org.springframework.boot.testsupport.compiler.TestCompiler;
import org.springframework.boot.testsupport.compiler.TestCompiler.TestCompilationTask;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileSystemUtils;
/**
* A TestProject contains a copy of a subset of test sample code.
* <p>
* Why a copy? Because when doing incremental build testing, we need to make modifications
* to the contents of the 'test project'. But we don't want to actually modify the
* original content itself.
*
* @author Kris De Volder
*/
public class TestProject {
private static final Class<?>[] ALWAYS_INCLUDE = { ConfigurationProperties.class,
NestedConfigurationProperty.class };
/**
* Contains copies of the original source so we can modify it safely to test
* incremental builds.
*/
private File sourceFolder;
private TestCompiler compiler;
private Set<File> sourceFiles = new LinkedHashSet<>();
public TestProject(TemporaryFolder tempFolder, Class<?>... classes)
throws IOException {
this.sourceFolder = tempFolder.newFolder();
this.compiler = new TestCompiler(tempFolder) {
@Override
protected File getSourceFolder() {
return TestProject.this.sourceFolder;
}
};
Set<Class<?>> contents = new HashSet<>(Arrays.asList(classes));
contents.addAll(Arrays.asList(ALWAYS_INCLUDE));
copySources(contents);
}
private void copySources(Set<Class<?>> contents) throws IOException {
for (Class<?> type : contents) {
copySources(type);
}
}
private void copySources(Class<?> type) throws IOException {
File original = getOriginalSourceFile(type);
File target = getSourceFile(type);
target.getParentFile().mkdirs();
FileCopyUtils.copy(original, target);
this.sourceFiles.add(target);
}
public File getSourceFile(Class<?> type) {
return new File(this.sourceFolder, TestCompiler.sourcePathFor(type));
}
public ConfigurationMetadata fullBuild() {
TestConfigurationMetadataAnnotationProcessor processor = new TestConfigurationMetadataAnnotationProcessor(
this.compiler.getOutputLocation());
TestCompilationTask task = this.compiler.getTask(this.sourceFiles);
deleteFolderContents(this.compiler.getOutputLocation());
task.call(processor);
return processor.getMetadata();
}
public ConfigurationMetadata incrementalBuild(Class<?>... toRecompile) {
TestConfigurationMetadataAnnotationProcessor processor = new TestConfigurationMetadataAnnotationProcessor(
this.compiler.getOutputLocation());
TestCompilationTask task = this.compiler.getTask(toRecompile);
task.call(processor);
return processor.getMetadata();
}
private void deleteFolderContents(File outputFolder) {
FileSystemUtils.deleteRecursively(outputFolder);
outputFolder.mkdirs();
}
/**
* Retrieve File relative to project's output folder.
* @param relativePath the relative path
* @return the output file
*/
public File getOutputFile(String relativePath) {
Assert.assertFalse(new File(relativePath).isAbsolute());
return new File(this.compiler.getOutputLocation(), relativePath);
}
/**
* Add source code at the end of file, just before last '}'
* @param target the target
* @param snippetStream the snippet stream
* @throws Exception if the source cannot be added
*/
public void addSourceCode(Class<?> target, InputStream snippetStream)
throws Exception {
File targetFile = getSourceFile(target);
String contents = getContents(targetFile);
int insertAt = contents.lastIndexOf('}');
String additionalSource = FileCopyUtils
.copyToString(new InputStreamReader(snippetStream));
contents = contents.substring(0, insertAt) + additionalSource
+ contents.substring(insertAt);
putContents(targetFile, contents);
}
/**
* Delete source file for given class from project.
* @param type the class to delete
*/
public void delete(Class<?> type) {
File target = getSourceFile(type);
target.delete();
this.sourceFiles.remove(target);
}
/**
* Restore source code of given class to its original contents.
* @param type the class to revert
* @throws IOException on IO error
*/
public void revert(Class<?> type) throws IOException {
Assert.assertTrue(getSourceFile(type).exists());
copySources(type);
}
/**
* Add source code of given class to this project.
* @param type the class to add
* @throws IOException on IO error
*/
public void add(Class<?> type) throws IOException {
Assert.assertFalse(getSourceFile(type).exists());
copySources(type);
}
public void replaceText(Class<?> type, String find, String replace) throws Exception {
File target = getSourceFile(type);
String contents = getContents(target);
contents = contents.replace(find, replace);
putContents(target, contents);
}
/**
* Find the 'original' source code for given test class. Clients or subclasses should
* have no need to know about these. They should work only with the copied source
* code.
*/
private File getOriginalSourceFile(Class<?> type) {
return new File(TestCompiler.SOURCE_FOLDER, TestCompiler.sourcePathFor(type));
}
private static void putContents(File targetFile, String contents)
throws FileNotFoundException, IOException, UnsupportedEncodingException {
FileCopyUtils.copy(new StringReader(contents), new FileWriter(targetFile));
}
private static String getContents(File file) throws Exception {
return FileCopyUtils.copyToString(new FileReader(file));
}
}

View File

@@ -0,0 +1,132 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.fieldvalues;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.configurationsample.fieldvalues.FieldValues;
import org.springframework.boot.testsupport.compiler.TestCompiler;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Abstract base class for {@link FieldValuesParser} tests.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
public abstract class AbstractFieldValuesProcessorTests {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
protected abstract FieldValuesParser createProcessor(ProcessingEnvironment env);
@Test
public void getFieldValues() throws Exception {
TestProcessor processor = new TestProcessor();
TestCompiler compiler = new TestCompiler(this.temporaryFolder);
compiler.getTask(FieldValues.class).call(processor);
Map<String, Object> values = processor.getValues();
assertThat(values.get("string")).isEqualTo("1");
assertThat(values.get("stringNone")).isNull();
assertThat(values.get("stringConst")).isEqualTo("c");
assertThat(values.get("bool")).isEqualTo(true);
assertThat(values.get("boolNone")).isEqualTo(false);
assertThat(values.get("boolConst")).isEqualTo(true);
assertThat(values.get("boolObject")).isEqualTo(true);
assertThat(values.get("boolObjectNone")).isNull();
assertThat(values.get("boolObjectConst")).isEqualTo(true);
assertThat(values.get("integer")).isEqualTo(1);
assertThat(values.get("integerNone")).isEqualTo(0);
assertThat(values.get("integerConst")).isEqualTo(2);
assertThat(values.get("integerObject")).isEqualTo(3);
assertThat(values.get("integerObjectNone")).isNull();
assertThat(values.get("integerObjectConst")).isEqualTo(4);
assertThat(values.get("charset")).isEqualTo("US-ASCII");
assertThat(values.get("charsetConst")).isEqualTo("UTF-8");
assertThat(values.get("mimeType")).isEqualTo("text/html");
assertThat(values.get("mimeTypeConst")).isEqualTo("text/plain");
assertThat(values.get("object")).isEqualTo(123);
assertThat(values.get("objectNone")).isNull();
assertThat(values.get("objectConst")).isEqualTo("c");
assertThat(values.get("objectInstance")).isNull();
assertThat(values.get("stringArray")).isEqualTo(new Object[] { "FOO", "BAR" });
assertThat(values.get("stringArrayNone")).isNull();
assertThat(values.get("stringEmptyArray")).isEqualTo(new Object[0]);
assertThat(values.get("stringArrayConst")).isEqualTo(new Object[] { "OK", "KO" });
assertThat(values.get("stringArrayConstElements"))
.isEqualTo(new Object[] { "c" });
assertThat(values.get("integerArray")).isEqualTo(new Object[] { 42, 24 });
assertThat(values.get("unknownArray")).isNull();
}
@SupportedAnnotationTypes({
"org.springframework.boot.configurationsample.ConfigurationProperties" })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
private class TestProcessor extends AbstractProcessor {
private FieldValuesParser processor;
private Map<String, Object> values = new HashMap<>();
@Override
public synchronized void init(ProcessingEnvironment env) {
this.processor = createProcessor(env);
}
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
for (TypeElement annotation : annotations) {
for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
if (element instanceof TypeElement) {
try {
this.values.putAll(
this.processor.getFieldValues((TypeElement) element));
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
}
}
return false;
}
public Map<String, Object> getValues() {
return this.values;
}
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.fieldvalues.javac;
import javax.annotation.processing.ProcessingEnvironment;
import org.springframework.boot.configurationprocessor.fieldvalues.AbstractFieldValuesProcessorTests;
import org.springframework.boot.configurationprocessor.fieldvalues.FieldValuesParser;
import static org.junit.Assume.assumeNoException;
/**
* Tests for {@link JavaCompilerFieldValuesParser}.
*
* @author Phillip Webb
*/
public class JavaCompilerFieldValuesProcessorTests
extends AbstractFieldValuesProcessorTests {
@Override
protected FieldValuesParser createProcessor(ProcessingEnvironment env) {
try {
return new JavaCompilerFieldValuesParser(env);
}
catch (Throwable ex) {
assumeNoException(ex);
throw new IllegalStateException();
}
}
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.metadata;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ConfigurationMetadata}.
*
* @author Stephane Nicoll
*/
public class ConfigurationMetadataTests {
@Test
public void toDashedCaseCamelCase() {
assertThat(toDashedCase("simpleCamelCase")).isEqualTo("simple-camel-case");
}
@Test
public void toDashedCaseUpperCamelCaseSuffix() {
assertThat(toDashedCase("myDLQ")).isEqualTo("my-d-l-q");
}
@Test
public void toDashedCaseUpperCamelCaseMiddle() {
assertThat(toDashedCase("someDLQKey")).isEqualTo("some-d-l-q-key");
}
@Test
public void toDashedCaseWordsUnderscore() {
assertThat(toDashedCase("Word_With_underscore"))
.isEqualTo("word-with-underscore");
}
@Test
public void toDashedCaseWordsSeveralUnderscores() {
assertThat(toDashedCase("Word___With__underscore"))
.isEqualTo("word---with--underscore");
}
@Test
public void toDashedCaseLowerCaseUnderscore() {
assertThat(toDashedCase("lower_underscore")).isEqualTo("lower-underscore");
}
@Test
public void toDashedCaseUpperUnderscoreSuffix() {
assertThat(toDashedCase("my_DLQ")).isEqualTo("my-d-l-q");
}
@Test
public void toDashedCaseUpperUnderscoreMiddle() {
assertThat(toDashedCase("some_DLQ_key")).isEqualTo("some-d-l-q-key");
}
@Test
public void toDashedCaseMultipleUnderscores() {
assertThat(toDashedCase("super___crazy")).isEqualTo("super---crazy");
}
@Test
public void toDashedCaseLowercase() {
assertThat(toDashedCase("lowercase")).isEqualTo("lowercase");
}
private String toDashedCase(String name) {
return ConfigurationMetadata.toDashedCase(name);
}
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.metadata;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link JsonMarshaller}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
public class JsonMarshallerTests {
@Test
public void marshallAndUnmarshal() throws Exception {
ConfigurationMetadata metadata = new ConfigurationMetadata();
metadata.add(ItemMetadata.newProperty("a", "b", StringBuffer.class.getName(),
InputStream.class.getName(), "sourceMethod", "desc", "x",
new ItemDeprecation("Deprecation comment", "b.c.d")));
metadata.add(ItemMetadata.newProperty("b.c.d", null, null, null, null, null, null,
null));
metadata.add(
ItemMetadata.newProperty("c", null, null, null, null, null, 123, null));
metadata.add(
ItemMetadata.newProperty("d", null, null, null, null, null, true, null));
metadata.add(ItemMetadata.newProperty("e", null, null, null, null, null,
new String[] { "y", "n" }, null));
metadata.add(ItemMetadata.newProperty("f", null, null, null, null, null,
new Boolean[] { true, false }, null));
metadata.add(ItemMetadata.newGroup("d", null, null, null));
metadata.add(ItemHint.newHint("a.b"));
metadata.add(ItemHint.newHint("c", new ItemHint.ValueHint(123, "hey"),
new ItemHint.ValueHint(456, null)));
metadata.add(new ItemHint("d", null,
Arrays.asList(
new ItemHint.ValueProvider("first",
Collections.<String, Object>singletonMap("target",
"foo")),
new ItemHint.ValueProvider("second", null))));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonMarshaller marshaller = new JsonMarshaller();
marshaller.write(metadata, outputStream);
ConfigurationMetadata read = marshaller
.read(new ByteArrayInputStream(outputStream.toByteArray()));
assertThat(read).has(Metadata.withProperty("a.b", StringBuffer.class)
.fromSource(InputStream.class).withDescription("desc")
.withDefaultValue("x").withDeprecation("Deprecation comment", "b.c.d"));
assertThat(read).has(Metadata.withProperty("b.c.d"));
assertThat(read).has(Metadata.withProperty("c").withDefaultValue(123));
assertThat(read).has(Metadata.withProperty("d").withDefaultValue(true));
assertThat(read).has(
Metadata.withProperty("e").withDefaultValue(new String[] { "y", "n" }));
assertThat(read).has(Metadata.withProperty("f")
.withDefaultValue(new Object[] { true, false }));
assertThat(read).has(Metadata.withGroup("d"));
assertThat(read).has(Metadata.withHint("a.b"));
assertThat(read).has(
Metadata.withHint("c").withValue(0, 123, "hey").withValue(1, 456, null));
assertThat(read).has(Metadata.withHint("d").withProvider("first", "target", "foo")
.withProvider("second"));
}
}

View File

@@ -0,0 +1,429 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.metadata;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.assertj.core.api.Condition;
import org.hamcrest.collection.IsMapContaining;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
import org.springframework.util.ObjectUtils;
/**
* AssertJ {@link Condition} to help test {@link ConfigurationMetadata}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
public final class Metadata {
private Metadata() {
}
public static MetadataItemCondition withGroup(String name) {
return new MetadataItemCondition(ItemType.GROUP, name);
}
public static MetadataItemCondition withGroup(String name, Class<?> type) {
return new MetadataItemCondition(ItemType.GROUP, name).ofType(type);
}
public static MetadataItemCondition withGroup(String name, String type) {
return new MetadataItemCondition(ItemType.GROUP, name).ofType(type);
}
public static MetadataItemCondition withProperty(String name) {
return new MetadataItemCondition(ItemType.PROPERTY, name);
}
public static MetadataItemCondition withProperty(String name, Class<?> type) {
return new MetadataItemCondition(ItemType.PROPERTY, name).ofType(type);
}
public static MetadataItemCondition withProperty(String name, String type) {
return new MetadataItemCondition(ItemType.PROPERTY, name).ofType(type);
}
public static Metadata.MetadataItemCondition withEnabledFlag(String key) {
return withProperty(key).ofType(Boolean.class);
}
public static MetadataHintCondition withHint(String name) {
return new MetadataHintCondition(name);
}
public static class MetadataItemCondition extends Condition<ConfigurationMetadata> {
private final ItemType itemType;
private final String name;
private final String type;
private final Class<?> sourceType;
private final String sourceMethod;
private final String description;
private final Object defaultValue;
private final ItemDeprecation deprecation;
public MetadataItemCondition(ItemType itemType, String name) {
this(itemType, name, null, null, null, null, null, null);
}
public MetadataItemCondition(ItemType itemType, String name, String type,
Class<?> sourceType, String sourceMethod, String description,
Object defaultValue, ItemDeprecation deprecation) {
this.itemType = itemType;
this.name = name;
this.type = type;
this.sourceType = sourceType;
this.sourceMethod = sourceMethod;
this.description = description;
this.defaultValue = defaultValue;
this.deprecation = deprecation;
describedAs(createDescription());
}
private String createDescription() {
StringBuilder description = new StringBuilder();
description.append("an item named '" + this.name + "'");
if (this.type != null) {
description.append(" with dataType:").append(this.type);
}
if (this.sourceType != null) {
description.append(" with sourceType:").append(this.sourceType);
}
if (this.sourceMethod != null) {
description.append(" with sourceMethod:").append(this.sourceMethod);
}
if (this.defaultValue != null) {
description.append(" with defaultValue:").append(this.defaultValue);
}
if (this.description != null) {
description.append(" with description:").append(this.description);
}
if (this.deprecation != null) {
description.append(" with deprecation:").append(this.deprecation);
}
return description.toString();
}
@Override
public boolean matches(ConfigurationMetadata value) {
ItemMetadata itemMetadata = getFirstItemWithName(value, this.name);
if (itemMetadata == null) {
return false;
}
if (this.type != null && !this.type.equals(itemMetadata.getType())) {
return false;
}
if (this.sourceType != null
&& !this.sourceType.getName().equals(itemMetadata.getSourceType())) {
return false;
}
if (this.sourceMethod != null
&& !this.sourceMethod.equals(itemMetadata.getSourceMethod())) {
return false;
}
if (this.defaultValue != null && !ObjectUtils
.nullSafeEquals(this.defaultValue, itemMetadata.getDefaultValue())) {
return false;
}
if (this.defaultValue == null && itemMetadata.getDefaultValue() != null) {
return false;
}
if (this.description != null
&& !this.description.equals(itemMetadata.getDescription())) {
return false;
}
if (this.deprecation == null && itemMetadata.getDeprecation() != null) {
return false;
}
if (this.deprecation != null
&& !this.deprecation.equals(itemMetadata.getDeprecation())) {
return false;
}
return true;
}
public MetadataItemCondition ofType(Class<?> dataType) {
return new MetadataItemCondition(this.itemType, this.name, dataType.getName(),
this.sourceType, this.sourceMethod, this.description,
this.defaultValue, this.deprecation);
}
public MetadataItemCondition ofType(String dataType) {
return new MetadataItemCondition(this.itemType, this.name, dataType,
this.sourceType, this.sourceMethod, this.description,
this.defaultValue, this.deprecation);
}
public MetadataItemCondition fromSource(Class<?> sourceType) {
return new MetadataItemCondition(this.itemType, this.name, this.type,
sourceType, this.sourceMethod, this.description, this.defaultValue,
this.deprecation);
}
public MetadataItemCondition fromSourceMethod(String sourceMethod) {
return new MetadataItemCondition(this.itemType, this.name, this.type,
this.sourceType, sourceMethod, this.description, this.defaultValue,
this.deprecation);
}
public MetadataItemCondition withDescription(String description) {
return new MetadataItemCondition(this.itemType, this.name, this.type,
this.sourceType, this.sourceMethod, description, this.defaultValue,
this.deprecation);
}
public MetadataItemCondition withDefaultValue(Object defaultValue) {
return new MetadataItemCondition(this.itemType, this.name, this.type,
this.sourceType, this.sourceMethod, this.description, defaultValue,
this.deprecation);
}
public MetadataItemCondition withDeprecation(String reason, String replacement) {
return withDeprecation(reason, replacement, null);
}
public MetadataItemCondition withDeprecation(String reason, String replacement,
String level) {
return new MetadataItemCondition(this.itemType, this.name, this.type,
this.sourceType, this.sourceMethod, this.description,
this.defaultValue, new ItemDeprecation(reason, replacement, level));
}
public MetadataItemCondition withNoDeprecation() {
return new MetadataItemCondition(this.itemType, this.name, this.type,
this.sourceType, this.sourceMethod, this.description,
this.defaultValue, null);
}
private ItemMetadata getFirstItemWithName(ConfigurationMetadata metadata,
String name) {
for (ItemMetadata item : metadata.getItems()) {
if (item.isOfItemType(this.itemType) && name.equals(item.getName())) {
return item;
}
}
return null;
}
}
public static class MetadataHintCondition extends Condition<ConfigurationMetadata> {
private final String name;
private final List<ItemHintValueCondition> valueConditions;
private final List<ItemHintProviderCondition> providerConditions;
public MetadataHintCondition(String name) {
this.name = name;
this.valueConditions = Collections.emptyList();
this.providerConditions = Collections.emptyList();
}
public MetadataHintCondition(String name,
List<ItemHintValueCondition> valueConditions,
List<ItemHintProviderCondition> providerConditions) {
this.name = name;
this.valueConditions = valueConditions;
this.providerConditions = providerConditions;
describedAs(createDescription());
}
private String createDescription() {
StringBuilder description = new StringBuilder();
description.append("a hints name '" + this.name + "'");
if (!this.valueConditions.isEmpty()) {
description.append(" with values:").append(this.valueConditions);
}
if (!this.providerConditions.isEmpty()) {
description.append(" with providers:").append(this.providerConditions);
}
return description.toString();
}
@Override
public boolean matches(ConfigurationMetadata metadata) {
ItemHint itemHint = getFirstHintWithName(metadata, this.name);
if (itemHint == null) {
return false;
}
return matches(itemHint, this.valueConditions)
&& matches(itemHint, this.providerConditions);
}
private boolean matches(ItemHint itemHint,
List<? extends Condition<ItemHint>> conditions) {
for (Condition<ItemHint> condition : conditions) {
if (!condition.matches(itemHint)) {
return false;
}
}
return true;
}
private ItemHint getFirstHintWithName(ConfigurationMetadata metadata,
String name) {
for (ItemHint hint : metadata.getHints()) {
if (name.equals(hint.getName())) {
return hint;
}
}
return null;
}
public MetadataHintCondition withValue(int index, Object value,
String description) {
return new MetadataHintCondition(this.name,
add(this.valueConditions,
new ItemHintValueCondition(index, value, description)),
this.providerConditions);
}
public MetadataHintCondition withProvider(String provider) {
return withProvider(this.providerConditions.size(), provider, null);
}
public MetadataHintCondition withProvider(String provider, String key,
Object value) {
return withProvider(this.providerConditions.size(), provider,
Collections.singletonMap(key, value));
}
public MetadataHintCondition withProvider(int index, String provider,
Map<String, Object> parameters) {
return new MetadataHintCondition(this.name, this.valueConditions,
add(this.providerConditions,
new ItemHintProviderCondition(index, provider, parameters)));
}
private <T> List<T> add(List<T> items, T item) {
List<T> result = new ArrayList<>(items);
result.add(item);
return result;
}
}
private static class ItemHintValueCondition extends Condition<ItemHint> {
private final int index;
private final Object value;
private final String description;
ItemHintValueCondition(int index, Object value, String description) {
this.index = index;
this.value = value;
this.description = description;
describedAs(createDescription());
}
private String createDescription() {
StringBuilder description = new StringBuilder();
description.append("value hint at index '" + this.index + "'");
if (this.value != null) {
description.append(" with value:").append(this.value);
}
if (this.description != null) {
description.append(" with description:").append(this.description);
}
return description.toString();
}
@Override
public boolean matches(ItemHint value) {
if (this.index + 1 > value.getValues().size()) {
return false;
}
ItemHint.ValueHint valueHint = value.getValues().get(this.index);
if (this.value != null && !this.value.equals(valueHint.getValue())) {
return false;
}
if (this.description != null
&& !this.description.equals(valueHint.getDescription())) {
return false;
}
return true;
}
}
private static class ItemHintProviderCondition extends Condition<ItemHint> {
private final int index;
private final String name;
private final Map<String, Object> parameters;
ItemHintProviderCondition(int index, String name,
Map<String, Object> parameters) {
this.index = index;
this.name = name;
this.parameters = parameters;
describedAs(createDescription());
}
public String createDescription() {
StringBuilder description = new StringBuilder();
description.append("value provider");
if (this.name != null) {
description.append(" with name:").append(this.name);
}
if (this.parameters != null) {
description.append(" with parameters:").append(this.parameters);
}
return description.toString();
}
@Override
public boolean matches(ItemHint hint) {
if (this.index + 1 > hint.getProviders().size()) {
return false;
}
ItemHint.ValueProvider valueProvider = hint.getProviders().get(this.index);
if (this.name != null && !this.name.equals(valueProvider.getName())) {
return false;
}
if (this.parameters != null) {
for (Map.Entry<String, Object> entry : this.parameters.entrySet()) {
if (!IsMapContaining.hasEntry(entry.getKey(), entry.getValue())
.matches(valueProvider.getParameters())) {
return false;
}
}
}
return true;
}
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor.metadata;
/**
* {@link JsonConverter} for use in tests.
*
* @author Phillip Webb
*/
public class TestJsonConverter extends JsonConverter {
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Alternative to Spring Boot's {@code @ConfigurationProperties} for testing (removes the
* need for a dependency on the real annotation).
*
* @author Stephane Nicoll
* @author Phillip Webb
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {
String value() default "";
String prefix() default "";
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample;
public enum DefaultEnablement {
ENABLED, DISABLED, NEUTRAL
}

View File

@@ -0,0 +1,52 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates that a getter in a {@link ConfigurationProperties} object is deprecated. This
* annotation has no bearing on the actual binding processes, but it is used by the
* {@code spring-boot-configuration-processor} to add deprecation meta-data.
* <p>
* This annotation <strong>must</strong> be used on the getter of the deprecated element.
*
* @author Phillip Webb
* @since 1.3.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DeprecatedConfigurationProperty {
/**
* The reason for the deprecation.
* @return the deprecation reason
*/
String reason() default "";
/**
* The field that should be used instead (if any).
* @return the replacement field
*/
String replacement() default "";
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Alternative to Spring Boot's {@code @Endpoint} for testing (removes the need for a
* dependency on the real annotation).
*
* @author Stephane Nicoll
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Endpoint {
String id();
DefaultEnablement defaultEnablement() default DefaultEnablement.NEUTRAL;
EndpointExposure[] exposure() default {};
}

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample;
public enum EndpointExposure {
JMX,
WEB
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Alternative to Spring Boot's {@code @NestedConfigurationProperty} for testing (removes
* the need for a dependency on the real annotation).
*
* @author Stephane Nicoll
* @author Phillip Webb
* @since 1.2.0
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NestedConfigurationProperty {
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.endpoint;
import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.boot.configurationsample.Endpoint;
/**
* An endpoint with additional custom properties.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "customprops")
@ConfigurationProperties("endpoints.customprops")
public class CustomPropertiesEndpoint {
private String name = "test";
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.endpoint;
import org.springframework.boot.configurationsample.DefaultEnablement;
import org.springframework.boot.configurationsample.Endpoint;
/**
* An endpoint that is disabled unless configured explicitly.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "disabled", defaultEnablement = DefaultEnablement.DISABLED)
public class DisabledEndpoint {
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.endpoint;
import org.springframework.boot.configurationsample.DefaultEnablement;
import org.springframework.boot.configurationsample.Endpoint;
/**
* An endpoint that is enabled unless configured explicitly..
*
* @author Stephane Nicoll
*/
@Endpoint(id = "enabled", defaultEnablement = DefaultEnablement.ENABLED)
public class EnabledEndpoint {
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.endpoint;
import org.springframework.boot.configurationsample.Endpoint;
import org.springframework.boot.configurationsample.EndpointExposure;
/**
* An endpoint that only exposes a JMX MBean.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "jmx", exposure = EndpointExposure.JMX)
public class OnlyJmxEndpoint {
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.endpoint;
import org.springframework.boot.configurationsample.Endpoint;
import org.springframework.boot.configurationsample.EndpointExposure;
/**
* An endpoints that only exposes a web endpoint.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "web", exposure = EndpointExposure.WEB)
public class OnlyWebEndpoint {
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.endpoint;
import org.springframework.boot.configurationsample.Endpoint;
/**
* A simple endpoint with no default override.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "simple")
public class SimpleEndpoint {
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.endpoint.incremental;
import org.springframework.boot.configurationsample.Endpoint;
/**
* An endpoint that is enabled by default.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "incremental")
public class IncrementalEndpoint {
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.endpoint.incremental;
import org.springframework.boot.configurationsample.Endpoint;
import org.springframework.boot.configurationsample.EndpointExposure;
/**
* An endpoint that only exposes a JMX MBean.
*
* @author Stephane Nicoll
*/
@Endpoint(id = "incremental", exposure = EndpointExposure.JMX)
public class IncrementalJmxEndpoint {
}

View File

@@ -0,0 +1,110 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
@SuppressWarnings("unused")
@ConfigurationProperties
public class FieldValues {
private static final String STRING_CONST = "c";
private static final boolean BOOLEAN_CONST = true;
private static final Boolean BOOLEAN_OBJ_CONST = true;
private static final int INTEGER_CONST = 2;
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";
private String stringNone;
private String stringConst = STRING_CONST;
private boolean bool = true;
private boolean boolNone;
private boolean boolConst = BOOLEAN_CONST;
private Boolean boolObject = Boolean.TRUE;
private Boolean boolObjectNone;
private Boolean boolObjectConst = BOOLEAN_OBJ_CONST;
private int integer = 1;
private int integerNone;
private int integerConst = INTEGER_CONST;
private Integer integerObject = 3;
private Integer integerObjectNone;
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;
private Object objectConst = STRING_CONST;
private Object objectInstance = new StringBuffer();
private String[] stringArray = new String[] { "FOO", "BAR" };
private String[] stringArrayNone;
private String[] stringEmptyArray = new String[0];
private String[] stringArrayConst = STRING_ARRAY_CONST;
private String[] stringArrayConstElements = new String[] { STRING_CONST };
private Integer[] integerArray = new Integer[] { 42, 24 };
private FieldValues[] unknownArray = new FieldValues[] { new FieldValues() };
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.incremental;
import org.springframework.boot.configurationsample.ConfigurationProperties;
@ConfigurationProperties("bar")
public class BarProperties {
private String name;
private String description;
/**
* A nice counter description.
*/
private Integer counter = 0;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getCounter() {
return this.counter;
}
public void setCounter(Integer counter) {
this.counter = counter;
}
}

View File

@@ -0,0 +1,57 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.incremental;
import org.springframework.boot.configurationsample.ConfigurationProperties;
@ConfigurationProperties("foo")
public class FooProperties {
private String name;
private String description;
/**
* A nice counter description.
*/
private Integer counter = 0;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getCounter() {
return this.counter;
}
public void setCounter(Integer counter) {
this.counter = counter;
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.incremental;
import org.springframework.boot.configurationsample.ConfigurationProperties;
@ConfigurationProperties("bar")
public class RenamedBarProperties {
private String name;
private String description;
/**
* A nice counter description.
*/
private Integer counter = 0;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getCounter() {
return this.counter;
}
public void setCounter(Integer counter) {
this.counter = counter;
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.lombok;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Configuration properties using lombok @Getter/@Setter at field level.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties(prefix = "explicit")
public class LombokExplicitProperties {
@Getter
private final String id = "super-id";
/**
* Name description.
*/
@Getter
@Setter
private String name;
@Getter
@Setter
private String description;
@Getter
@Setter
private Integer counter;
@Deprecated
@Getter
@Setter
private Integer number = 0;
@Getter
private final List<String> items = new ArrayList<>();
// Should be ignored if no annotation is set
@SuppressWarnings("unused")
private String ignored;
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.lombok;
import lombok.Data;
import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.boot.configurationsample.NestedConfigurationProperty;
/**
* Demonstrate the auto-detection of inner config classes using Lombok.
*
* @author Stephane Nicoll
*/
@Data
@ConfigurationProperties(prefix = "config")
@SuppressWarnings("unused")
public class LombokInnerClassProperties {
private final Foo first = new Foo();
private Foo second = new Foo();
@NestedConfigurationProperty
private final SimpleLombokPojo third = new SimpleLombokPojo();
private Fourth fourth;
@Data
public static class Foo {
private String name;
private final Bar bar = new Bar();
@Data
public static class Bar {
private String name;
}
}
public enum Fourth {
YES, NO
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.lombok;
import lombok.Data;
import org.springframework.boot.configurationsample.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "config")
@SuppressWarnings("unused")
public class LombokInnerClassWithGetterProperties {
private final Foo first = new Foo();
public Foo getFirst() {
return this.first;
}
@Data
public static class Foo {
private String name;
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.lombok;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Configuration properties using lombok @Data.
*
* @author Stephane Nicoll
*/
@Data
@ConfigurationProperties(prefix = "data")
@SuppressWarnings("unused")
public class LombokSimpleDataProperties {
private final String id = "super-id";
/**
* Name description.
*/
private String name;
private String description;
private Integer counter;
@Deprecated
private Integer number = 0;
private final List<String> items = new ArrayList<>();
private final String ignored = "foo";
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.lombok;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Configuration properties using lombok @Getter/@Setter at class level.
*
* @author Stephane Nicoll
*/
@Getter
@Setter
@ConfigurationProperties(prefix = "simple")
@SuppressWarnings("unused")
public class LombokSimpleProperties {
private final String id = "super-id";
/**
* Name description.
*/
private String name;
private String description;
private Integer counter;
@Deprecated
private Integer number = 0;
private final List<String> items = new ArrayList<>();
private final String ignored = "foo";
}

View File

@@ -0,0 +1,32 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.lombok;
import lombok.Data;
/**
* Lombok POJO for use with samples.
*
* @author Stephane Nicoll
*/
@Data
@SuppressWarnings("unused")
public class SimpleLombokPojo {
private int value;
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.method;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Sample for testing method configuration with deprecated class.
*
* @author Stephane Nicoll
*/
@Deprecated
public class DeprecatedClassMethodConfig {
@ConfigurationProperties(prefix = "foo")
public Foo foo() {
return new Foo();
}
public static class Foo {
private String name;
private boolean flag;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public boolean isFlag() {
return this.flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.method;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Sample for testing deprecated method configuration.
*
* @author Stephane Nicoll
*/
public class DeprecatedMethodConfig {
@ConfigurationProperties(prefix = "foo")
@Deprecated
public Foo foo() {
return new Foo();
}
public static class Foo {
private String name;
private boolean flag;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public boolean isFlag() {
return this.flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.method;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Sample for testing method configuration.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties("something")
public class EmptyTypeMethodConfig {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@ConfigurationProperties("something")
public Foo foo() {
return new Foo();
}
public static class Foo {
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.method;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Sample for testing invalid method configuration.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties(prefix = "something")
public class InvalidMethodConfig {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@ConfigurationProperties(prefix = "invalid")
InvalidMethodConfig foo() {
return new InvalidMethodConfig();
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.method;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Sample for testing mixed method and class configuration.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties("conflict")
public class MethodAndClassConfig {
private String value;
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
@ConfigurationProperties(prefix = "conflict")
public Foo foo() {
return new Foo();
}
public static class Foo {
private String name;
private boolean flag;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public boolean isFlag() {
return this.flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.method;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Sample for testing simple method configuration.
*
* @author Stephane Nicoll
*/
public class SimpleMethodConfig {
@ConfigurationProperties(prefix = "foo")
public Foo foo() {
return new Foo();
}
public static class Foo {
private String name;
private boolean flag;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public boolean isFlag() {
return this.flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.simple;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Class with nested configuration properties.
*
* @author Hrishikesh Joshi
*/
public class ClassWithNestedProperties {
public static class NestedParentClass {
private int parentClassProperty = 10;
public int getParentClassProperty() {
return this.parentClassProperty;
}
public void setParentClassProperty(int parentClassProperty) {
this.parentClassProperty = parentClassProperty;
}
}
@ConfigurationProperties(prefix = "nestedChildProps")
public static class NestedChildClass extends NestedParentClass {
private int childClassProperty = 20;
public int getChildClassProperty() {
return this.childClassProperty;
}
public void setChildClassProperty(int childClassProperty) {
this.childClassProperty = childClassProperty;
}
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.simple;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Deprecated configuration properties.
*
* @author Stephane Nicoll
*/
@Deprecated
@ConfigurationProperties(prefix = "deprecated")
public class DeprecatedProperties {
private String name;
private String description;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.simple;
import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.boot.configurationsample.DeprecatedConfigurationProperty;
/**
* Configuration properties with a single deprecated element.
*
* @author Phillip Webb
*/
@ConfigurationProperties("singledeprecated")
public class DeprecatedSingleProperty {
private String newName;
@Deprecated
@DeprecatedConfigurationProperty(reason = "renamed", replacement = "singledeprecated.new-name")
public String getName() {
return getNewName();
}
@Deprecated
public void setName(String name) {
setNewName(name);
}
public String getNewName() {
return this.newName;
}
public void setNewName(String newName) {
this.newName = newName;
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.simple;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Configuration properties with inherited values.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties(prefix = "hierarchical")
public class HierarchicalProperties extends HierarchicalPropertiesParent {
private String third;
public String getThird() {
return this.third;
}
public void setThird(String third) {
this.third = third;
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.simple;
/**
* Grandparent for {@link HierarchicalProperties}.
*
* @author Stephane Nicoll
*/
public abstract class HierarchicalPropertiesGrandparent {
private String first;
public String getFirst() {
return this.first;
}
public void setFirst(String first) {
this.first = first;
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.simple;
/**
* Parent for {@link HierarchicalProperties}.
*
* @author Stephane Nicoll
*/
public abstract class HierarchicalPropertiesParent
extends HierarchicalPropertiesGrandparent {
private String second;
public String getSecond() {
return this.second;
}
public void setSecond(String second) {
this.second = second;
}
// Useless override
@Override
public String getFirst() {
return super.getFirst();
}
@Override
public void setFirst(String first) {
super.setFirst(first);
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.simple;
/**
* This has no annotation on purpose to check that no meta-data is generated.
*
* @author Stephane Nicoll
*/
public class NotAnnotated {
}

View File

@@ -0,0 +1,84 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.simple;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Properties with collections.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties(prefix = "collection")
public class SimpleCollectionProperties {
private Map<Integer, String> integersToNames;
private Collection<Long> longs;
private List<Float> floats;
private final Map<String, Integer> namesToIntegers = new HashMap<>();
private final Collection<Byte> bytes = new LinkedHashSet<>();
private final List<Double> doubles = new ArrayList<>();
public Map<Integer, String> getIntegersToNames() {
return this.integersToNames;
}
public void setIntegersToNames(Map<Integer, String> integersToNames) {
this.integersToNames = integersToNames;
}
public Collection<Long> getLongs() {
return this.longs;
}
public void setLongs(Collection<Long> longs) {
this.longs = longs;
}
public List<Float> getFloats() {
return this.floats;
}
public void setFloats(List<Float> floats) {
this.floats = floats;
}
public Map<String, Integer> getNamesToIntegers() {
return this.namesToIntegers;
}
public Collection<Byte> getBytes() {
return this.bytes;
}
public List<Double> getDoubles() {
return this.doubles;
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.simple;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Properties with a simple prefix.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties("simple")
public class SimplePrefixValueProperties {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.simple;
import java.beans.FeatureDescriptor;
import java.util.Comparator;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Simple properties.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties(prefix = "simple")
public class SimpleProperties {
/**
* The name of this simple properties.
*/
private String theName = "boot";
// isFlag is also detected
/**
* A simple flag.
*/
private boolean flag;
// An interface can still be injected because it might have a converter
private Comparator<?> comparator;
// There is only a getter on this instance but we don't know what to do with it ->
// ignored
private FeatureDescriptor featureDescriptor;
// There is only a setter on this "simple" property --> ignored
@SuppressWarnings("unused")
private Long counter;
// There is only a getter on this "simple" property --> ignored
private Integer size;
public String getTheName() {
return this.theName;
}
@Deprecated
public void setTheName(String name) {
this.theName = name;
}
@Deprecated
public boolean isFlag() {
return this.flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public Comparator<?> getComparator() {
return this.comparator;
}
public void setComparator(Comparator<?> comparator) {
this.comparator = comparator;
}
public FeatureDescriptor getFeatureDescriptor() {
return this.featureDescriptor;
}
public void setCounter(Long counter) {
this.counter = counter;
}
public Integer getSize() {
return this.size;
}
}

View File

@@ -0,0 +1,199 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.simple;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Expose simple types to make sure these are detected properly.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties(prefix = "simple.type")
public class SimpleTypeProperties {
private String myString;
private Byte myByte;
private byte myPrimitiveByte;
private Character myChar;
private char myPrimitiveChar;
private Boolean myBoolean;
private boolean myPrimitiveBoolean;
private Short myShort;
private short myPrimitiveShort;
private Integer myInteger;
private int myPrimitiveInteger;
private Long myLong;
private long myPrimitiveLong;
private Double myDouble;
private double myPrimitiveDouble;
private Float myFloat;
private float myPrimitiveFloat;
public String getMyString() {
return this.myString;
}
public void setMyString(String myString) {
this.myString = myString;
}
public Byte getMyByte() {
return this.myByte;
}
public void setMyByte(Byte myByte) {
this.myByte = myByte;
}
public byte getMyPrimitiveByte() {
return this.myPrimitiveByte;
}
public void setMyPrimitiveByte(byte myPrimitiveByte) {
this.myPrimitiveByte = myPrimitiveByte;
}
public Character getMyChar() {
return this.myChar;
}
public void setMyChar(Character myChar) {
this.myChar = myChar;
}
public char getMyPrimitiveChar() {
return this.myPrimitiveChar;
}
public void setMyPrimitiveChar(char myPrimitiveChar) {
this.myPrimitiveChar = myPrimitiveChar;
}
public Boolean getMyBoolean() {
return this.myBoolean;
}
public void setMyBoolean(Boolean myBoolean) {
this.myBoolean = myBoolean;
}
public boolean isMyPrimitiveBoolean() {
return this.myPrimitiveBoolean;
}
public void setMyPrimitiveBoolean(boolean myPrimitiveBoolean) {
this.myPrimitiveBoolean = myPrimitiveBoolean;
}
public Short getMyShort() {
return this.myShort;
}
public void setMyShort(Short myShort) {
this.myShort = myShort;
}
public short getMyPrimitiveShort() {
return this.myPrimitiveShort;
}
public void setMyPrimitiveShort(short myPrimitiveShort) {
this.myPrimitiveShort = myPrimitiveShort;
}
public Integer getMyInteger() {
return this.myInteger;
}
public void setMyInteger(Integer myInteger) {
this.myInteger = myInteger;
}
public int getMyPrimitiveInteger() {
return this.myPrimitiveInteger;
}
public void setMyPrimitiveInteger(int myPrimitiveInteger) {
this.myPrimitiveInteger = myPrimitiveInteger;
}
public Long getMyLong() {
return this.myLong;
}
public void setMyLong(Long myLong) {
this.myLong = myLong;
}
public long getMyPrimitiveLong() {
return this.myPrimitiveLong;
}
public void setMyPrimitiveLong(long myPrimitiveLong) {
this.myPrimitiveLong = myPrimitiveLong;
}
public Double getMyDouble() {
return this.myDouble;
}
public void setMyDouble(Double myDouble) {
this.myDouble = myDouble;
}
public double getMyPrimitiveDouble() {
return this.myPrimitiveDouble;
}
public void setMyPrimitiveDouble(double myPrimitiveDouble) {
this.myPrimitiveDouble = myPrimitiveDouble;
}
public Float getMyFloat() {
return this.myFloat;
}
public void setMyFloat(Float myFloat) {
this.myFloat = myFloat;
}
public float getMyPrimitiveFloat() {
return this.myPrimitiveFloat;
}
public void setMyPrimitiveFloat(float myPrimitiveFloat) {
this.myPrimitiveFloat = myPrimitiveFloat;
}
}

View File

@@ -0,0 +1,52 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.specific;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Demonstrate the use of boxing/unboxing. Even if the type does not strictly match, it
* should still be detected.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties("boxing")
public class BoxingPojo {
private boolean flag;
private Integer counter;
public boolean isFlag() {
return this.flag;
}
// Setter use Boolean
public void setFlag(Boolean flag) {
this.flag = flag;
}
public Integer getCounter() {
return this.counter;
}
// Setter use int
public void setCounter(int counter) {
this.counter = counter;
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.specific;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Sample with builder style setters.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties(prefix = "builder")
public class BuilderPojo {
private String name;
public String getName() {
return this.name;
}
public BuilderPojo setName(String name) {
this.name = name;
return this;
}
}

View File

@@ -0,0 +1,60 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.specific;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Demonstrate that an unrelated setter is not taken into account to detect the deprecated
* flag.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties("not.deprecated")
public class DeprecatedUnrelatedMethodPojo {
private Integer counter;
private boolean flag;
public Integer getCounter() {
return this.counter;
}
public void setCounter(Integer counter) {
this.counter = counter;
}
@Deprecated
public void setCounter(String counterAsString) {
this.counter = Integer.valueOf(counterAsString);
}
public boolean isFlag() {
return this.flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
@Deprecated
public void setFlag(Boolean flag) {
this.flag = flag;
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.specific;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Test that the same type can be registered several times if the prefix is different.
*
* @author Stephane Nicoll
*/
public class DoubleRegistrationProperties {
@ConfigurationProperties("one")
public SimplePojo one() {
return new SimplePojo();
}
@ConfigurationProperties("two")
public SimplePojo two() {
return new SimplePojo();
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.specific;
import java.io.PrintWriter;
import java.io.Writer;
import javax.sql.DataSource;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Sample config with types that should not be added to the meta-data as we have no way to
* bind them from simple strings.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties(prefix = "excluded")
public class ExcludedTypesPojo {
private String name;
private ClassLoader classLoader;
private DataSource dataSource;
private PrintWriter printWriter;
private Writer writer;
private Writer[] writerArray;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public ClassLoader getClassLoader() {
return this.classLoader;
}
public void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
public DataSource getDataSource() {
return this.dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public PrintWriter getPrintWriter() {
return this.printWriter;
}
public void setPrintWriter(PrintWriter printWriter) {
this.printWriter = printWriter;
}
public Writer getWriter() {
return this.writer;
}
public void setWriter(Writer writer) {
this.writer = writer;
}
public Writer[] getWriterArray() {
return this.writerArray;
}
public void setWriterArray(Writer[] writerArray) {
this.writerArray = writerArray;
}
}

View File

@@ -0,0 +1,108 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.specific;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.boot.configurationsample.NestedConfigurationProperty;
/**
* Demonstrate that only relevant generics are stored in the metadata.
*
* @param <T> the type of the config
* @author Stephane Nicoll
*/
@ConfigurationProperties("generic")
public class GenericConfig<T> {
private final Foo foo = new Foo();
public Foo getFoo() {
return this.foo;
}
public static class Foo {
private String name;
@NestedConfigurationProperty
private final Bar<String> bar = new Bar<>();
private final Map<String, Bar<Integer>> stringToBar = new HashMap<>();
private final Map<String, Integer> stringToInteger = new HashMap<>();
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Bar<String> getBar() {
return this.bar;
}
public Map<String, Bar<Integer>> getStringToBar() {
return this.stringToBar;
}
public Map<String, Integer> getStringToInteger() {
return this.stringToInteger;
}
}
public static class Bar<U> {
private String name;
@NestedConfigurationProperty
private final Biz<String> biz = new Biz<>();
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Biz<String> getBiz() {
return this.biz;
}
public static class Biz<V> {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.specific;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Demonstrate that a method that exposes a root group within an annotated class is
* ignored as it should.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties("specific")
public class InnerClassAnnotatedGetterConfig {
private String value;
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
@ConfigurationProperties("foo")
public Foo getFoo() {
return new Foo();
}
public static class Foo {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.specific;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Demonstrate inner classes end up in metadata regardless of position in hierarchy and
* without the use of
* {@link org.springframework.boot.configurationsample.NestedConfigurationProperty}.
*
* @author Madhura Bhave
*/
@ConfigurationProperties(prefix = "config")
public class InnerClassHierarchicalProperties {
private Foo foo;
public Foo getFoo() {
return this.foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
public static class Foo {
private Bar bar;
public Bar getBar() {
return this.bar;
}
public void setBar(Bar bar) {
this.bar = bar;
}
public static class Baz {
private String blah;
public String getBlah() {
return this.blah;
}
public void setBlah(String blah) {
this.blah = blah;
}
}
}
public static class Bar {
private String bling;
private Foo.Baz baz;
public String getBling() {
return this.bling;
}
public void setBling(String foo) {
this.bling = foo;
}
public Foo.Baz getBaz() {
return this.baz;
}
public void setBaz(Foo.Baz baz) {
this.baz = baz;
}
}
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.specific;
import org.springframework.boot.configurationsample.ConfigurationProperties;
import org.springframework.boot.configurationsample.NestedConfigurationProperty;
/**
* Demonstrate the auto-detection of inner config classes.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties(prefix = "config")
public class InnerClassProperties {
private final Foo first = new Foo();
private Foo second = new Foo();
@NestedConfigurationProperty
private final SimplePojo third = new SimplePojo();
private Fourth fourth;
public Foo getFirst() {
return this.first;
}
public Foo getTheSecond() {
return this.second;
}
public void setTheSecond(Foo second) {
this.second = second;
}
public SimplePojo getThird() {
return this.third;
}
public Fourth getFourth() {
return this.fourth;
}
public void setFourth(Fourth fourth) {
this.fourth = fourth;
}
public static class Foo {
private String name;
private final Bar bar = new Bar();
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Bar getBar() {
return this.bar;
}
public static class Bar {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}
public enum Fourth {
YES, NO
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.specific;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Sample with a simple inner class config.
*
* @author Stephane Nicoll
*/
public class InnerClassRootConfig {
@ConfigurationProperties(prefix = "config")
static class Config {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.specific;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Demonstrates that invalid accessors are ignored.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties(prefix = "config")
public class InvalidAccessorProperties {
private String name;
private boolean flag;
public void set(String name) {
this.name = name;
}
public String get() {
return this.name;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public boolean is() {
return this.flag;
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.specific;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Test that compilation fails if the same type is registered twice with the same prefix.
*
* @author Stephane Nicoll
*/
public class InvalidDoubleRegistrationProperties {
@ConfigurationProperties("foo")
public Foo foo() {
return new Foo();
}
@ConfigurationProperties("foo")
public static class Foo {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.specific;
/**
* POJO for use with samples.
*
* @author Stephane Nicoll
*/
public class SimplePojo {
private int value;
public int getValue() {
return this.value;
}
public void setValue(int value) {
this.value = value;
}
}

View File

@@ -0,0 +1,9 @@
private String extra;
public String getExtra() {
return extra;
}
public void setExtra(String extra) {
this.extra = extra;
}