From ecb417e1158b2788d4dcc09e7026bbaf339c9d59 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Sun, 12 Mar 2017 21:25:18 +0100 Subject: [PATCH] Remove invalid characters from field names in YAML data. --- src/com/esotericsoftware/yamlbeans/Beans.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/com/esotericsoftware/yamlbeans/Beans.java b/src/com/esotericsoftware/yamlbeans/Beans.java index 7fb00cc..d7d1b06 100644 --- a/src/com/esotericsoftware/yamlbeans/Beans.java +++ b/src/com/esotericsoftware/yamlbeans/Beans.java @@ -155,11 +155,20 @@ static public Set getProperties (Class type, boolean beanProperties, b return properties; } + static private String toJavaIdentifier (String name) { + StringBuilder buffer = new StringBuilder(); + for (int i = 0, n = name.length(); i < n; i++) { + char c = name.charAt(i); + if (Character.isJavaIdentifierPart(c)) buffer.append(c); + } + return buffer.toString(); + } + static public Property getProperty (Class type, String name, boolean beanProperties, boolean privateFields, YamlConfig config) { if (type == null) throw new IllegalArgumentException("type cannot be null."); if (name == null || name.length() == 0) throw new IllegalArgumentException("name cannot be null or empty."); - name = name.replace(" ", ""); + name = toJavaIdentifier(name); if (beanProperties) { DeferredConstruction deferredConstruction = getDeferredConstruction(type, config); @@ -210,7 +219,7 @@ static private ArrayList getAllFields (Class type) { nextClass = nextClass.getSuperclass(); } ArrayList allFields = new ArrayList(); - for(int i = classes.size() - 1; i >= 0; i--) { + for (int i = classes.size() - 1; i >= 0; i--) { Collections.addAll(allFields, classes.get(i).getDeclaredFields()); } return allFields;