How to iterate an attribute with reflection Java?

4

In the method below, I use reflection to set attributes of one class in another, based on annotation mapping. How do I map an attribute of a class that is an attribute of another?

For example, when using the following annotation: @Origin(field="modeloCarro.nome") I want to access the attribute attribute name modeloCarro .

/*
 * Method should be implemented for copying non homonyms fields.
 * 
 * @param T dto is the object that will receive the values.
 * 
 * @param Object obj is the original object
 */
public void toDTOMappedFields(Object dto, Object obj) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    Class<?> classOriginal = (Class<?>) obj.getClass();
    Class<?> classDTO = (Class<?>) dto.getClass();

    for (Field fieldDTO : classDTO.getDeclaredFields()) {
        if (fieldDTO.isAnnotationPresent(Origin.class)) {
            Origin origin = fieldDTO.getAnnotation(Origin.class);
            Field fieldOrigin = classOriginal.getDeclaredField(origin.field());
            fieldOrigin.setAccessible(true);
            fieldDTO.setAccessible(true);
            fieldDTO.set(dto, fieldOrigin.get(obj));
        }
    }
}
    
asked by anonymous 10.11.2015 / 14:39

2 answers

0

Being straightforward and without reinventing the wheel. Take a look at Apache Commons BeanUtils .

There is even a method BeanUtils.copyProperties that does exactly what you need.

Keep in mind that some exception handling will be required.

I hope I have helped.

    
11.11.2015 / 02:27
1

I think what you want is this, or something very similar:

/*
 * Method should be implemented for copying non homonyms fields.
 * 
 * @param T dto is the object that will receive the values.
 * 
 * @param Object obj is the original object
 */
public void toDTOMappedFields(Object dto, Object obj) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    for (Field fieldDTO : dto.getClass().getDeclaredFields()) {
        if (fieldDTO.isAnnotationPresent(Origin.class)) {
            Origin origin = fieldDTO.getAnnotation(Origin.class);
            String attributeRef = origin.field();
            String[] parts = attributeRef.split("\.");
            Object ref = obj;
            for (String p : parts) {
                Field fieldOrigin = obj.getClass().getDeclaredField(p);
                fieldOrigin.setAccessible(true);
                ref = fieldOrigin.get(ref);
            }
            fieldDTO.setAccessible(true);
            fieldDTO.set(dto, ref);
        }
    }
}

Obviously, you still need to treat a bunch of possible exceptions, if some field does not exist ( NoSuchFieldException ), or if you pass through references, you have a null in the middle of the path ( NullPointerException ), or you you have problems with security permissions on reflection ( SecurityException ), or problems with data access of wrong types ( IllegalArgumentException ). IllegalAccessException should never occur.

    
10.11.2015 / 15:07