Synthetic Fields Handling. JaCoCo.

557 views
Skip to first unread message

Dima Kolomiyets

unread,
Aug 2, 2018, 3:06:30 AM8/2/18
to XStream User
Hello,

we are using XStream v1.2.2 in the project, and JaCoCo in the CI.

The tests are failing with an ObjectAccessException ( see xstream-jacoco-stacktrace.txt )

After some googling a have found, that:
"To collect execution data JaCoCo instruments the classes under test which adds two members to the classes: A private static field $jacocoData and a private static method $jacocoInit(). Both members are marked as synthetic.

Please change your code to ignore synthetic members. This is a good practice anyways as also the Java compiler creates synthetic members in certain situation."
(https://www.eclemma.org/jacoco/trunk/doc/faq.html)

In the sources of the XStream I have found PureJavaReflectionProvider:151

protected boolean fieldModifiersSupported(Field field) {
return !(Modifier.isStatic(field.getModifiers())
|| Modifier.isTransient(field.getModifiers()));
}

So, there is no handling of synthetic fields (" || field.isSynthetic()") (?).

I took a look at v1.4.9, and the situation stays pretty much the same:

protected boolean fieldModifiersSupported(Field field) {
int modifiers = field.getModifiers();
return !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers));
}

Question:
Is there a way to bypass the issue while staying with version 1.2.2 ?
Should we report a bug ?

Kind regards,

Dmytro

xstream-jacoco-stacktrace.txt

Jörg Schaible

unread,
Aug 2, 2018, 8:10:50 PM8/2/18
to xstrea...@googlegroups.com
Hi Dima,

On Thu, 02 Aug 2018 00:06:30 -0700 Dima Kolomiyets wrote:

> Hello,
>
> we are using XStream v1.2.2 in the project, and JaCoCo in the CI.
>
> The tests are failing with an ObjectAccessException ( see
> xstream-jacoco-stacktrace.txt )
>
> After some googling a have found, that:
> "To collect execution data JaCoCo instruments the classes under test
> which adds two members to the classes: A private static field
> $jacocoData and a private static method $jacocoInit(). Both members are
> marked as synthetic.
>
> Please change your code to ignore synthetic members. This is a good
> practice anyways as also the Java compiler creates synthetic members in
> certain situation."
> (https://www.eclemma.org/jacoco/trunk/doc/faq.html)

This might be a good advice for standard application, but not necessarily in every case and not for such
specialized libraries like XStream. XStream serialized dynamic proxies, certain CGLIB proxies, certain lambda
objects and nested classes. All use synthetic stuff and XStream will fail its unit tests, if those are omitted.

> In the sources of the XStream I have found
> PureJavaReflectionProvider:151
>
> protected boolean fieldModifiersSupported(Field field) {
> return !(Modifier.isStatic(field.getModifiers())
> || Modifier.isTransient(field.getModifiers()));
> }
>
> So, there is no handling of synthetic fields (" || field.isSynthetic()")
> (?).

On purpose.

> I took a look at v1.4.9, and the situation stays pretty much the same:
>
> protected boolean fieldModifiersSupported(Field field) {
> int modifiers = field.getModifiers();
> return !(Modifier.isStatic(modifiers) ||
> Modifier.isTransient(modifiers));
> }
>
> Question:
> Is there a way to bypass the issue while staying with version 1.2.2 ?

The method is protected, you can always override with your version and use those converters for your
purpose instead. If the objects you're serializing do not have any synthetic stuff, you can get away with it.
Anyway, nobody will do something for a version more than a decade old.

> Should we report a bug ?

You can, but I would close it as invalid.

Let's turn it the other way round: Cobertura does not create such problems ;-)

Yeah, I know, that it is no longer maintained ...

Cheers,
Jörg

Jörg Schaible

unread,
Aug 15, 2018, 7:17:13 PM8/15/18
to xstrea...@googlegroups.com
Hi Dima,

On Fri, 03 Aug 2018 00:08:30 +0000 Jörg Schaible wrote:

> Hi Dima,
>
> On Thu, 02 Aug 2018 00:06:30 -0700 Dima Kolomiyets wrote:
>
>> Hello,
>>
>> we are using XStream v1.2.2 in the project, and JaCoCo in the CI.
>>
>> The tests are failing with an ObjectAccessException ( see
>> xstream-jacoco-stacktrace.txt )
>>
>> After some googling a have found, that:
>> "To collect execution data JaCoCo instruments the classes under test
>> which adds two members to the classes: A private static field
>> $jacocoData and a private static method $jacocoInit(). Both members are
>> marked as synthetic.
>>
>> Please change your code to ignore synthetic members. This is a good
>> practice anyways as also the Java compiler creates synthetic members in
>> certain situation."
>> (https://www.eclemma.org/jacoco/trunk/doc/faq.html)
>
> This might be a good advice for standard application, but not
> necessarily in every case and not for such specialized libraries like
> XStream. XStream serialized dynamic proxies, certain CGLIB proxies,
> certain lambda objects and nested classes. All use synthetic stuff and
> XStream will fail its unit tests, if those are omitted.

XStream's FieldDictionary will ignore now any synthetic field having a name starting with "$jacoco". IHMO it is
a valid guess, that the occurrence of such a field simply hints for a code coverage run with JaCoCo and will
never appear in production.

[snip]

Cheers,
Jörg

Dima Kolomiyets

unread,
Aug 20, 2018, 5:34:10 AM8/20/18
to XStream User
четвер, 16 серпня 2018 р. 01:17:13 UTC+2 користувач joehni написав:
Hello, Jörg,
sincerely thank you for the fix.

When will be the nearest release with this fix ?

Jörg Schaible

unread,
Aug 20, 2018, 8:10:22 PM8/20/18
to xstrea...@googlegroups.com
Hi Dima,

On Mon, 20 Aug 2018 02:34:09 -0700 Dima Kolomiyets wrote:

[snip]

> Hello, Jörg,
> sincerely thank you for the fix.
>
> When will be the nearest release with this fix ?

There are some open issues. I am working on it. But I cannot state a concrete date.

Regards,
Jörg

Reply all
Reply to author
Forward
0 new messages