I have a model class in java and I overwrote the toString to provide me with a custom toString. The toString uses reflection to find the field names and values and it works when I run it locally via my ide. However when I run it via mvn agents I always seem to get the error:
java.lang.ClassCastException: [Z cannot be cast to [Ljava.lang.String
Here is the toString:
@SneakyThrows
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Class<?> thisClass = Class.forName(this.getClass().getName());
Field[] aClassFields = thisClass.getDeclaredFields();
for (Field f : aClassFields) {
String fName = f.getName();
fName = fName.startsWith("_") ? fName.substring(1) : fName;
if (null != f.get(this)) {
if (f.get(this) instanceof String || f.get(this) instanceof List) {
sb.append(getVariableNameStr(fName, f.get(this).toString()));
} else {
StringBuilder stringArrayStr = new StringBuilder();
for (String s : (String[]) f.get(this)) {
stringArrayStr.append(fName).append(": ").append(s).append(", ");
}
sb.append(stringArrayStr);
}
}
}
return sb.toString().substring(0, sb.toString().length() - 2);
}
The line it fails on in the code is the following:
for (String s : (String[]) f.get(this)) {
Why does this pass locally and fail using mvn? Can anybody tell me what is incorrect about this line?
Just to clear up - the model class has 3 types of field - String, List and String array. The errored line occurs on String array entries.
A
Aucun commentaire:
Enregistrer un commentaire