ByteBuddy enhancement generates faulty code with many-to-many associations

Description

SpotBugs reports a high-prio warning in entity classes that have been enhanced with ByteBuddy enhancement:


EC: Using pointer equality to compare different types (EC_UNRELATED_TYPES_USING_POINTER_EQUALITY)

This method uses pointer equality to compare two references that seem to be of different types. The result of this comparison will always be false at runtime.


The faulty code is in the generated many-to-many handler functions. Here's the relevant part of org.hibernate.bytecode.enhance.internal.bytebuddy.CodeTemplates

 

I believe that the intended conditions in the if-statement in line 21 should have been:

The bug has been around at least since 5.3.27, probably even eralier, and is still there in the current version.

Activity

Show:

Steve Ebersole April 24, 2023 at 6:19 PM

I don’t see why you need a reproducer, just look at the code.

Why projects generally need reproducers… to ensure we do not later introduce a regression. But no worries.

Thomas Mogwitz April 13, 2023 at 8:23 AM

I’m not trying anything in particular. I’m using Hibernate bytecode enhancement with <enableAssociationManagement>true</enableAssociationManagement>.

And I’m using SpotBugs to keep the code clean. SpotBugs’s specialty is that it scans class files (ie the bytecode) instead of the sources. So as I said, SpotBugs reported an error in the code generated by Hibernate. (see bug description)

As you probably know this code is generated from templates residing in org.hibernate.bytecode.enhance.internal.bytebuddy.CodeTemplates. If you take a closer look at the code, you can see the line this faulty generated code stems from, and you can see that it makes no sense (see bug description - line 21 - marked with a comment)

Since we have a clean code policy I tried to fix this by having a modified copy of CodeTemplate.class in one of our jars, which in turn is used as a dependency to hibernate-enhance-maven-plugin, so the modified CodeTemplate.class is in the class path before the original class from Hibernate. But this hack is not beautiful and needs to reviewed every time the Hibernate version changes, so I’d like you to fix this bug.

I don’t see why you need a reproducer, just look at the code.

Steve Ebersole April 12, 2023 at 3:03 PM

While creating a full test case here would be tough, could you at least explain what you are trying? We are going to need to create a reproducer.

Fixed

Details

Assignee

Reporter

Affects versions

Priority

Created October 13, 2022 at 2:30 PM
Updated April 12, 2024 at 9:30 AM
Resolved April 24, 2023 at 6:09 PM