Merge branch '1.5.x'
This commit is contained in:
@@ -17,7 +17,6 @@
|
||||
package org.springframework.boot.configurationprocessor;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -134,7 +133,12 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
|
||||
}
|
||||
}
|
||||
if (roundEnv.processingOver()) {
|
||||
writeMetaData();
|
||||
try {
|
||||
writeMetaData();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException("Failed to write metadata", ex);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -390,16 +394,11 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
|
||||
return values;
|
||||
}
|
||||
|
||||
protected ConfigurationMetadata writeMetaData() {
|
||||
protected ConfigurationMetadata writeMetaData() throws Exception {
|
||||
ConfigurationMetadata metadata = this.metadataCollector.getMetadata();
|
||||
metadata = mergeAdditionalMetadata(metadata);
|
||||
if (!metadata.getItems().isEmpty()) {
|
||||
try {
|
||||
this.metadataStore.writeMetadata(metadata);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException("Failed to write metadata", ex);
|
||||
}
|
||||
this.metadataStore.writeMetadata(metadata);
|
||||
return metadata;
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -27,8 +27,6 @@ import javax.tools.Diagnostic;
|
||||
import javax.tools.FileObject;
|
||||
import javax.tools.StandardLocation;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
|
||||
import org.springframework.boot.configurationprocessor.metadata.InvalidConfigurationMetadataException;
|
||||
import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller;
|
||||
@@ -87,7 +85,7 @@ public class MetadataStore {
|
||||
catch (IOException ex) {
|
||||
return null;
|
||||
}
|
||||
catch (JSONException ex) {
|
||||
catch (Exception ex) {
|
||||
throw new InvalidConfigurationMetadataException(
|
||||
"Invalid additional meta-data in '" + METADATA_PATH + "': "
|
||||
+ ex.getMessage(),
|
||||
|
||||
@@ -20,15 +20,12 @@ 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.ListIterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Configuration meta-data.
|
||||
*
|
||||
@@ -46,18 +43,18 @@ public class ConfigurationMetadata {
|
||||
SEPARATORS = Collections.unmodifiableSet(new HashSet<Character>(chars));
|
||||
}
|
||||
|
||||
private final MultiValueMap<String, ItemMetadata> items;
|
||||
private final Map<String, List<ItemMetadata>> items;
|
||||
|
||||
private final MultiValueMap<String, ItemHint> hints;
|
||||
private final Map<String, List<ItemHint>> hints;
|
||||
|
||||
public ConfigurationMetadata() {
|
||||
this.items = new LinkedMultiValueMap<String, ItemMetadata>();
|
||||
this.hints = new LinkedMultiValueMap<String, ItemHint>();
|
||||
this.items = new LinkedHashMap<String, List<ItemMetadata>>();
|
||||
this.hints = new LinkedHashMap<String, List<ItemHint>>();
|
||||
}
|
||||
|
||||
public ConfigurationMetadata(ConfigurationMetadata metadata) {
|
||||
this.items = new LinkedMultiValueMap<String, ItemMetadata>(metadata.items);
|
||||
this.hints = new LinkedMultiValueMap<String, ItemHint>(metadata.hints);
|
||||
this.items = new LinkedHashMap<String, List<ItemMetadata>>(metadata.items);
|
||||
this.hints = new LinkedHashMap<String, List<ItemHint>>(metadata.hints);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,7 +62,7 @@ public class ConfigurationMetadata {
|
||||
* @param itemMetadata the meta-data to add
|
||||
*/
|
||||
public void add(ItemMetadata itemMetadata) {
|
||||
this.items.add(itemMetadata.getName(), itemMetadata);
|
||||
add(this.items, itemMetadata.getName(), itemMetadata);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,7 +70,7 @@ public class ConfigurationMetadata {
|
||||
* @param itemHint the item hint to add
|
||||
*/
|
||||
public void add(ItemHint itemHint) {
|
||||
this.hints.add(itemHint.getName(), itemHint);
|
||||
add(this.hints, itemHint.getName(), itemHint);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,13 +128,22 @@ public class ConfigurationMetadata {
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.items.add(metadata.getName(), metadata);
|
||||
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<V>();
|
||||
map.put(key, values);
|
||||
}
|
||||
values.add(value);
|
||||
}
|
||||
|
||||
private ItemMetadata findMatchingItemMetadata(ItemMetadata metadata) {
|
||||
List<ItemMetadata> candidates = this.items.get(metadata.getName());
|
||||
if (CollectionUtils.isEmpty(candidates)) {
|
||||
if (candidates == null || candidates.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
ListIterator<ItemMetadata> it = candidates.listIterator();
|
||||
@@ -150,14 +156,20 @@ public class ConfigurationMetadata {
|
||||
return candidates.get(0);
|
||||
}
|
||||
for (ItemMetadata candidate : candidates) {
|
||||
if (ObjectUtils.nullSafeEquals(candidate.getSourceType(),
|
||||
metadata.getSourceType())) {
|
||||
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);
|
||||
@@ -185,8 +197,7 @@ public class ConfigurationMetadata {
|
||||
return dashed.toString().toLowerCase();
|
||||
}
|
||||
|
||||
private static <T extends Comparable<T>> List<T> flattenValues(
|
||||
MultiValueMap<?, T> map) {
|
||||
private static <T extends Comparable<T>> List<T> flattenValues(Map<?, List<T>> map) {
|
||||
List<T> content = new ArrayList<T>();
|
||||
for (List<T> values : map.values()) {
|
||||
content.addAll(values);
|
||||
|
||||
@@ -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.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<String>();
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* 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 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.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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,18 +20,14 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Array;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
|
||||
@@ -51,136 +47,26 @@ public class JsonMarshaller {
|
||||
|
||||
public void write(ConfigurationMetadata metadata, OutputStream outputStream)
|
||||
throws IOException {
|
||||
JSONObject object = new JSONOrderedObject();
|
||||
object.put("groups", toJsonArray(metadata, ItemType.GROUP));
|
||||
object.put("properties", toJsonArray(metadata, ItemType.PROPERTY));
|
||||
object.put("hints", toJsonArray(metadata.getHints()));
|
||||
outputStream.write(object.toString(2).getBytes(UTF_8));
|
||||
}
|
||||
|
||||
private JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
for (ItemMetadata item : metadata.getItems()) {
|
||||
if (item.isOfItemType(itemType)) {
|
||||
jsonArray.put(toJsonObject(item));
|
||||
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;
|
||||
}
|
||||
}
|
||||
return jsonArray;
|
||||
}
|
||||
|
||||
private JSONArray toJsonArray(Collection<ItemHint> hints) {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
for (ItemHint hint : hints) {
|
||||
jsonArray.put(toJsonObject(hint));
|
||||
}
|
||||
return jsonArray;
|
||||
}
|
||||
|
||||
private JSONObject toJsonObject(ItemMetadata item) {
|
||||
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.getReason() != null) {
|
||||
deprecationJsonObject.put("reason", deprecation.getReason());
|
||||
if (ex instanceof RuntimeException) {
|
||||
throw (RuntimeException) ex;
|
||||
}
|
||||
if (deprecation.getReplacement() != null) {
|
||||
deprecationJsonObject.put("replacement", deprecation.getReplacement());
|
||||
}
|
||||
jsonObject.put("deprecation", deprecationJsonObject);
|
||||
}
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
private JSONObject toJsonObject(ItemHint hint) {
|
||||
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) {
|
||||
JSONArray values = new JSONArray();
|
||||
for (ItemHint.ValueHint value : hint.getValues()) {
|
||||
values.put(getItemHintValue(value));
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
private JSONObject getItemHintValue(ItemHint.ValueHint value) {
|
||||
JSONObject result = new JSONOrderedObject();
|
||||
putHintValue(result, value.getValue());
|
||||
putIfPresent(result, "description", value.getDescription());
|
||||
return result;
|
||||
}
|
||||
|
||||
private JSONArray getItemHintProviders(ItemHint hint) {
|
||||
JSONArray providers = new JSONArray();
|
||||
for (ItemHint.ValueProvider provider : hint.getProviders()) {
|
||||
providers.put(getItemHintProvider(provider));
|
||||
}
|
||||
return providers;
|
||||
}
|
||||
|
||||
private JSONObject getItemHintProvider(ItemHint.ValueProvider provider) {
|
||||
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) {
|
||||
if (value != null) {
|
||||
jsonObject.put(name, value);
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void putHintValue(JSONObject jsonObject, Object value) {
|
||||
Object hintValue = extractItemValue(value);
|
||||
jsonObject.put("value", hintValue);
|
||||
}
|
||||
|
||||
private void putDefaultValue(JSONObject jsonObject, Object value) {
|
||||
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;
|
||||
}
|
||||
|
||||
public ConfigurationMetadata read(InputStream inputStream) throws IOException {
|
||||
public ConfigurationMetadata read(InputStream inputStream) throws Exception {
|
||||
ConfigurationMetadata metadata = new ConfigurationMetadata();
|
||||
JSONObject object = new JSONObject(toString(inputStream));
|
||||
JSONArray groups = object.optJSONArray("groups");
|
||||
@@ -205,7 +91,8 @@ public class JsonMarshaller {
|
||||
return metadata;
|
||||
}
|
||||
|
||||
private ItemMetadata toItemMetadata(JSONObject object, ItemType itemType) {
|
||||
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);
|
||||
@@ -217,7 +104,7 @@ public class JsonMarshaller {
|
||||
description, defaultValue, deprecation);
|
||||
}
|
||||
|
||||
private ItemDeprecation toItemDeprecation(JSONObject object) {
|
||||
private ItemDeprecation toItemDeprecation(JSONObject object) throws Exception {
|
||||
if (object.has("deprecation")) {
|
||||
JSONObject deprecationJsonObject = object.getJSONObject("deprecation");
|
||||
ItemDeprecation deprecation = new ItemDeprecation();
|
||||
@@ -229,7 +116,7 @@ public class JsonMarshaller {
|
||||
return (object.optBoolean("deprecated") ? new ItemDeprecation() : null);
|
||||
}
|
||||
|
||||
private ItemHint toItemHint(JSONObject object) {
|
||||
private ItemHint toItemHint(JSONObject object) throws Exception {
|
||||
String name = object.getString("name");
|
||||
List<ItemHint.ValueHint> values = new ArrayList<ItemHint.ValueHint>();
|
||||
if (object.has("values")) {
|
||||
@@ -248,19 +135,19 @@ public class JsonMarshaller {
|
||||
return new ItemHint(name, values, providers);
|
||||
}
|
||||
|
||||
private ItemHint.ValueHint toValueHint(JSONObject object) {
|
||||
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) {
|
||||
private ItemHint.ValueProvider toValueProvider(JSONObject object) throws Exception {
|
||||
String name = object.getString("name");
|
||||
Map<String, Object> parameters = new HashMap<String, Object>();
|
||||
if (object.has("parameters")) {
|
||||
JSONObject parametersObject = object.getJSONObject("parameters");
|
||||
for (Object k : parametersObject.keySet()) {
|
||||
String key = (String) k;
|
||||
for (Iterator<?> iterator = parametersObject.keys(); iterator.hasNext();) {
|
||||
String key = (String) iterator.next();
|
||||
Object value = readItemValue(parametersObject.get(key));
|
||||
parameters.put(key, value);
|
||||
}
|
||||
@@ -268,7 +155,7 @@ public class JsonMarshaller {
|
||||
return new ItemHint.ValueProvider(name, parameters);
|
||||
}
|
||||
|
||||
private Object readItemValue(Object value) {
|
||||
private Object readItemValue(Object value) throws Exception {
|
||||
if (value instanceof JSONArray) {
|
||||
JSONArray array = (JSONArray) value;
|
||||
Object[] content = new Object[array.length()];
|
||||
@@ -291,25 +178,4 @@ public class JsonMarshaller {
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension to {@link JSONObject} that remembers the order of inserts.
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static class JSONOrderedObject extends JSONObject {
|
||||
|
||||
private Set<String> keys = new LinkedHashSet<String>();
|
||||
|
||||
@Override
|
||||
public JSONObject put(String key, Object value) throws JSONException {
|
||||
this.keys.add(key);
|
||||
return super.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set keySet() {
|
||||
return this.keys;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.springframework.boot.configurationprocessor.metadata.ConfigurationMet
|
||||
import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation;
|
||||
import org.springframework.boot.configurationprocessor.metadata.ItemHint;
|
||||
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
|
||||
import org.springframework.boot.configurationprocessor.metadata.TestJsonConverter;
|
||||
import org.springframework.boot.configurationsample.incremental.BarProperties;
|
||||
import org.springframework.boot.configurationsample.incremental.FooProperties;
|
||||
import org.springframework.boot.configurationsample.incremental.RenamedBarProperties;
|
||||
@@ -630,8 +631,9 @@ public class ConfigurationMetadataAnnotationProcessorTests {
|
||||
JSONObject additionalMetadata = new JSONObject();
|
||||
additionalMetadata.put("properties", properties);
|
||||
FileWriter writer = new FileWriter(additionalMetadataFile);
|
||||
additionalMetadata.write(writer);
|
||||
writer.append(additionalMetadata.toString(2));
|
||||
writer.flush();
|
||||
writer.close();
|
||||
ConfigurationMetadata metadata = compile(SimpleProperties.class);
|
||||
assertThat(metadata).has(Metadata.withProperty("simple.comparator"));
|
||||
assertThat(metadata).has(Metadata.withProperty("foo", String.class)
|
||||
@@ -724,23 +726,28 @@ public class ConfigurationMetadataAnnotationProcessorTests {
|
||||
return processor.getMetadata();
|
||||
}
|
||||
|
||||
private void writeAdditionalMetadata(ItemMetadata... metadata) throws IOException {
|
||||
private void writeAdditionalMetadata(ItemMetadata... metadata) throws Exception {
|
||||
TestJsonConverter converter = new TestJsonConverter();
|
||||
File additionalMetadataFile = createAdditionalMetadataFile();
|
||||
JSONObject additionalMetadata = new JSONObject();
|
||||
additionalMetadata.put("properties", metadata);
|
||||
JSONArray properties = new JSONArray();
|
||||
for (ItemMetadata itemMetadata : metadata) {
|
||||
properties.put(converter.toJsonObject(itemMetadata));
|
||||
}
|
||||
additionalMetadata.put("properties", properties);
|
||||
writeMetadata(additionalMetadataFile, additionalMetadata);
|
||||
}
|
||||
|
||||
private void writeAdditionalHints(ItemHint... hints) throws IOException {
|
||||
private void writeAdditionalHints(ItemHint... hints) throws Exception {
|
||||
TestJsonConverter converter = new TestJsonConverter();
|
||||
File additionalMetadataFile = createAdditionalMetadataFile();
|
||||
JSONObject additionalMetadata = new JSONObject();
|
||||
additionalMetadata.put("hints", hints);
|
||||
additionalMetadata.put("hints", converter.toJsonArray(Arrays.asList(hints)));
|
||||
writeMetadata(additionalMetadataFile, additionalMetadata);
|
||||
}
|
||||
|
||||
private void writePropertyDeprecation(ItemMetadata... items) throws IOException {
|
||||
private void writePropertyDeprecation(ItemMetadata... items) throws Exception {
|
||||
File additionalMetadataFile = createAdditionalMetadataFile();
|
||||
|
||||
JSONArray propertiesArray = new JSONArray();
|
||||
for (ItemMetadata item : items) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
@@ -776,11 +783,10 @@ public class ConfigurationMetadataAnnotationProcessorTests {
|
||||
return additionalMetadataFile;
|
||||
}
|
||||
|
||||
private void writeMetadata(File metadataFile, JSONObject metadata)
|
||||
throws IOException {
|
||||
private void writeMetadata(File metadataFile, JSONObject metadata) throws Exception {
|
||||
FileWriter writer = new FileWriter(metadataFile);
|
||||
try {
|
||||
metadata.write(writer);
|
||||
writer.append(metadata.toString(2));
|
||||
}
|
||||
finally {
|
||||
writer.close();
|
||||
|
||||
@@ -70,7 +70,7 @@ public class TestConfigurationMetadataAnnotationProcessor
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ConfigurationMetadata writeMetaData() {
|
||||
protected ConfigurationMetadata writeMetaData() throws Exception {
|
||||
super.writeMetaData();
|
||||
try {
|
||||
File metadataFile = new File(this.outputLocation,
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.boot.configurationprocessor.metadata;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
@@ -38,7 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
public class JsonMarshallerTests {
|
||||
|
||||
@Test
|
||||
public void marshallAndUnmarshal() throws IOException {
|
||||
public void marshallAndUnmarshal() throws Exception {
|
||||
ConfigurationMetadata metadata = new ConfigurationMetadata();
|
||||
metadata.add(ItemMetadata.newProperty("a", "b", StringBuffer.class.getName(),
|
||||
InputStream.class.getName(), "sourceMethod", "desc", "x",
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* {@link JsonConverter} for use in tests.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class TestJsonConverter extends JsonConverter {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user