vendredi 25 décembre 2015

Casting an object to a another certain object, then modifying it

So here's what I'm trying to do. At the beginning of the program, the class looks through all the fields in that class. If the field is a specific type of object, then it adds it to the list. The list of certain objects can be called later on, and modified.

SORRY, forgot to post the actual error. When I try to cast the class to the type Value, I get this error:

Cannot cast from Class<capture#8-of ?> to Value<?>

Here's the main part:

private List<Value> values = new ArrayList<Value>();

for (Field f : getClass().getDeclaredFields()) {
        if (f == null)
            continue;
        Class<?> clazz = f.getType();
        if (clazz.isAssignableFrom(Value.class)) {
            Value<?> value = (Value) clazz;
            list.add(value);
        }
    }

Here's the Value class:

public class Value<E> {

private String name;
private E value;

public Value(final String name, final E value) {
    this.name = name;
    this.value = value;
}

public final String getValueName() {
    return name;
}

public final E getValue() {
    return value;
}

public final void setValue(final E newValue) {
    value = newValue;
}

Is there any way to do this?





Aucun commentaire:

Enregistrer un commentaire