Skip to content

The Unified and Autonomous Development Platform

Update snakeyaml to 1.3.2 to fix a security volunerability

Robin Shen89e836c94e8a
diff --git a/pom.xml b/pom.xml
index 109b541662..fcccfbc2bc 100644
--- a/pom.xml
+++ b/pom.xml
@@ -347,7 +347,7 @@
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
- <version>1.31</version>
+ <version>1.32</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
diff --git a/server-core/src/main/java/org/yaml/snakeyaml/constructor/BaseConstructor.java b/server-core/src/main/java/org/yaml/snakeyaml/constructor/BaseConstructor.java
index c1303d85f8..347fe05e65 100644
--- a/server-core/src/main/java/org/yaml/snakeyaml/constructor/BaseConstructor.java
+++ b/server-core/src/main/java/org/yaml/snakeyaml/constructor/BaseConstructor.java
@@ -1,17 +1,15 @@
/**
* Copyright (c) 2008, SnakeYAML
*
- * 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
+ * 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
+ * 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.
+ * 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.yaml.snakeyaml.constructor;
@@ -33,7 +31,6 @@ import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
-
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.composer.Composer;
@@ -54,597 +51,594 @@ import io.onedev.server.web.editable.annotation.Editable;
public abstract class BaseConstructor {
- /**
- * An instance returned by newInstance methods when instantiation has not been performed.
- */
- protected static final Object NOT_INSTANTIATED_OBJECT = new Object();
-
- /**
- * It maps the node kind to the the Construct implementation. When the
- * runtime class is known then the implicit tag is ignored.
- */
- protected final Map<NodeId, Construct> yamlClassConstructors = new EnumMap<NodeId, Construct>(
- NodeId.class);
- /**
- * It maps the (explicit or implicit) tag to the Construct implementation.
- * It is used:
- * 1) explicit tag - if present.
- * 2) implicit tag - when the runtime class of the instance is unknown (the
- * node has the Object.class)
- */
- protected final Map<Tag, Construct> yamlConstructors = new HashMap<Tag, Construct>();
- /**
- * It maps the (explicit or implicit) tag to the Construct implementation.
- * It is used when no exact match found.
- */
- protected final Map<String, Construct> yamlMultiConstructors = new HashMap<String, Construct>();
-
- protected Composer composer;
- final Map<Node, Object> constructedObjects;
- private final Set<Node> recursiveObjects;
- private final ArrayList<RecursiveTuple<Map<Object, Object>, RecursiveTuple<Object, Object>>> maps2fill;
- private final ArrayList<RecursiveTuple<Set<Object>, Object>> sets2fill;
-
- protected Tag rootTag;
- private PropertyUtils propertyUtils;
- private boolean explicitPropertyUtils;
- private boolean allowDuplicateKeys = true;
- private boolean wrappedToRootException = false;
-
- private boolean enumCaseSensitive = false;
-
- protected final Map<Class<? extends Object>, TypeDescription> typeDefinitions;
- protected final Map<Tag, Class<? extends Object>> typeTags;
-
- protected LoaderOptions loadingConfig;
-
- public BaseConstructor() {
- this(new LoaderOptions());
- }
-
- public BaseConstructor(LoaderOptions loadingConfig) {
- constructedObjects = new HashMap<Node, Object>();
- recursiveObjects = new HashSet<Node>();
- maps2fill = new ArrayList<RecursiveTuple<Map<Object, Object>, RecursiveTuple<Object, Object>>>();
- sets2fill = new ArrayList<RecursiveTuple<Set<Object>, Object>>();
- typeDefinitions = new HashMap<Class<? extends Object>, TypeDescription>();
- typeTags = new HashMap<Tag, Class<? extends Object>>();
-
- rootTag = null;
- explicitPropertyUtils = false;
-
- typeDefinitions.put(SortedMap.class, new TypeDescription(SortedMap.class, Tag.OMAP,
- TreeMap.class));
- typeDefinitions.put(SortedSet.class, new TypeDescription(SortedSet.class, Tag.SET,
- TreeSet.class));
- this.loadingConfig = loadingConfig;
- }
-
- public void setComposer(Composer composer) {
- this.composer = composer;
- }
-
- /**
- * Check if more documents available
- *
- * @return true when there are more YAML documents in the stream
- */
- public boolean checkData() {
- // If there are more documents available?
- return composer.checkNode();
- }
-
- /**
- * Construct and return the next document
- *
- * @return constructed instance
- */
- public Object getData() throws NoSuchElementException {
- // Construct and return the next document.
- if (!composer.checkNode()) throw new NoSuchElementException("No document is available.");
- Node node = composer.getNode();
- if (rootTag != null) {
- node.setTag(rootTag);
+ /**
+ * An instance returned by newInstance methods when instantiation has not been performed.
+ */
+ protected static final Object NOT_INSTANTIATED_OBJECT = new Object();
+
+ /**
+ * It maps the node kind to the the Construct implementation. When the runtime class is known then
+ * the implicit tag is ignored.
+ */
+ protected final Map<NodeId, Construct> yamlClassConstructors =
+ new EnumMap<NodeId, Construct>(NodeId.class);
+ /**
+ * It maps the (explicit or implicit) tag to the Construct implementation. It is used: 1) explicit
+ * tag - if present. 2) implicit tag - when the runtime class of the instance is unknown (the node
+ * has the Object.class)
+ */
+ protected final Map<Tag, Construct> yamlConstructors = new HashMap<Tag, Construct>();
+ /**
+ * It maps the (explicit or implicit) tag to the Construct implementation. It is used when no
+ * exact match found.
+ */
+ protected final Map<String, Construct> yamlMultiConstructors = new HashMap<String, Construct>();
+
+ protected Composer composer;
+ final Map<Node, Object> constructedObjects;
+ private final Set<Node> recursiveObjects;
+ private final ArrayList<RecursiveTuple<Map<Object, Object>, RecursiveTuple<Object, Object>>> maps2fill;
+ private final ArrayList<RecursiveTuple<Set<Object>, Object>> sets2fill;
+
+ protected Tag rootTag;
+ private PropertyUtils propertyUtils;
+ private boolean explicitPropertyUtils;
+ private boolean allowDuplicateKeys = true;
+ private boolean wrappedToRootException = false;
+
+ private boolean enumCaseSensitive = false;
+
+ protected final Map<Class<? extends Object>, TypeDescription> typeDefinitions;
+ protected final Map<Tag, Class<? extends Object>> typeTags;
+
+ protected LoaderOptions loadingConfig;
+
+ public BaseConstructor() {
+ this(new LoaderOptions());
+ }
+
+ public BaseConstructor(LoaderOptions loadingConfig) {
+ constructedObjects = new HashMap<Node, Object>();
+ recursiveObjects = new HashSet<Node>();
+ maps2fill =
+ new ArrayList<RecursiveTuple<Map<Object, Object>, RecursiveTuple<Object, Object>>>();
+ sets2fill = new ArrayList<RecursiveTuple<Set<Object>, Object>>();
+ typeDefinitions = new HashMap<Class<? extends Object>, TypeDescription>();
+ typeTags = new HashMap<Tag, Class<? extends Object>>();
+
+ rootTag = null;
+ explicitPropertyUtils = false;
+
+ typeDefinitions.put(SortedMap.class,
+ new TypeDescription(SortedMap.class, Tag.OMAP, TreeMap.class));
+ typeDefinitions.put(SortedSet.class,
+ new TypeDescription(SortedSet.class, Tag.SET, TreeSet.class));
+ this.loadingConfig = loadingConfig;
+ }
+
+ public void setComposer(Composer composer) {
+ this.composer = composer;
+ }
+
+ /**
+ * Check if more documents available
+ *
+ * @return true when there are more YAML documents in the stream
+ */
+ public boolean checkData() {
+ // If there are more documents available?
+ return composer.checkNode();
+ }
+
+ /**
+ * Construct and return the next document
+ *
+ * @return constructed instance
+ */
+ public Object getData() throws NoSuchElementException {
+ // Construct and return the next document.
+ if (!composer.checkNode()) {
+ throw new NoSuchElementException("No document is available.");
+ }
+ Node node = composer.getNode();
+ if (rootTag != null) {
+ node.setTag(rootTag);
+ }
+ return constructDocument(node);
+ }
+
+ /**
+ * Ensure that the stream contains a single document and construct it
+ *
+ * @param type the class of the instance being created
+ * @return constructed instance
+ * @throws ComposerException in case there are more documents in the stream
+ */
+ public Object getSingleData(Class<?> type) {
+ // Ensure that the stream contains a single document and construct it
+ final Node node = composer.getSingleNode();
+ if (node != null && !Tag.NULL.equals(node.getTag())) {
+ if (Object.class != type) {
+ node.setTag(new Tag(type));
+ } else if (rootTag != null) {
+ node.setTag(rootTag);
+ }
+ return constructDocument(node);
+ } else {
+ Construct construct = yamlConstructors.get(Tag.NULL);
+ return construct.construct(node);
+ }
+ }
+
+ /**
+ * Construct complete YAML document. Call the second step in case of recursive structures. At the
+ * end cleans all the state.
+ *
+ * @param node root Node
+ * @return Java instance
+ */
+ protected final Object constructDocument(Node node) {
+ try {
+ Object data = constructObject(node);
+ fillRecursive();
+ return data;
+ } catch (RuntimeException e) {
+ if (wrappedToRootException && !(e instanceof YAMLException)) {
+ throw new YAMLException(e);
+ } else {
+ throw e;
+ }
+ } finally {
+ // clean up resources
+ constructedObjects.clear();
+ recursiveObjects.clear();
+ }
+ }
+
+ /**
+ * Fill the recursive structures and clean the internal collections
+ */
+ private void fillRecursive() {
+ if (!maps2fill.isEmpty()) {
+ for (RecursiveTuple<Map<Object, Object>, RecursiveTuple<Object, Object>> entry : maps2fill) {
+ RecursiveTuple<Object, Object> key_value = entry._2();
+ entry._1().put(key_value._1(), key_value._2());
+ }
+ maps2fill.clear();
+ }
+ if (!sets2fill.isEmpty()) {
+ for (RecursiveTuple<Set<Object>, Object> value : sets2fill) {
+ value._1().add(value._2());
+ }
+ sets2fill.clear();
+ }
+ }
+
+ /**
+ * Construct object from the specified Node. Return existing instance if the node is already
+ * constructed.
+ *
+ * @param node Node to be constructed
+ * @return Java instance
+ */
+ protected Object constructObject(Node node) {
+ if (constructedObjects.containsKey(node)) {
+ return constructedObjects.get(node);
+ }
+ return constructObjectNoCheck(node);
+ }
+
+ protected Object constructObjectNoCheck(Node node) {
+ if (recursiveObjects.contains(node)) {
+ throw new ConstructorException(null, null, "found unconstructable recursive node",
+ node.getStartMark());
+ }
+ recursiveObjects.add(node);
+ Construct constructor = getConstructor(node);
+ Object data = (constructedObjects.containsKey(node)) ? constructedObjects.get(node)
+ : constructor.construct(node);
+
+ finalizeConstruction(node, data);
+ constructedObjects.put(node, data);
+ recursiveObjects.remove(node);
+ if (node.isTwoStepsConstruction()) {
+ constructor.construct2ndStep(node, data);
+ }
+ return data;
+ }
+
+ /**
+ * Get the constructor to construct the Node. For implicit tags if the runtime class is known a
+ * dedicated Construct implementation is used. Otherwise the constructor is chosen by the tag.
+ *
+ * @param node {@link Node} to construct an instance from
+ * @return {@link Construct} implementation for the specified node
+ */
+ protected Construct getConstructor(Node node) {
+ if (node.useClassConstructor()) {
+ return yamlClassConstructors.get(node.getNodeId());
+ } else {
+ Construct constructor = yamlConstructors.get(node.getTag());
+ if (constructor == null) {
+ for (String prefix : yamlMultiConstructors.keySet()) {
+ if (node.getTag().startsWith(prefix)) {
+ return yamlMultiConstructors.get(prefix);
+ }
}
- return constructDocument(node);
- }
-
- /**
- * Ensure that the stream contains a single document and construct it
- *
- * @param type the class of the instance being created
- * @return constructed instance
- * @throws ComposerException in case there are more documents in the stream
- */
- public Object getSingleData(Class<?> type) {
- // Ensure that the stream contains a single document and construct it
- final Node node = composer.getSingleNode();
- if (node != null && !Tag.NULL.equals(node.getTag())) {
- if (Object.class != type) {
- node.setTag(new Tag(type));
- } else if (rootTag != null) {
- node.setTag(rootTag);
- }
- return constructDocument(node);
- } else {
- Construct construct = yamlConstructors.get(Tag.NULL);
- return construct.construct(node);
+ return yamlConstructors.get(null);
+ }
+ return constructor;
+ }
+ }
+
+ protected String constructScalar(ScalarNode node) {
+ return node.getValue();
+ }
+
+ // >>>> DEFAULTS >>>>
+ protected List<Object> createDefaultList(int initSize) {
+ return new ArrayList<Object>(initSize);
+ }
+
+ protected Set<Object> createDefaultSet(int initSize) {
+ return new LinkedHashSet<Object>(initSize);
+ }
+
+ protected Map<Object, Object> createDefaultMap(int initSize) {
+ // respect order from YAML document
+ return new LinkedHashMap<Object, Object>(initSize);
+ }
+
+ protected Object createArray(Class<?> type, int size) {
+ return Array.newInstance(type.getComponentType(), size);
+ }
+
+ // <<<< DEFAULTS <<<<
+
+ protected Object finalizeConstruction(Node node, Object data) {
+ final Class<? extends Object> type = node.getType();
+ if (typeDefinitions.containsKey(type)) {
+ return typeDefinitions.get(type).finalizeConstruction(data);
+ }
+ return data;
+ }
+
+ // >>>> NEW instance
+ protected Object newInstance(Node node) {
+ return newInstance(Object.class, node);
+ }
+
+ protected final Object newInstance(Class<?> ancestor, Node node) {
+ return newInstance(ancestor, node, true);
+ }
+
+ /**
+ * Tries to create a new object for the node.
+ *
+ * @param ancestor expected ancestor of the {@code node.getType()}
+ * @param node for which to create a corresponding java object
+ * @param tryDefault should default constructor to be tried when there is no corresponding
+ * {@code TypeDescription} or {@code TypeDescription.newInstance(node)} returns
+ * {@code null}.
+ *
+ * @return - a new object created for {@code node.getType()} by using corresponding
+ * TypeDescription.newInstance or default constructor. - {@code NOT_INSTANTIATED_OBJECT}
+ * in case no object has been created
+ */
+ protected Object newInstance(Class<?> ancestor, Node node, boolean tryDefault) {
+ try {
+ final Class<? extends Object> type = node.getType();
+ if (typeDefinitions.containsKey(type)) {
+ TypeDescription td = typeDefinitions.get(type);
+ final Object instance = td.newInstance(node);
+ if (instance != null) {
+ return instance;
}
- }
-
- /**
- * Construct complete YAML document. Call the second step in case of
- * recursive structures. At the end cleans all the state.
- *
- * @param node root Node
- * @return Java instance
- */
- protected final Object constructDocument(Node node) {
- try {
- Object data = constructObject(node);
- fillRecursive();
- return data;
- } catch (RuntimeException e) {
- if (wrappedToRootException && !(e instanceof YAMLException)) {
- throw new YAMLException(e);
- } else {
- throw e;
- }
- } finally {
- //clean up resources
- constructedObjects.clear();
- recursiveObjects.clear();
+ }
+
+ if (tryDefault) {
+ /*
+ * Removed <code> have InstantiationException in case of abstract type
+ */
+ if (ancestor.isAssignableFrom(type) && !Modifier.isAbstract(type.getModifiers())) {
+ java.lang.reflect.Constructor<?> c = type.getDeclaredConstructor();
+ c.setAccessible(true);
+ Object instance = c.newInstance();
+ if (type.getAnnotation(Editable.class) != null) {
+ for (Method getter: BeanUtils.findGetters(type)) {
+ if (getter.getAnnotation(Editable.class) != null) {
+ Method setter = BeanUtils.findSetter(getter);
+ if (setter != null) {
+ if (Collection.class.isAssignableFrom(getter.getReturnType()))
+ setter.invoke(instance, new Object[] {new ArrayList<>()});
+ else if (Map.class.isAssignableFrom(getter.getReturnType()))
+ setter.invoke(instance, new Object[] {new LinkedHashMap<>()});
+ else if (Object.class.isAssignableFrom(getter.getReturnType()))
+ setter.invoke(instance, new Object[] {null});
+ }
+ }
+ }
+ }
+ return instance;
}
- }
-
- /**
- * Fill the recursive structures and clean the internal collections
- */
- private void fillRecursive() {
- if (!maps2fill.isEmpty()) {
- for (RecursiveTuple<Map<Object, Object>, RecursiveTuple<Object, Object>> entry : maps2fill) {
- RecursiveTuple<Object, Object> key_value = entry._2();
- entry._1().put(key_value._1(), key_value._2());
- }
- maps2fill.clear();
- }
- if (!sets2fill.isEmpty()) {
- for (RecursiveTuple<Set<Object>, Object> value : sets2fill) {
- value._1().add(value._2());
- }
- sets2fill.clear();
+ }
+ } catch (Exception e) {
+ throw new YAMLException(e);
+ }
+
+ return NOT_INSTANTIATED_OBJECT;
+ }
+
+ @SuppressWarnings("unchecked")
+ protected Set<Object> newSet(CollectionNode<?> node) {
+ Object instance = newInstance(Set.class, node);
+ if (instance != NOT_INSTANTIATED_OBJECT) {
+ return (Set<Object>) instance;
+ } else {
+ return createDefaultSet(node.getValue().size());
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ protected List<Object> newList(SequenceNode node) {
+ Object instance = newInstance(List.class, node);
+ if (instance != NOT_INSTANTIATED_OBJECT) {
+ return (List<Object>) instance;
+ } else {
+ return createDefaultList(node.getValue().size());
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ protected Map<Object, Object> newMap(MappingNode node) {
+ Object instance = newInstance(Map.class, node);
+ if (instance != NOT_INSTANTIATED_OBJECT) {
+ return (Map<Object, Object>) instance;
+ } else {
+ return createDefaultMap(node.getValue().size());
+ }
+ }
+
+ // <<<< NEW instance
+
+ // >>>> Construct => NEW, 2ndStep(filling)
+ protected List<? extends Object> constructSequence(SequenceNode node) {
+ List<Object> result = newList(node);
+ constructSequenceStep2(node, result);
+ return result;
+ }
+
+ protected Set<? extends Object> constructSet(SequenceNode node) {
+ Set<Object> result = newSet(node);
+ constructSequenceStep2(node, result);
+ return result;
+ }
+
+ protected Object constructArray(SequenceNode node) {
+ return constructArrayStep2(node, createArray(node.getType(), node.getValue().size()));
+ }
+
+ protected void constructSequenceStep2(SequenceNode node, Collection<Object> collection) {
+ for (Node child : node.getValue()) {
+ collection.add(constructObject(child));
+ }
+ }
+
+ protected Object constructArrayStep2(SequenceNode node, Object array) {
+ final Class<?> componentType = node.getType().getComponentType();
+
+ int index = 0;
+ for (Node child : node.getValue()) {
+ // Handle multi-dimensional arrays...
+ if (child.getType() == Object.class) {
+ child.setType(componentType);
+ }
+
+ final Object value = constructObject(child);
+
+ if (componentType.isPrimitive()) {
+ // Null values are disallowed for primitives
+ if (value == null) {
+ throw new NullPointerException("Unable to construct element value for " + child);
}
- }
- /**
- * Construct object from the specified Node. Return existing instance if the
- * node is already constructed.
- *
- * @param node Node to be constructed
- * @return Java instance
- */
- protected Object constructObject(Node node) {
- if (constructedObjects.containsKey(node)) {
- return constructedObjects.get(node);
- }
- return constructObjectNoCheck(node);
- }
+ // Primitive arrays require quite a lot of work.
+ if (byte.class.equals(componentType)) {
+ Array.setByte(array, index, ((Number) value).byteValue());
- protected Object constructObjectNoCheck(Node node) {
- if (recursiveObjects.contains(node)) {
- throw new ConstructorException(null, null, "found unconstructable recursive node",
- node.getStartMark());
- }
- recursiveObjects.add(node);
- Construct constructor = getConstructor(node);
- Object data = (constructedObjects.containsKey(node)) ? constructedObjects.get(node)
- : constructor.construct(node);
-
- finalizeConstruction(node, data);
- constructedObjects.put(node, data);
- recursiveObjects.remove(node);
- if (node.isTwoStepsConstruction()) {
- constructor.construct2ndStep(node, data);
- }
- return data;
- }
+ } else if (short.class.equals(componentType)) {
+ Array.setShort(array, index, ((Number) value).shortValue());
- /**
- * Get the constructor to construct the Node. For implicit tags if the
- * runtime class is known a dedicated Construct implementation is used.
- * Otherwise the constructor is chosen by the tag.
- *
- * @param node {@link Node} to construct an instance from
- * @return {@link Construct} implementation for the specified node
- */
- protected Construct getConstructor(Node node) {
- if (node.useClassConstructor()) {
- return yamlClassConstructors.get(node.getNodeId());
- } else {
- Construct constructor = yamlConstructors.get(node.getTag());
- if (constructor == null) {
- for (String prefix : yamlMultiConstructors.keySet()) {
- if (node.getTag().startsWith(prefix)) {
- return yamlMultiConstructors.get(prefix);
- }
- }
- return yamlConstructors.get(null);
- }
- return constructor;
- }
- }
+ } else if (int.class.equals(componentType)) {
+ Array.setInt(array, index, ((Number) value).intValue());
- protected String constructScalar(ScalarNode node) {
- return node.getValue();
- }
+ } else if (long.class.equals(componentType)) {
+ Array.setLong(array, index, ((Number) value).longValue());
- // >>>> DEFAULTS >>>>
- protected List<Object> createDefaultList(int initSize) {
- return new ArrayList<Object>(initSize);
- }
-
- protected Set<Object> createDefaultSet(int initSize) {
- return new LinkedHashSet<Object>(initSize);
- }
+ } else if (float.class.equals(componentType)) {
+ Array.setFloat(array, index, ((Number) value).floatValue());
- protected Map<Object, Object> createDefaultMap(int initSize) {
- // respect order from YAML document
- return new LinkedHashMap<Object, Object>(initSize);
- }
+ } else if (double.class.equals(componentType)) {
+ Array.setDouble(array, index, ((Number) value).doubleValue());
- protected Object createArray(Class<?> type, int size) {
- return Array.newInstance(type.getComponentType(), size);
- }
+ } else if (char.class.equals(componentType)) {
+ Array.setChar(array, index, ((Character) value).charValue());
- // <<<< DEFAULTS <<<<
+ } else if (boolean.class.equals(componentType)) {
+ Array.setBoolean(array, index, ((Boolean) value).booleanValue());
- protected Object finalizeConstruction(Node node, Object data) {
- final Class<? extends Object> type = node.getType();
- if (typeDefinitions.containsKey(type)) {
- return typeDefinitions.get(type).finalizeConstruction(data);
+ } else {
+ throw new YAMLException("unexpected primitive type");
}
- return data;
- }
-
- // >>>> NEW instance
- protected Object newInstance(Node node) {
- return newInstance(Object.class, node);
- }
-
- final protected Object newInstance(Class<?> ancestor, Node node) {
- return newInstance(ancestor, node, true);
- }
- /**
- * Tries to create a new object for the node.
- *
- * @param ancestor expected ancestor of the {@code node.getType()}
- * @param node for which to create a corresponding java object
- * @param tryDefault should default constructor to be tried when there is no corresponding
- * {@code TypeDescription} or {@code TypeDescription.newInstance(node)} returns {@code null}.
- *
- * @return - a new object created for {@code node.getType()} by using corresponding
- * TypeDescription.newInstance or default constructor.
- * - {@code NOT_INSTANTIATED_OBJECT} in case no object has been created
- */
- protected Object newInstance(Class<?> ancestor, Node node, boolean tryDefault) {
+ } else {
+ // Non-primitive arrays can simply be assigned:
+ Array.set(array, index, value);
+ }
+
+ ++index;
+ }
+ return array;
+ }
+
+ protected Set<Object> constructSet(MappingNode node) {
+ final Set<Object> set = newSet(node);
+ constructSet2ndStep(node, set);
+ return set;
+ }
+
+ protected Map<Object, Object> constructMapping(MappingNode node) {
+ final Map<Object, Object> mapping = newMap(node);
+ constructMapping2ndStep(node, mapping);
+ return mapping;
+ }
+
+ protected void constructMapping2ndStep(MappingNode node, Map<Object, Object> mapping) {
+ List<NodeTuple> nodeValue = node.getValue();
+ for (NodeTuple tuple : nodeValue) {
+ Node keyNode = tuple.getKeyNode();
+ Node valueNode = tuple.getValueNode();
+ Object key = constructObject(keyNode);
+ if (key != null) {
try {
- final Class<? extends Object> type = node.getType();
- if (typeDefinitions.containsKey(type)) {
- TypeDescription td = typeDefinitions.get(type);
- final Object instance = td.newInstance(node);
- if (instance != null) {
- return instance;
- }
- }
-
- if (tryDefault) {
- /*
- * Removed <code> have InstantiationException in case of
- * abstract type
- */
- if (ancestor.isAssignableFrom(type) && !Modifier.isAbstract(type.getModifiers())) {
- java.lang.reflect.Constructor<?> c = type.getDeclaredConstructor();
- c.setAccessible(true);
- Object instance = c.newInstance();
- if (type.getAnnotation(Editable.class) != null) {
- for (Method getter: BeanUtils.findGetters(type)) {
- if (getter.getAnnotation(Editable.class) != null) {
- Method setter = BeanUtils.findSetter(getter);
- if (setter != null) {
- if (Collection.class.isAssignableFrom(getter.getReturnType()))
- setter.invoke(instance, new Object[] {new ArrayList<>()});
- else if (Map.class.isAssignableFrom(getter.getReturnType()))
- setter.invoke(instance, new Object[] {new LinkedHashMap<>()});
- else if (Object.class.isAssignableFrom(getter.getReturnType()))
- setter.invoke(instance, new Object[] {null});
- }
- }
- }
- }
- return instance;
- }
- }
+ key.hashCode();// check circular dependencies
} catch (Exception e) {
- throw new YAMLException(e);
+ throw new ConstructorException("while constructing a mapping", node.getStartMark(),
+ "found unacceptable key " + key, tuple.getKeyNode().getStartMark(), e);
}
-
- return NOT_INSTANTIATED_OBJECT;
- }
-
- @SuppressWarnings("unchecked")
- protected Set<Object> newSet(CollectionNode<?> node) {
- Object instance = newInstance(Set.class, node);
- if (instance != NOT_INSTANTIATED_OBJECT) {
- return (Set<Object>) instance;
- } else {
- return createDefaultSet(node.getValue().size());
- }
- }
-
- @SuppressWarnings("unchecked")
- protected List<Object> newList(SequenceNode node) {
- Object instance = newInstance(List.class, node);
- if (instance != NOT_INSTANTIATED_OBJECT) {
- return (List<Object>) instance;
+ }
+ Object value = constructObject(valueNode);
+ if (keyNode.isTwoStepsConstruction()) {
+ if (loadingConfig.getAllowRecursiveKeys()) {
+ postponeMapFilling(mapping, key, value);
} else {
- return createDefaultList(node.getValue().size());
- }
- }
-
- @SuppressWarnings("unchecked")
- protected Map<Object, Object> newMap(MappingNode node) {
- Object instance = newInstance(Map.class, node);
- if (instance != NOT_INSTANTIATED_OBJECT) {
- return (Map<Object, Object>) instance;
- } else {
- return createDefaultMap(node.getValue().size());
- }
- }
-
- // <<<< NEW instance
-
- // >>>> Construct => NEW, 2ndStep(filling)
- protected List<? extends Object> constructSequence(SequenceNode node) {
- List<Object> result = newList(node);
- constructSequenceStep2(node, result);
- return result;
- }
-
- protected Set<? extends Object> constructSet(SequenceNode node) {
- Set<Object> result = newSet(node);
- constructSequenceStep2(node, result);
- return result;
- }
-
- protected Object constructArray(SequenceNode node) {
- return constructArrayStep2(node, createArray(node.getType(), node.getValue().size()));
- }
-
- protected void constructSequenceStep2(SequenceNode node, Collection<Object> collection) {
- for (Node child : node.getValue()) {
- collection.add(constructObject(child));
- }
- }
-
- protected Object constructArrayStep2(SequenceNode node, Object array) {
- final Class<?> componentType = node.getType().getComponentType();
-
- int index = 0;
- for (Node child : node.getValue()) {
- // Handle multi-dimensional arrays...
- if (child.getType() == Object.class) {
- child.setType(componentType);
- }
-
- final Object value = constructObject(child);
-
- if (componentType.isPrimitive()) {
- // Null values are disallowed for primitives
- if (value == null) {
- throw new NullPointerException(
- "Unable to construct element value for " + child);
- }
-
- // Primitive arrays require quite a lot of work.
- if (byte.class.equals(componentType)) {
- Array.setByte(array, index, ((Number) value).byteValue());
-
- } else if (short.class.equals(componentType)) {
- Array.setShort(array, index, ((Number) value).shortValue());
-
- } else if (int.class.equals(componentType)) {
- Array.setInt(array, index, ((Number) value).intValue());
-
- } else if (long.class.equals(componentType)) {
- Array.setLong(array, index, ((Number) value).longValue());
-
- } else if (float.class.equals(componentType)) {
- Array.setFloat(array, index, ((Number) value).floatValue());
-
- } else if (double.class.equals(componentType)) {
- Array.setDouble(array, index, ((Number) value).doubleValue());
-
- } else if (char.class.equals(componentType)) {
- Array.setChar(array, index, ((Character) value).charValue());
-
- } else if (boolean.class.equals(componentType)) {
- Array.setBoolean(array, index, ((Boolean) value).booleanValue());
-
- } else {
- throw new YAMLException("unexpected primitive type");
- }
-
- } else {
- // Non-primitive arrays can simply be assigned:
- Array.set(array, index, value);
- }
-
- ++index;
- }
- return array;
- }
-
- protected Set<Object> constructSet(MappingNode node) {
- final Set<Object> set = newSet(node);
- constructSet2ndStep(node, set);
- return set;
- }
-
- protected Map<Object, Object> constructMapping(MappingNode node) {
- final Map<Object, Object> mapping = newMap(node);
- constructMapping2ndStep(node, mapping);
- return mapping;
- }
-
- protected void constructMapping2ndStep(MappingNode node, Map<Object, Object> mapping) {
- List<NodeTuple> nodeValue = node.getValue();
- for (NodeTuple tuple : nodeValue) {
- Node keyNode = tuple.getKeyNode();
- Node valueNode = tuple.getValueNode();
- Object key = constructObject(keyNode);
- if (key != null) {
- try {
- key.hashCode();// check circular dependencies
- } catch (Exception e) {
- throw new ConstructorException("while constructing a mapping",
- node.getStartMark(), "found unacceptable key " + key,
- tuple.getKeyNode().getStartMark(), e);
- }
- }
- Object value = constructObject(valueNode);
- if (keyNode.isTwoStepsConstruction()) {
- if (loadingConfig.getAllowRecursiveKeys()) {
- postponeMapFilling(mapping, key, value);
- } else {
- throw new YAMLException("Recursive key for mapping is detected but it is not configured to be allowed.");
- }
- } else {
- mapping.put(key, value);
- }
- }
- }
-
- /*
- * if keyObject is created it 2 steps we should postpone putting
- * it in map because it may have different hash after
- * initialization compared to clean just created one. And map of
- * course does not observe key hashCode changes.
- */
- @SuppressWarnings({ "unchecked", "rawtypes" })
- protected void postponeMapFilling(Map<Object, Object> mapping, Object key, Object value) {
- maps2fill.add(0, new RecursiveTuple(mapping, new RecursiveTuple(key, value)));
- }
-
- protected void constructSet2ndStep(MappingNode node, Set<Object> set) {
- List<NodeTuple> nodeValue = node.getValue();
- for (NodeTuple tuple : nodeValue) {
- Node keyNode = tuple.getKeyNode();
- Object key = constructObject(keyNode);
- if (key != null) {
- try {
- key.hashCode();// check circular dependencies
- } catch (Exception e) {
- throw new ConstructorException("while constructing a Set", node.getStartMark(),
- "found unacceptable key " + key, tuple.getKeyNode().getStartMark(), e);
- }
- }
- if (keyNode.isTwoStepsConstruction()) {
- postponeSetFilling(set, key);
- } else {
- set.add(key);
- }
- }
- }
-
- /*
- * if keyObject is created it 2 steps we should postpone putting
- * it into the set because it may have different hash after
- * initialization compared to clean just created one. And set of
- * course does not observe value hashCode changes.
- */
- protected void postponeSetFilling(Set<Object> set, Object key) {
- sets2fill.add(0, new RecursiveTuple<Set<Object>, Object>(set, key));
- }
-
- public void setPropertyUtils(PropertyUtils propertyUtils) {
- this.propertyUtils = propertyUtils;
- explicitPropertyUtils = true;
- Collection<TypeDescription> tds = typeDefinitions.values();
- for (TypeDescription typeDescription : tds) {
- typeDescription.setPropertyUtils(propertyUtils);
- }
- }
-
- public final PropertyUtils getPropertyUtils() {
- if (propertyUtils == null) {
- propertyUtils = new PropertyUtils();
- }
- return propertyUtils;
- }
-
- /**
- * Make YAML aware how to parse a custom Class. If there is no root Class
- * assigned in constructor then the 'root' property of this definition is
- * respected.
- *
- * @param definition to be added to the Constructor
- * @return the previous value associated with <code>definition</code>, or
- * <code>null</code> if there was no mapping for <code>definition</code>.
- */
- public TypeDescription addTypeDescription(TypeDescription definition) {
- if (definition == null) {
- throw new NullPointerException("TypeDescription is required.");
- }
- Tag tag = definition.getTag();
- typeTags.put(tag, definition.getType());
- definition.setPropertyUtils(getPropertyUtils());
- return typeDefinitions.put(definition.getType(), definition);
- }
-
- private static class RecursiveTuple<T, K> {
- private final T _1;
- private final K _2;
-
- public RecursiveTuple(T _1, K _2) {
- this._1 = _1;
- this._2 = _2;
- }
-
- public K _2() {
- return _2;
+ throw new YAMLException(
+ "Recursive key for mapping is detected but it is not configured to be allowed.");
}
-
- public T _1() {
- return _1;
+ } else {
+ mapping.put(key, value);
+ }
+ }
+ }
+
+ /*
+ * if keyObject is created it 2 steps we should postpone putting it in map because it may have
+ * different hash after initialization compared to clean just created one. And map of course does
+ * not observe key hashCode changes.
+ */
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+protected void postponeMapFilling(Map<Object, Object> mapping, Object key, Object value) {
+ maps2fill.add(0, new RecursiveTuple(mapping, new RecursiveTuple(key, value)));
+ }
+
+ protected void constructSet2ndStep(MappingNode node, Set<Object> set) {
+ List<NodeTuple> nodeValue = node.getValue();
+ for (NodeTuple tuple : nodeValue) {
+ Node keyNode = tuple.getKeyNode();
+ Object key = constructObject(keyNode);
+ if (key != null) {
+ try {
+ key.hashCode();// check circular dependencies
+ } catch (Exception e) {
+ throw new ConstructorException("while constructing a Set", node.getStartMark(),
+ "found unacceptable key " + key, tuple.getKeyNode().getStartMark(), e);
}
- }
-
- public final boolean isExplicitPropertyUtils() {
- return explicitPropertyUtils;
- }
-
- public boolean isAllowDuplicateKeys() {
- return allowDuplicateKeys;
- }
-
- public void setAllowDuplicateKeys(boolean allowDuplicateKeys) {
- this.allowDuplicateKeys = allowDuplicateKeys;
- }
-
- public boolean isWrappedToRootException() {
- return wrappedToRootException;
- }
-
- public void setWrappedToRootException(boolean wrappedToRootException) {
- this.wrappedToRootException = wrappedToRootException;
- }
-
- public boolean isEnumCaseSensitive() {
- return enumCaseSensitive;
- }
-
- public void setEnumCaseSensitive(boolean enumCaseSensitive) {
- this.enumCaseSensitive = enumCaseSensitive;
- }
+ }
+ if (keyNode.isTwoStepsConstruction()) {
+ postponeSetFilling(set, key);
+ } else {
+ set.add(key);
+ }
+ }
+ }
+
+ /*
+ * if keyObject is created it 2 steps we should postpone putting it into the set because it may
+ * have different hash after initialization compared to clean just created one. And set of course
+ * does not observe value hashCode changes.
+ */
+ protected void postponeSetFilling(Set<Object> set, Object key) {
+ sets2fill.add(0, new RecursiveTuple<Set<Object>, Object>(set, key));
+ }
+
+ public void setPropertyUtils(PropertyUtils propertyUtils) {
+ this.propertyUtils = propertyUtils;
+ explicitPropertyUtils = true;
+ Collection<TypeDescription> tds = typeDefinitions.values();
+ for (TypeDescription typeDescription : tds) {
+ typeDescription.setPropertyUtils(propertyUtils);
+ }
+ }
+
+ public final PropertyUtils getPropertyUtils() {
+ if (propertyUtils == null) {
+ propertyUtils = new PropertyUtils();
+ }
+ return propertyUtils;
+ }
+
+ /**
+ * Make YAML aware how to parse a custom Class. If there is no root Class assigned in constructor
+ * then the 'root' property of this definition is respected.
+ *
+ * @param definition to be added to the Constructor
+ * @return the previous value associated with <code>definition</code>, or <code>null</code> if
+ * there was no mapping for <code>definition</code>.
+ */
+ public TypeDescription addTypeDescription(TypeDescription definition) {
+ if (definition == null) {
+ throw new NullPointerException("TypeDescription is required.");
+ }
+ Tag tag = definition.getTag();
+ typeTags.put(tag, definition.getType());
+ definition.setPropertyUtils(getPropertyUtils());
+ return typeDefinitions.put(definition.getType(), definition);
+ }
+
+ private static class RecursiveTuple<T, K> {
+
+ private final T _1;
+ private final K _2;
+
+ public RecursiveTuple(T _1, K _2) {
+ this._1 = _1;
+ this._2 = _2;
+ }
+
+ public K _2() {
+ return _2;
+ }
+
+ public T _1() {
+ return _1;
+ }
+ }
+
+ public final boolean isExplicitPropertyUtils() {
+ return explicitPropertyUtils;
+ }
+
+ public boolean isAllowDuplicateKeys() {
+ return allowDuplicateKeys;
+ }
+
+ public void setAllowDuplicateKeys(boolean allowDuplicateKeys) {
+ this.allowDuplicateKeys = allowDuplicateKeys;
+ }
+
+ public boolean isWrappedToRootException() {
+ return wrappedToRootException;
+ }
+
+ public void setWrappedToRootException(boolean wrappedToRootException) {
+ this.wrappedToRootException = wrappedToRootException;
+ }
+
+ public boolean isEnumCaseSensitive() {
+ return enumCaseSensitive;
+ }
+
+ public void setEnumCaseSensitive(boolean enumCaseSensitive) {
+ this.enumCaseSensitive = enumCaseSensitive;
+ }
}
diff --git a/server-core/src/main/java/org/yaml/snakeyaml/nodes/ScalarNode.java b/server-core/src/main/java/org/yaml/snakeyaml/nodes/ScalarNode.java
index a45526afe9..df61acdbee 100644
--- a/server-core/src/main/java/org/yaml/snakeyaml/nodes/ScalarNode.java
+++ b/server-core/src/main/java/org/yaml/snakeyaml/nodes/ScalarNode.java
@@ -1,17 +1,15 @@
/**
- * Copyright (c) 2008, http://www.snakeyaml.org
+ * Copyright (c) 2008, SnakeYAML
*
- * 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
+ * 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
+ * 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.
+ * 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.yaml.snakeyaml.nodes;
@@ -26,100 +24,106 @@ import org.yaml.snakeyaml.error.Mark;
* </p>
*/
public class ScalarNode extends Node {
- private DumperOptions.ScalarStyle style;
- private String value;
- public ScalarNode(Tag tag, String value) {
- this(tag, value, null, null, ScalarStyle.PLAIN);
+ private final DumperOptions.ScalarStyle style;
+
+ private String value;
+
+ public ScalarNode(Tag tag, String value) {
+ this(tag, value, null, null, ScalarStyle.PLAIN);
+ }
+
+ public ScalarNode(Tag tag, String value, Mark startMark, Mark endMark,
+ DumperOptions.ScalarStyle style) {
+ this(tag, true, value, startMark, endMark, style);
+ }
+
+ public ScalarNode(Tag tag, boolean resolved, String value, Mark startMark, Mark endMark,
+ DumperOptions.ScalarStyle style) {
+ super(tag, startMark, endMark);
+ if (value == null) {
+ throw new NullPointerException("value in a Node is required.");
}
-
- public ScalarNode(Tag tag, String value, Mark startMark, Mark endMark, DumperOptions.ScalarStyle style) {
- this(tag, true, value, startMark, endMark, style);
+ this.value = value;
+ if (style == null) {
+ throw new NullPointerException("Scalar style must be provided.");
}
+ this.style = style;
+ this.resolved = resolved;
+ }
- public ScalarNode(Tag tag, boolean resolved, String value, Mark startMark, Mark endMark,
- DumperOptions.ScalarStyle style) {
- super(tag, startMark, endMark);
- if (value == null) {
- throw new NullPointerException("value in a Node is required.");
- }
- this.value = value;
- if (style == null) throw new NullPointerException("Scalar style must be provided.");
- this.style = style;
- this.resolved = resolved;
- }
+ /*
+ * Existed in older versions but replaced with {@link DumperOptions.ScalarStyle}-based
+ * constructor. Restored in v1.22 for backwards compatibility.
+ *
+ * @deprecated Since restored in v1.22. Use {@link ScalarNode#ScalarNode(Tag, String, Mark, Mark,
+ * org.yaml.snakeyaml.DumperOptions.ScalarStyle) }.
+ */
+ @Deprecated
+ public ScalarNode(Tag tag, String value, Mark startMark, Mark endMark, Character style) {
+ this(tag, value, startMark, endMark, DumperOptions.ScalarStyle.createStyle(style));
+ }
- /*
- * Existed in older versions but replaced with {@link DumperOptions.ScalarStyle}-based constructor.
- * Restored in v1.22 for backwards compatibility.
- * @deprecated Since restored in v1.22. Use {@link ScalarNode#ScalarNode(Tag, String, Mark, Mark, org.yaml.snakeyaml.DumperOptions.ScalarStyle) }.
- */
- @Deprecated
- public ScalarNode(Tag tag, String value, Mark startMark, Mark endMark, Character style) {
- this(tag, value, startMark, endMark, DumperOptions.ScalarStyle.createStyle(style));
- }
+ /*
+ * Existed in older versions but replaced with {@link DumperOptions.ScalarStyle}-based
+ * constructor. Restored in v1.22 for backwards compatibility.
+ *
+ * @deprecated Since restored in v1.22. Use {@link ScalarNode#ScalarNode(Tag, boolean, String,
+ * Mark, Mark, org.yaml.snakeyaml.DumperOptions.ScalarStyle) }.
+ */
+ @Deprecated
+ public ScalarNode(Tag tag, boolean resolved, String value, Mark startMark, Mark endMark,
+ Character style) {
+ this(tag, resolved, value, startMark, endMark, DumperOptions.ScalarStyle.createStyle(style));
+ }
- /*
- * Existed in older versions but replaced with {@link DumperOptions.ScalarStyle}-based constructor.
- * Restored in v1.22 for backwards compatibility.
- * @deprecated Since restored in v1.22. Use {@link ScalarNode#ScalarNode(Tag, boolean, String, Mark, Mark, org.yaml.snakeyaml.DumperOptions.ScalarStyle) }.
- */
- @Deprecated
- public ScalarNode(Tag tag, boolean resolved, String value, Mark startMark, Mark endMark,
- Character style) {
- this(tag, resolved, value, startMark, endMark, DumperOptions.ScalarStyle.createStyle(style));
- }
-
- /**
- * Get scalar style of this node.
- *
- * @see org.yaml.snakeyaml.events.ScalarEvent
- * @see <a href="http://yaml.org/spec/1.1/#id903915">Chapter 9. Scalar
- * Styles</a>
- * @return style of this scalar node
- * @deprecated use getScalarStyle instead
- */
- @Deprecated
- public Character getStyle() {
- return style.getChar();
- }
+ /**
+ * Get scalar style of this node.
+ *
+ * @see org.yaml.snakeyaml.events.ScalarEvent
+ * @see <a href="http://yaml.org/spec/1.1/#id903915">Chapter 9. Scalar Styles</a>
+ * @return style of this scalar node
+ * @deprecated use getScalarStyle instead
+ */
+ @Deprecated
+ public Character getStyle() {
+ return style.getChar();
+ }
- /**
- * Get scalar style of this node.
- *
- * @see org.yaml.snakeyaml.events.ScalarEvent
- * @see <a href="http://yaml.org/spec/1.1/#id903915">Chapter 9. Scalar
- * Styles</a>
- * @return style of this scalar node
- */
- public DumperOptions.ScalarStyle getScalarStyle() {
- return style;
- }
+ /**
+ * Get scalar style of this node.
+ *
+ * @see org.yaml.snakeyaml.events.ScalarEvent
+ * @see <a href="http://yaml.org/spec/1.1/#id903915">Chapter 9. Scalar Styles</a>
+ * @return style of this scalar node
+ */
+ public DumperOptions.ScalarStyle getScalarStyle() {
+ return style;
+ }
- @Override
- public NodeId getNodeId() {
- return NodeId.scalar;
- }
+ @Override
+ public NodeId getNodeId() {
+ return NodeId.scalar;
+ }
- /**
- * Value of this scalar.
- *
- * @return Scalar's value.
- */
- public String getValue() {
- return value;
- }
+ /**
+ * Value of this scalar.
+ *
+ * @return Scalar's value.
+ */
+ public String getValue() {
+ return value;
+ }
- public void setValue(String value) {
- this.value = value;
- }
-
- public String toString() {
- return "<" + this.getClass().getName() + " (tag=" + getTag() + ", value=" + getValue()
- + ")>";
- }
+ public void setValue(String value) {
+ this.value = value;
+ }
+
+ public String toString() {
+ return "<" + this.getClass().getName() + " (tag=" + getTag() + ", value=" + getValue() + ")>";
+ }
- public boolean isPlain() {
- return style == DumperOptions.ScalarStyle.PLAIN;
- }
+ public boolean isPlain() {
+ return style == DumperOptions.ScalarStyle.PLAIN;
+ }
}
diff --git a/server-core/src/main/java/org/yaml/snakeyaml/representer/Representer.java b/server-core/src/main/java/org/yaml/snakeyaml/representer/Representer.java
index b786dbcea2..761b5215c5 100644
--- a/server-core/src/main/java/org/yaml/snakeyaml/representer/Representer.java
+++ b/server-core/src/main/java/org/yaml/snakeyaml/representer/Representer.java
@@ -1,17 +1,15 @@
/**
- * Copyright (c) 2008, http://www.snakeyaml.org
+ * Copyright (c) 2008, SnakeYAML
*
- * 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
+ * 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
+ * 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.
+ * 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.yaml.snakeyaml.representer;
@@ -24,7 +22,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
-
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
import org.yaml.snakeyaml.TypeDescription;
@@ -43,238 +40,225 @@ import org.yaml.snakeyaml.nodes.Tag;
*/
public class Representer extends SafeRepresenter {
- protected Map<Class<? extends Object>, TypeDescription> typeDefinitions = Collections
- .emptyMap();
+ protected Map<Class<? extends Object>, TypeDescription> typeDefinitions = Collections.emptyMap();
- public Representer() {
- this.representers.put(null, new RepresentJavaBean());
- }
+ public Representer() {
+ this.representers.put(null, new RepresentJavaBean());
+ }
- public Representer(DumperOptions options) {
- super(options);
- this.representers.put(null, new RepresentJavaBean());
+ public Representer(DumperOptions options) {
+ super(options);
+ this.representers.put(null, new RepresentJavaBean());
+ }
+
+ public TypeDescription addTypeDescription(TypeDescription td) {
+ if (Collections.EMPTY_MAP == typeDefinitions) {
+ typeDefinitions = new HashMap<Class<? extends Object>, TypeDescription>();
}
-
- public TypeDescription addTypeDescription(TypeDescription td) {
- if (Collections.EMPTY_MAP == typeDefinitions) {
- typeDefinitions = new HashMap<Class<? extends Object>, TypeDescription>();
- }
- if (td.getTag() != null) {
- addClassTag(td.getType(), td.getTag());
- }
- td.setPropertyUtils(getPropertyUtils());
- return typeDefinitions.put(td.getType(), td);
+ if (td.getTag() != null) {
+ addClassTag(td.getType(), td.getTag());
}
+ td.setPropertyUtils(getPropertyUtils());
+ return typeDefinitions.put(td.getType(), td);
+ }
- @Override
- public void setPropertyUtils(PropertyUtils propertyUtils) {
- super.setPropertyUtils(propertyUtils);
- Collection<TypeDescription> tds = typeDefinitions.values();
- for (TypeDescription typeDescription : tds) {
- typeDescription.setPropertyUtils(propertyUtils);
- }
+ @Override
+ public void setPropertyUtils(PropertyUtils propertyUtils) {
+ super.setPropertyUtils(propertyUtils);
+ Collection<TypeDescription> tds = typeDefinitions.values();
+ for (TypeDescription typeDescription : tds) {
+ typeDescription.setPropertyUtils(propertyUtils);
}
+ }
- protected class RepresentJavaBean implements Represent {
- public Node representData(Object data) {
- return representJavaBean(getProperties(data.getClass()), data);
- }
+ protected class RepresentJavaBean implements Represent {
+
+ public Node representData(Object data) {
+ return representJavaBean(getProperties(data.getClass()), data);
}
+ }
- /**
- * Tag logic:
- * - explicit root tag is set in serializer
- * - if there is a predefined class tag it is used
- * - a global tag with class name is always used as tag. The JavaBean parent
- * of the specified JavaBean may set another tag (tag:yaml.org,2002:map)
- * when the property class is the same as runtime class
- *
- * @param properties
- * JavaBean getters
- * @param javaBean
- * instance for Node
- * @return Node to get serialized
- */
- protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
- List<NodeTuple> value = new ArrayList<NodeTuple>(properties.size());
- Tag tag;
- Tag customTag = classTags.get(javaBean.getClass());
- tag = customTag != null ? customTag : new Tag("!" + javaBean.getClass().getSimpleName());
- // flow style will be chosen by BaseRepresenter
- MappingNode node = new MappingNode(tag, value, FlowStyle.AUTO);
- representedObjects.put(javaBean, node);
- DumperOptions.FlowStyle bestStyle = FlowStyle.FLOW;
- for (Property property : properties) {
- Object memberValue = property.get(javaBean);
- Tag customPropertyTag = memberValue == null ? null
- : classTags.get(memberValue.getClass());
- NodeTuple tuple = representJavaBeanProperty(javaBean, property, memberValue,
- customPropertyTag);
- if (tuple == null) {
- continue;
- }
- if (!((ScalarNode) tuple.getKeyNode()).isPlain()) {
- bestStyle = FlowStyle.BLOCK;
- }
- Node nodeValue = tuple.getValueNode();
- if (!(nodeValue instanceof ScalarNode && ((ScalarNode) nodeValue).isPlain())) {
- bestStyle = FlowStyle.BLOCK;
- }
- value.add(tuple);
- }
- if (defaultFlowStyle != FlowStyle.AUTO) {
- node.setFlowStyle(defaultFlowStyle);
- } else {
- node.setFlowStyle(bestStyle);
- }
- return node;
+ /**
+ * Tag logic: - explicit root tag is set in serializer - if there is a predefined class tag it is
+ * used - a global tag with class name is always used as tag. The JavaBean parent of the specified
+ * JavaBean may set another tag (tag:yaml.org,2002:map) when the property class is the same as
+ * runtime class
+ *
+ * @param properties JavaBean getters
+ * @param javaBean instance for Node
+ * @return Node to get serialized
+ */
+ protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
+ List<NodeTuple> value = new ArrayList<NodeTuple>(properties.size());
+ Tag tag;
+ Tag customTag = classTags.get(javaBean.getClass());
+ tag = customTag != null ? customTag : new Tag("!" + javaBean.getClass().getSimpleName());
+ // flow style will be chosen by BaseRepresenter
+ MappingNode node = new MappingNode(tag, value, FlowStyle.AUTO);
+ representedObjects.put(javaBean, node);
+ DumperOptions.FlowStyle bestStyle = FlowStyle.FLOW;
+ for (Property property : properties) {
+ Object memberValue = property.get(javaBean);
+ Tag customPropertyTag = memberValue == null ? null : classTags.get(memberValue.getClass());
+ NodeTuple tuple =
+ representJavaBeanProperty(javaBean, property, memberValue, customPropertyTag);
+ if (tuple == null) {
+ continue;
+ }
+ if (!((ScalarNode) tuple.getKeyNode()).isPlain()) {
+ bestStyle = FlowStyle.BLOCK;
+ }
+ Node nodeValue = tuple.getValueNode();
+ if (!(nodeValue instanceof ScalarNode && ((ScalarNode) nodeValue).isPlain())) {
+ bestStyle = FlowStyle.BLOCK;
+ }
+ value.add(tuple);
+ }
+ if (defaultFlowStyle != FlowStyle.AUTO) {
+ node.setFlowStyle(defaultFlowStyle);
+ } else {
+ node.setFlowStyle(bestStyle);
}
+ return node;
+ }
- /**
- * Represent one JavaBean property.
- *
- * @param javaBean
- * - the instance to be represented
- * @param property
- * - the property of the instance
- * @param propertyValue
- * - value to be represented
- * @param customTag
- * - user defined Tag
- * @return NodeTuple to be used in a MappingNode. Return null to skip the
- * property
- */
- protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
- Object propertyValue, Tag customTag) {
- ScalarNode nodeKey = (ScalarNode) representData(property.getName());
- // the first occurrence of the node must keep the tag
- boolean hasAlias = this.representedObjects.containsKey(propertyValue);
+ /**
+ * Represent one JavaBean property.
+ *
+ * @param javaBean - the instance to be represented
+ * @param property - the property of the instance
+ * @param propertyValue - value to be represented
+ * @param customTag - user defined Tag
+ * @return NodeTuple to be used in a MappingNode. Return null to skip the property
+ */
+ protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
+ Object propertyValue, Tag customTag) {
+ ScalarNode nodeKey = (ScalarNode) representData(property.getName());
+ // the first occurrence of the node must keep the tag
+ boolean hasAlias = this.representedObjects.containsKey(propertyValue);
- Node nodeValue = representData(propertyValue);
+ Node nodeValue = representData(propertyValue);
- if (propertyValue != null && !hasAlias) {
- NodeId nodeId = nodeValue.getNodeId();
- if (customTag == null) {
- if (nodeId == NodeId.scalar) {
- //generic Enum requires the full tag
- if (property.getType() != java.lang.Enum.class) {
- if (propertyValue instanceof Enum<?>) {
- nodeValue.setTag(Tag.STR);
- }
- }
- } else {
- if (nodeId == NodeId.mapping) {
- if (property.getType() == propertyValue.getClass()) {
- if (!(propertyValue instanceof Map<?, ?>)) {
- if (!nodeValue.getTag().equals(Tag.SET)) {
- nodeValue.setTag(Tag.MAP);
- }
- }
- }
- }
- checkGlobalTag(property, nodeValue, propertyValue);
+ if (propertyValue != null && !hasAlias) {
+ NodeId nodeId = nodeValue.getNodeId();
+ if (customTag == null) {
+ if (nodeId == NodeId.scalar) {
+ // generic Enum requires the full tag
+ if (property.getType() != java.lang.Enum.class) {
+ if (propertyValue instanceof Enum<?>) {
+ nodeValue.setTag(Tag.STR);
+ }
+ }
+ } else {
+ if (nodeId == NodeId.mapping) {
+ if (property.getType() == propertyValue.getClass()) {
+ if (!(propertyValue instanceof Map<?, ?>)) {
+ if (!nodeValue.getTag().equals(Tag.SET)) {
+ nodeValue.setTag(Tag.MAP);
}
+ }
}
+ }
+ checkGlobalTag(property, nodeValue, propertyValue);
}
+ }
+ }
+
+ return new NodeTuple(nodeKey, nodeValue);
+ }
- return new NodeTuple(nodeKey, nodeValue);
+ /**
+ * Remove redundant global tag for a type safe (generic) collection if it is the same as defined
+ * by the JavaBean property
+ *
+ * @param property - JavaBean property
+ * @param node - representation of the property
+ * @param object - instance represented by the node
+ */
+ @SuppressWarnings("unchecked")
+ protected void checkGlobalTag(Property property, Node node, Object object) {
+ // Skip primitive arrays.
+ if (object.getClass().isArray() && object.getClass().getComponentType().isPrimitive()) {
+ return;
}
- /**
- * Remove redundant global tag for a type safe (generic) collection if it is
- * the same as defined by the JavaBean property
- *
- * @param property
- * - JavaBean property
- * @param node
- * - representation of the property
- * @param object
- * - instance represented by the node
- */
- @SuppressWarnings("unchecked")
- protected void checkGlobalTag(Property property, Node node, Object object) {
- // Skip primitive arrays.
- if (object.getClass().isArray() && object.getClass().getComponentType().isPrimitive()) {
- return;
+ Class<?>[] arguments = property.getActualTypeArguments();
+ if (arguments != null) {
+ if (node.getNodeId() == NodeId.sequence) {
+ // apply map tag where class is the same
+ Class<? extends Object> t = arguments[0];
+ SequenceNode snode = (SequenceNode) node;
+ Iterable<Object> memberList = Collections.EMPTY_LIST;
+ if (object.getClass().isArray()) {
+ memberList = Arrays.asList((Object[]) object);
+ } else if (object instanceof Iterable<?>) {
+ // list
+ memberList = (Iterable<Object>) object;
}
-
- Class<?>[] arguments = property.getActualTypeArguments();
- if (arguments != null) {
- if (node.getNodeId() == NodeId.sequence) {
- // apply map tag where class is the same
- Class<? extends Object> t = arguments[0];
- SequenceNode snode = (SequenceNode) node;
- Iterable<Object> memberList = Collections.EMPTY_LIST;
- if (object.getClass().isArray()) {
- memberList = Arrays.asList((Object[]) object);
- } else if (object instanceof Iterable<?>) {
- // list
- memberList = (Iterable<Object>) object;
- }
- Iterator<Object> iter = memberList.iterator();
- if (iter.hasNext()) {
- for (Node childNode : snode.getValue()) {
- Object member = iter.next();
- if (member != null) {
- if (t.equals(member.getClass()))
- if (childNode.getNodeId() == NodeId.mapping) {
- childNode.setTag(Tag.MAP);
- }
- }
- }
- }
- } else if (object instanceof Set) {
- Class<?> t = arguments[0];
- MappingNode mnode = (MappingNode) node;
- Iterator<NodeTuple> iter = mnode.getValue().iterator();
- Set<?> set = (Set<?>) object;
- for (Object member : set) {
- NodeTuple tuple = iter.next();
- Node keyNode = tuple.getKeyNode();
- if (t.equals(member.getClass())) {
- if (keyNode.getNodeId() == NodeId.mapping) {
- keyNode.setTag(Tag.MAP);
- }
- }
+ Iterator<Object> iter = memberList.iterator();
+ if (iter.hasNext()) {
+ for (Node childNode : snode.getValue()) {
+ Object member = iter.next();
+ if (member != null) {
+ if (t.equals(member.getClass())) {
+ if (childNode.getNodeId() == NodeId.mapping) {
+ childNode.setTag(Tag.MAP);
}
- } else if (object instanceof Map) { // NodeId.mapping ends-up here
- Class<?> keyType = arguments[0];
- Class<?> valueType = arguments[1];
- MappingNode mnode = (MappingNode) node;
- for (NodeTuple tuple : mnode.getValue()) {
- resetTag(keyType, tuple.getKeyNode());
- resetTag(valueType, tuple.getValueNode());
- }
- } else {
- // the type for collection entries cannot be
- // detected
+ }
}
+ }
}
- }
-
- private void resetTag(Class<? extends Object> type, Node node) {
- Tag tag = node.getTag();
- if (tag.matches(type)) {
- if (Enum.class.isAssignableFrom(type)) {
- node.setTag(Tag.STR);
- } else {
- node.setTag(Tag.MAP);
+ } else if (object instanceof Set) {
+ Class<?> t = arguments[0];
+ MappingNode mnode = (MappingNode) node;
+ Iterator<NodeTuple> iter = mnode.getValue().iterator();
+ Set<?> set = (Set<?>) object;
+ for (Object member : set) {
+ NodeTuple tuple = iter.next();
+ Node keyNode = tuple.getKeyNode();
+ if (t.equals(member.getClass())) {
+ if (keyNode.getNodeId() == NodeId.mapping) {
+ keyNode.setTag(Tag.MAP);
}
+ }
+ }
+ } else if (object instanceof Map) { // NodeId.mapping ends-up here
+ Class<?> keyType = arguments[0];
+ Class<?> valueType = arguments[1];
+ MappingNode mnode = (MappingNode) node;
+ for (NodeTuple tuple : mnode.getValue()) {
+ resetTag(keyType, tuple.getKeyNode());
+ resetTag(valueType, tuple.getValueNode());
}
+ } else {
+ // the type for collection entries cannot be
+ // detected
+ }
}
+ }
- /**
- * Get JavaBean properties to be serialised. The order is respected. This
- * method may be overridden to provide custom property selection or order.
- *
- * @param type
- * - JavaBean to inspect the properties
- * @return properties to serialise
- */
- protected Set<Property> getProperties(Class<? extends Object> type) {
- if (typeDefinitions.containsKey(type)) {
- return typeDefinitions.get(type).getProperties();
- }
- return getPropertyUtils().getProperties(type);
+ private void resetTag(Class<? extends Object> type, Node node) {
+ Tag tag = node.getTag();
+ if (tag.matches(type)) {
+ if (Enum.class.isAssignableFrom(type)) {
+ node.setTag(Tag.STR);
+ } else {
+ node.setTag(Tag.MAP);
+ }
+ }
+ }
+
+ /**
+ * Get JavaBean properties to be serialised. The order is respected. This method may be overridden
+ * to provide custom property selection or order.
+ *
+ * @param type - JavaBean to inspect the properties
+ * @return properties to serialise
+ */
+ protected Set<Property> getProperties(Class<? extends Object> type) {
+ if (typeDefinitions.containsKey(type)) {
+ return typeDefinitions.get(type).getProperties();
}
+ return getPropertyUtils().getProperties(type);
+ }
}