try {
List<?> l = List.class.cast(obj);
// do extra validation:
for(Object o : l) {
if(!(o instanceof Class)) {
throw new WrongParameterValueException("Wrong parameter format for parameter \"" + getName() + "\". Given list contains objects of different type!");
}
}
// TODO: can we use reflection to get extra checks?
// TODO: Should we copy the list?
return (List<Class<? extends C>>) l;
}
catch(ClassCastException e) {
// continue with others
}
// Did we get a single class?
try {
if(restrictionClass.isAssignableFrom((Class<?>) obj)) {
List<Class<? extends C>> clss = new ArrayList<Class<? extends C>>(1);
clss.add((Class<? extends C>) obj);
return clss;
}
}
catch(ClassCastException e) {
// continue with others
}
if(obj instanceof String) {
String[] classes = SPLIT.split((String) obj);
// TODO: allow empty lists (and list constraints) to enforce length?
if(classes.length == 0) {
throw new UnspecifiedParameterException("Wrong parameter format! Given list of classes for parameter \"" + getName() + "\" is either empty or has the wrong format!");
}
List<Class<? extends C>> cls = new ArrayList<Class<? extends C>>(classes.length);
for(String cl : classes) {
try {
Class<?> c;
try {
c = Class.forName(cl);
}
catch(ClassNotFoundException e) {
// try in package of restriction class
c = Class.forName(restrictionClass.getPackage().getName() + "." + cl);
}
// Redundant check, also in validate(), but not expensive.
if(!restrictionClass.isAssignableFrom(c)) {
throw new WrongParameterValueException(this, cl, "Class \"" + cl + "\" does not extend/implement restriction class " + restrictionClass + ".\n");
}
else {
cls.add((Class<? extends C>) c);
}
}
catch(ClassNotFoundException e) {
throw new WrongParameterValueException(this, cl, "Class \"" + cl + "\" not found.\n", e);
}
}
return cls;
}
// INCOMPLETE
throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a list of Class values!");
}