The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

thewonggei (3) [Avatar] Offline
#1
There is a very minor bug in the getField method when the RuntimeException is actually thrown. At this point the value of clazz is null and so the exception's output states that a field cannot be found on "class null". When I adapted this code I just created a local variable

Class originalClazz = clazz;

and then changed the throwing of the exception to use originalClazz instead of clazz.
felipe.leme (18) [Avatar] Offline
#2
Re: Bug in TestingHelper class in Listing 18.8.
Hi,

Thanks for pointing out, I changed it to:


private static Field getField(final Class<?> clazz, String fieldName) {
Class<?> tmpClass = clazz;
do {
for ( Field field : tmpClass.getDeclaredFields() ) {
String candidateName = field.getName();
if ( ! candidateName.equals(fieldName) ) {
continue;
}
field.setAccessible(true);
return field;
}
tmpClass = tmpClass.getSuperclass();
} while ( tmpClass != null );
throw new RuntimeException("Field '" + fieldName + "' not found on class " + clazz);
}


-- Felipe
thewonggei (3) [Avatar] Offline
#3
Re: Bug in TestingHelper class in Listing 18.8.
That's even better. Thank you for respoding Felipe!