I want to be able to get various non-trivial info from classes which have come from some external source. Typical scenario is that someone supplies you with a load of new classes, and you want to check versioning information etc before you even allow the rest of your system to see them.
The easy way is to use OOP and instantiate each and every class into an instance of some interface that has appropriate query methods (NB: I can (and in fact AFAICS have to, due to the vagaries of Classloaders!) mandate compliance with whatever interface I desire).
But I want to get this info without instantiating them…
I can also insist that they extend a base class that already exists (and is partially abstract).
How can I interact with the classes just after they’ve been loaded, but before they’re instantiated? (NB: one of the things I’m aiming for is to be able to do certain checks and logging within the Classloader. Because loading a single class can pull in multiple others all in a single method call, I may need the info from Class A before it is physically possible to instantiate A (because I need to know before I allow the loading of Class B, referenced by A)).
It would be beautiful if there were a static inheritance tree where methods were inherited but not variables. That way, you could write a method body, e.g. “getVersion()”, that used the current class’s static var “version”, but the method implementation itself was inherited to subclasses. They would just need to mask the superclass’s “version” var with their own, and when getVersion() was invoked on them, it would use their value.
AFAIAA, this is impossible in Java? It wouldn’t surprise me, given it’s a bit of a weird feature :).
AFAICS, the best way of achieving my aims within the bounds of the JLS
is to just use reflection and have a “known list” of methods that imported classes have to implement as static’s.
It’ll work fine, and the behaviour when loading a class that doesn’t have the desired method(s) will be a proprietary “ClassIncompatible” Exception that indicates to the rest of our sytem to message the admin / logfiles / etc that Class XXX is missing a required method. That’s a cop-out, but OK because it bails out before any system or game state is changed, and an admin trying to introduce that class will get notified pretty much immediately that they’ve screwed up :).
).