Fix PropertyBindings bugs

This commit is contained in:
Reinhard Pointner 2017-06-05 15:19:36 +08:00
parent ba024fcdc9
commit 41bb712dc6
4 changed files with 8 additions and 32 deletions

View File

@ -23,13 +23,7 @@ public class AssociativeScriptObject extends GroovyObjectSupport implements Iter
@Override @Override
public Object getProperty(String name) { public Object getProperty(String name) {
Object value = properties.get(name); return properties.get(name);
if (value != null) {
return value;
}
return super.getProperty(name); // throw MissingPropertyException
} }
@Override @Override

View File

@ -6,11 +6,11 @@ public class BindingException extends RuntimeException {
super(message, cause); super(message, cause);
} }
public BindingException(String binding, String innerMessage) { public BindingException(Object binding, String innerMessage) {
this(binding, innerMessage, null); this(binding, innerMessage, null);
} }
public BindingException(String binding, String innerMessage, Throwable cause) { public BindingException(Object binding, String innerMessage, Throwable cause) {
this(String.format("Binding \"%s\": %s", binding, innerMessage), cause); this(String.format("Binding \"%s\": %s", binding, innerMessage), cause);
} }

View File

@ -67,7 +67,7 @@ public class ExpressionBindings extends AbstractMap<String, Object> implements B
if (e.getCause() instanceof BindingException) { if (e.getCause() instanceof BindingException) {
throw (BindingException) e.getCause(); throw (BindingException) e.getCause();
} }
throw new BindingException(key.toString(), getRootCauseMessage(e), e); throw new BindingException(key, getRootCauseMessage(e), e);
} }
} }
return null; return null;

View File

@ -1,6 +1,7 @@
package net.filebot.format; package net.filebot.format;
import static net.filebot.util.ExceptionUtilities.*;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.AbstractMap; import java.util.AbstractMap;
@ -9,7 +10,6 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
/* /*
* Used to create a map view of the properties of an Object * Used to create a map view of the properties of an Object
*/ */
@ -18,12 +18,8 @@ public class PropertyBindings extends AbstractMap<String, Object> {
private final Object object; private final Object object;
private final Map<String, Object> properties = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER); private final Map<String, Object> properties = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
private final Object defaultValue; public PropertyBindings(Object object) {
public PropertyBindings(Object object, Object defaultValue) {
this.object = object; this.object = object;
this.defaultValue = defaultValue;
// get method bindings // get method bindings
for (Method method : object.getClass().getMethods()) { for (Method method : object.getClass().getMethods()) {
@ -34,14 +30,13 @@ public class PropertyBindings extends AbstractMap<String, Object> {
} }
// boolean properties // boolean properties
if (method.getName().length() > 2 && method.getName().substring(0, 3).equalsIgnoreCase("is")) { if (method.getName().length() > 2 && method.getName().substring(0, 2).equalsIgnoreCase("is")) {
properties.put(method.getName().substring(2), method); properties.put(method.getName().substring(2), method);
} }
} }
} }
} }
@Override @Override
public Object get(Object key) { public Object get(Object key) {
Object value = properties.get(key); Object value = properties.get(key);
@ -50,55 +45,44 @@ public class PropertyBindings extends AbstractMap<String, Object> {
if (value instanceof Method) { if (value instanceof Method) {
try { try {
value = ((Method) value).invoke(object); value = ((Method) value).invoke(object);
if (value == null) {
value = defaultValue;
}
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new BindingException(key, getRootCauseMessage(e), e);
} }
} }
return value; return value;
} }
@Override @Override
public Object put(String key, Object value) { public Object put(String key, Object value) {
return properties.put(key, value); return properties.put(key, value);
} }
@Override @Override
public Object remove(Object key) { public Object remove(Object key) {
return properties.remove(key); return properties.remove(key);
} }
@Override @Override
public boolean containsKey(Object key) { public boolean containsKey(Object key) {
return properties.containsKey(key); return properties.containsKey(key);
} }
@Override @Override
public Set<String> keySet() { public Set<String> keySet() {
return properties.keySet(); return properties.keySet();
} }
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
return properties.isEmpty(); return properties.isEmpty();
} }
@Override @Override
public String toString() { public String toString() {
return properties.toString(); return properties.toString();
} }
@Override @Override
public Set<Entry<String, Object>> entrySet() { public Set<Entry<String, Object>> entrySet() {
Set<Entry<String, Object>> entrySet = new HashSet<Entry<String, Object>>(); Set<Entry<String, Object>> entrySet = new HashSet<Entry<String, Object>>();
@ -111,13 +95,11 @@ public class PropertyBindings extends AbstractMap<String, Object> {
return key; return key;
} }
@Override @Override
public Object getValue() { public Object getValue() {
return get(key); return get(key);
} }
@Override @Override
public Object setValue(Object value) { public Object setValue(Object value) {
return put(key, value); return put(key, value);