How do I keep a class available to unload while keeping a MethodHandles.Lookup of it?

I have a library where a class can consent to full access of private members by registering its MethodHandles.lookup() in its static initializer. Something like this:
class Foo extends Thing{
static{ accessConsent(MethodHandles.lookup()); }

private int thingOnlyField; // Thing can now access this

...
}

class Thing{
private static final Map<Class, MethodHandles.Lookup> PRIVATE_LOOKUPS = ...
protected static void accessConsent(MethodHandles.Lookup lookup){
PRIVATE_LOOKUPS.put(lookup.lookupClass, lookup);
}
}
class Foo extends Thing{
static{ accessConsent(MethodHandles.lookup()); }

private int thingOnlyField; // Thing can now access this

...
}

class Thing{
private static final Map<Class, MethodHandles.Lookup> PRIVATE_LOOKUPS = ...
protected static void accessConsent(MethodHandles.Lookup lookup){
PRIVATE_LOOKUPS.put(lookup.lookupClass, lookup);
}
}
The private lookup gets stored inside a Map<Class, MethodHandles.Lookup>. I want to be able to give the class the freedom to unload. So when all references to the class loader and the class itself is gone. Issue is that Lookup contains a reference to the class so I would need a weakly referenced key and value map. (that is not the issue, already have that) If I do that though, the lookup itself will be randomly forgotten so that kind of defeats the whole purpose of providing it on load. Any ideas on how to best handle this?
1 Reply
JavaBot
JavaBot2w ago
This post has been reserved for your question.
Hey @LapisSea™! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server