ConfigUtil: Accept collections and maps as attributes
For now we allow them to exist by skipping them during load and store operations so that it's the caller responsibility to handle store and load. Later we could do better, by passing closure and invoking it from load and store methods. Change-Id: Id665e48f25767aa06b47857405cc566fc045423c
This commit is contained in:
committed by
David Pursehouse
parent
a8ba8665a5
commit
d287ab744d
@@ -23,7 +23,9 @@ import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -281,7 +283,7 @@ public class ConfigUtil {
|
||||
f.setAccessible(true);
|
||||
Object c = f.get(s);
|
||||
Object d = f.get(defaults);
|
||||
if (!isString(t)) {
|
||||
if (!isString(t) && !isCollectionOrMap(t)) {
|
||||
Preconditions.checkNotNull(d, "Default cannot be null for: " + n);
|
||||
}
|
||||
if (c == null || c.equals(d)) {
|
||||
@@ -297,6 +299,9 @@ public class ConfigUtil {
|
||||
cfg.setBoolean(section, sub, n, (Boolean) c);
|
||||
} else if (t.isEnum()) {
|
||||
cfg.setEnum(section, sub, n, (Enum<?>) c);
|
||||
} else if (isCollectionOrMap(t)) {
|
||||
// TODO(davido): accept closure passed in from caller
|
||||
continue;
|
||||
} else {
|
||||
throw new ConfigInvalidException("type is unknown: " + t.getName());
|
||||
}
|
||||
@@ -337,7 +342,7 @@ public class ConfigUtil {
|
||||
String n = f.getName();
|
||||
f.setAccessible(true);
|
||||
Object d = f.get(defaults);
|
||||
if (!isString(t)) {
|
||||
if (!isString(t) && !isCollectionOrMap(t)) {
|
||||
Preconditions.checkNotNull(d, "Default cannot be null for: " + n);
|
||||
}
|
||||
if (isString(t)) {
|
||||
@@ -357,6 +362,9 @@ public class ConfigUtil {
|
||||
}
|
||||
} else if (t.isEnum()) {
|
||||
f.set(s, cfg.getEnum(section, sub, n, (Enum<?>) d));
|
||||
} else if (isCollectionOrMap(t)) {
|
||||
// TODO(davido): accept closure passed in from caller
|
||||
continue;
|
||||
} else {
|
||||
throw new ConfigInvalidException("type is unknown: " + t.getName());
|
||||
}
|
||||
@@ -374,6 +382,11 @@ public class ConfigUtil {
|
||||
return s;
|
||||
}
|
||||
|
||||
private static boolean isCollectionOrMap(Class<?> t) {
|
||||
return Collection.class.isAssignableFrom(t)
|
||||
|| Map.class.isAssignableFrom(t);
|
||||
}
|
||||
|
||||
private static boolean skipField(Field field) {
|
||||
int modifiers = field.getModifiers();
|
||||
return Modifier.isFinal(modifiers) || Modifier.isTransient(modifiers);
|
||||
|
||||
@@ -27,6 +27,8 @@ import com.google.gerrit.extensions.client.Theme;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ConfigUtilTest {
|
||||
@@ -50,6 +52,8 @@ public class ConfigUtilTest {
|
||||
public String nd;
|
||||
public Theme t;
|
||||
public Theme td;
|
||||
public List<String> list;
|
||||
public Map<String, String> map;
|
||||
static SectionInfo defaults() {
|
||||
SectionInfo i = new SectionInfo();
|
||||
i.i = 1;
|
||||
|
||||
Reference in New Issue
Block a user