import java.lang.reflect.Field;
public class ReflectGetFields {
public void modeString() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException{
ReflectionPoint rp = new ReflectionPoint(1,2);
Field[] fields = rp.getClass().getFields();
System.out.println(fields);
for (int i = 0; i < fields.length; i++) {
if(fields[i].get(rp)==Class.forName("java.lang.String"));
{
String sss = (String) fields[i].get(rp);
StringBuilder strb = new StringBuilder(sss);
for (int a=0;i<strb.length();i++) {
if (strb.charAt(a)=='b') {
strb.setCharAt(a, 'a');
}
}
}
}
System.out.println(rp.toString());
}
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException {
ReflectGetFields rgf = new ReflectGetFields();
rgf.modeString();
}
}
Eclipse says :
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at com.github.user3301.Reflection.ReflectGetFields.modeString(ReflectGetFields.java:11)
at com.github.user3301.Reflection.ReflectGetFields.main(ReflectGetFields.java:36)
I just don't know where did I do wrong as fields[i].get(rp) returns a field in the class and there is a if statement to check if it is a String type, so why it cannot be cast into a String type?
Aucun commentaire:
Enregistrer un commentaire