Setting fields

We can also call setAccessible on Field objects, and then we can even set the value of private fields using reflection. Without further fake stories, just for the sake of this example, let's make a ConsistencyChecker named SettableChecker:

@Component 
@PoweredDevice 
public class SettableChecker implements ConsistencyChecker { 
    private static final Logger log = LoggerFactory.getLogger(SettableChecker.class); 

    private boolean setValue = false; 

    public boolean isInconsistent(Order order) { 
        return setValue; 
    } 
}

This checker will return false, unless we set the field to true using reflection. We do set it as such. We create a method in the Checker class and invoke it from the checking process for each checker:

private void setValueInChecker(ConsistencyChecker ...

Get Java Projects - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.