Skip to content

Conversation

@DomGarguilo
Copy link
Member

@DomGarguilo DomGarguilo commented Aug 7, 2025

This PR fixes the warnings from the core module. Here is a description from the ticket:

This warning gets emitted when an exception is thrown within an constructor.

There are several ways to avoid this warning:

  1. make the class final
  2. remove the thrown exception from the constructor
  3. add a finalize() method so it cant be overridden
  4. make the constructor private

There are pros and cons to each fix.

  • making a class final prohibits inheriting it
  • removing the thrown exception usually means forgoing validation
  • adding a finalize() method means we also need to ignore the deprecation warning since finalizer() is deprecated
  • making the constructor private usually takes a lot of refactoring to move to a different pattern to create the object

I think the best approach here is to work through a couple modules at a time and fix these.

I removed omit block from the root pom and instead moved it into each module so that we can just ignore this spotbugs warning until we fix them in each module.

@DomGarguilo DomGarguilo added this to the 4.0.0 milestone Aug 7, 2025
@DomGarguilo DomGarguilo self-assigned this Aug 7, 2025
Copy link
Contributor

@ddanielr ddanielr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this check out from the top-level pom to the modules makes sense.

Do we need to make release notes changes for finalizing any of the classes that are defined in our public API?

@DomGarguilo
Copy link
Member Author

Do we need to make release notes changes for finalizing any of the classes that are defined in our public API?

That is a good question. I am not sure.

@DomGarguilo DomGarguilo requested a review from ctubbsii August 8, 2025 16:28
@DomGarguilo
Copy link
Member Author

I think feedback regarding the method of fixing this warning would be helpful (making the class final vs. adding the finalize() method). I tried to only make things final if it was an inner class and/or something that seemed very unlikely to be subclassed, however I just used my best judgement so would like others input on this part.

@ddanielr
Copy link
Contributor

ddanielr commented Aug 8, 2025

Do we need to make release notes changes for finalizing any of the classes that are defined in our public API?

That is a good question. I am not sure.

I think feedback regarding the method of fixing this warning would be helpful (making the class final vs. adding the finalize() method). I tried to only make things final if it was an inner class and/or something that seemed very unlikely to be subclassed, however I just used my best judgement so would like others input on this part.

I don't see an issue with finalizing the default implementation classes since users should be implementing new classes from the interface rather than extending the clientImpl classes.

Since finalize() is deprecated, does this SpotBugs warning go away if we were to switch to a newer java version?

@dlmarion
Copy link
Contributor

dlmarion commented Aug 8, 2025

I think feedback regarding the method of fixing this warning would be helpful (making the class final vs. adding the finalize() method).

I have no issue with making classes final where we can and where it makes sense. I'm not sure adding a finalizer to fix the others is a good long-term approach though it's likely the easiest fix. Finalizers themselves are deprecated and will be removed in a future Java release. I'm wondering if fixing the objects so that they don't throw exceptions from the constructor is the better choice, for example, using an init method on the objects that needs to be called post-construction but before use. There is now a jvm option, --finalization=disabled, in JDK 18 that disables all Finalizers as a step toward removal.

Copy link
Member

@ctubbsii ctubbsii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are pretty broad suppressions. I think it's nice that you're trying to narrow them down and triage them one at a time, but I'm wondering if this should be considered a WIP, or is this the intended final PR?

core/pom.xml Outdated
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<configuration>
<omitVisitors>SharedVariableAtomicityDetector</omitVisitors>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have other spotbugs filters in src/main/spotbugs/exclude-filter.xml, where we have fine-grained control over things to filter, and instead of omitting entire detectors, we can filter based on the documented bug names. I'm not sure which is better, but it would probably be nice to consolidate the config, so we're doing it all in one place, instead of doing some here in the pom, and others elsewhere in the exclude-filter.xml file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would argue that each serves a different purpose. To me it seems the filters in exclude-filter.xml are more or less permanent for code that we don't expect to change where as this new <omitVisitors> approach is a nice way to temporarily omit these detectors while we are fixing them. If we find that we want to permanently omit these detectors than I think it would be a good idea to move things into the xml file but for now this seems to work well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only temporary while it's in the PR. Once it gets merged, it's more or less permanent, or at least as permanent as the exclude-filter.xml files. My review comments are based on the desired end result after this is merged. That is to say, doing it in a different way while you're working on it is fine... but unless there's a good reason to have these configured separately from what's in the exclude-filter.xml files, then it's better that the end result by the time this is merged, has removed these extra configs, or consolidated them with the exclude-filter.xml files, so we don't have config scattered everywhere that basically does similar things.

Comment on lines 575 to 580

@Override
@SuppressWarnings("deprecation")
public final void finalize() {
// Prevent finalizer attacks (SpotBugs CT_CONSTRUCTOR_THROW)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really rather not add these methods in so many places. If this isn't possible to address another way, I think we can just suppress it. Adding this deprecated method is probably the least good workaround.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I agree. Adding the finalize method definitely seems like the worst fix.

I think the best way to handle things is

  1. if its easy and safe to remove the exception thrown from the constructor, do that (usually not possible)
  2. try to make the class final if its obvious that it can be made final
  3. add a @SuppressFBWarnings tag to the place in the code that the warning is coming from

Do you think that sounds good or would do you think its best to suppress this at module/project level?

Copy link
Contributor

@keith-turner keith-turner Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoiding finalize here will be helpful in the future https://openjdk.org/jeps/421

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think that sounds good or would do you think its best to suppress this at module/project level?

I think trying those 3 fixes, in that order, are probably best. There may be good reasons to suppress at the module/project level, but it should be a very good reason. We have relatively few module/project-wide exceptions, and they are easily articulable, like the comments in the existing spotbugs/exclude-filter.xml files.

<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<configuration>
<omitVisitors>SharedVariableAtomicityDetector,ConstructorThrow</omitVisitors>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to previous comments about consolidating spotbugs config rules, this config here makes me wonder what rules are associated with "SharedVariableAtomicityDetector" and "ConstructorThrow", and why we're suppressing them here. It seems that suppression here is just because they are noisy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure i understand your question about why we're suppressing them here. Do you mean within that module specifically or within the poms in general?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, having the config here raises the question "why are we skipping these checks?" without bothering to provide an answer. In the other places, such as the annotation, or the XML file, we generally offer an explanation, but there isn't one here. It's not clear what checks are being skipped, and why, when these visitors are being omitted.

@DomGarguilo DomGarguilo changed the title Fix ConstructorThrow SpotBugs warnings in core module WIP - Fix ConstructorThrow SpotBugs warnings in core module Aug 13, 2025
@DomGarguilo DomGarguilo changed the title WIP - Fix ConstructorThrow SpotBugs warnings in core module Fix ConstructorThrow SpotBugs warnings in core module Aug 14, 2025
@DomGarguilo
Copy link
Member Author

This PR is no longer a WIP.

To fix these spotbugs warnings I either made the class final (best solution, not possible everywhere) or I added a @SuppressFBWarnings tag to the class.

Copy link
Contributor

@keith-turner keith-turner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the java changes look good, could not really review the maven changes very well though.

Comment on lines 70 to 76
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<configuration>
<omitVisitors>SharedVariableAtomicityDetector,ConstructorThrow</omitVisitors>
</configuration>
</plugin>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could these changes be made in the accumulo parent pom instead of in each pom?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They could but the point of doing it in each pom is to allow us to make these changes incrementally so we don't have to fix the whole codebase at once and instead can go module by module.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be much simpler to just set <spotbugs.omitVisitors /> in the property section of the various POMs rather than add the whole plugin config to the pom.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this change in 941c2a3 @ctubbsii. Definitely a better approach than what I had before.

@DomGarguilo DomGarguilo requested a review from ctubbsii August 29, 2025 16:14
* file://path/one/jar1.jar,file://path/two/jar2.jar
*/
public class URLContextClassLoaderFactory implements ContextClassLoaderFactory {
public final class URLContextClassLoaderFactory implements ContextClassLoaderFactory {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class actually seems like it might be useful to extend. The check in the constructor probably doesn't need to be there, and is overly restrictive. It's only there as a sanity check to make sure we didn't screw up elsewhere in Accumulo... but having it here isn't exactly the right place for that check, and is overly restrictive.

Comment on lines +33 to +36
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

@SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW",
justification = "Constructor validation is required for proper initialization")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we may be reluctant to make final public API classes that the user might have extended, stuff in *Impl are fair game. I'm not sure if it's possible to make them all final (we might extend them ourselves, elsewhere internally), but if it is possible, there shouldn't be any harm doing so.

<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<plugins />

Comment on lines +71 to +73
<build>
<plugins />
</build>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<build>
<plugins />
</build>

</plugin>
</plugins>
</pluginManagement>
<plugins />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<plugins />

Comment on lines +107 to +109
<build>
<plugins />
</build>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<build>
<plugins />
</build>

Comment on lines +132 to +134
<build>
<plugins />
</build>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<build>
<plugins />
</build>

</plugin>
</plugins>
</pluginManagement>
<plugins />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<plugins />

Comment on lines +118 to +120
<build>
<plugins />
</build>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<build>
<plugins />
</build>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants