Reputation: 1804
I want to make sure that my Objects (Class <? extends Object>
) are properly disposed of, because other instances may hold data that are used by them. Example, a DEK/KEK pair for secret data.
Since Overriding finalize()
is generally frowned upon, how do I make the IDE warn and force the developer to call dispose()
iff the Object reference is set to null, or would be automatically set to null (example exiting a method)?
Assuming source code is under my direct control, and the IDE is Netbeans.
Upvotes: 4
Views: 1894
Reputation: 1217
First of all, worth to mention that finalizers were deprecated in Java 9 in favour of the Cleaner. Probably this would be the better option then the one I will describe below. Didn't tried it yet though.
Speaking about the question itself: I'd try doing it that way:
Closeable
.void execute(Consumer<A> consumer)
.execute
: instantiate object of type A, pass it to consumer (accept
) then close it.Upvotes: 4