mardi 24 septembre 2019

Java reflection: check if Field an object

I have the below function, i want to check the field passed in parameters of type Object or (String, int, boolean...), is it possible to do this check?

private void delete(final Object source, Field field) throws IllegalAccessException {
    if (field.isAnnotationPresent(DBRef.class) && field.isAnnotationPresent(CascadeDelete.class)) {
        final Object fieldValue = field.get(source);

        if (fieldValue instanceof List<?>) {
            for (Object item : (List<?>) fieldValue) {
                checkNDelete(item);
            }
        } else {
            checkNDelete(fieldValue);
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire