Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

This instrumented code fails to execute

Giovanni Lovato March 22, 2019

This code:

var reflections = new Reflections("my.package");
var types = reflections
.getTypesAnnotatedWith(GenerateMessages.class)
.stream();
types.map(Class::getFields).flatMap(Stream::of)
.forEach(field -> {
try {
String key = (String) field.get(null); // <- FAILS HERE
} catch (IllegalArgumentException | IllegalAccessException e) {
this.getLogger().error("Cannot access field", e);
}
});

fails when instrumented by Clover on the line commented with FAILS HERE. This is the thrown exception:

java.lang.ClassCastException: class com_atlassian_clover.TestNameSniffer$1 
cannot be cast to class java.lang.String
(com_atlassian_clover.TestNameSniffer$1 is in unnamed module of loader
java.net.URLClassLoader @3141366; java.lang.String is in module java.base of loader 'bootstrap')

 

1 answer

1 accepted

0 votes
Answer accepted
Giovanni Lovato March 22, 2019

I've just realized the code assumes that the fields of those classes are of type String, since that is the adopted convention in that project, but I assume Clover is adding other fields so I need to be more careful on casting!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events