>> However, be aware that you can only do this on classes that you can
>> actually load in a JVM, meaning they are in the classpath or you have a
[quoted text clipped - 3 lines]
> If you are looking at compiled classes, then it'd be better, for several
> reasons, to use a byte-code library such as BCEL or ASM for the scanning.
Hmm... Looks like a good opportunity for a bit of...
<(free) product placement>
Absolutely. He could use Jen (http://jen.dev.java.net) which wraps around
ASM, making this as easy as:
SoftClass sc = new SoftClass(new ClassReader(classFileBytes));
SoftMethod sm = sc.getSoftMethod("void main(String[])");
if (sm != null && (sm.getModifiers() & SoftClass.ACC_STATIC &
SoftClass.ACC_PUBLIC) != 0) {
/* do something, this class has a main method */
}
(for example).
> Reasons:
>
> +) Probably faster (I haven't checked but it /ought/ to be).
Appreciably so, since there's no need to load, resolve, and define the
class.
> +) Safer (no risk of class initialisation code running).
Most definitely. Any exception (or Error) in a class initializer could
scupper the search, and theres no telling how long they might take, or
what else they might do...
> +) More flexible (as you say, no need for the .class file to be on the
> classpath).
>
> Downside:
>
> -) You have to learn (a bit about) BCEL or ASM[*].
;) See above.
> -) ?
>
[quoted text clipped - 8 lines]
> straightforward for the occasional user than ASM's Visitor pattern based
> API)
Well, 'more straightforward', perhaps, perhaps not, but either way, Jen
redresses the balance. ASM's performance is blindingly fast compared to
BCEL (around about 5 times as fast IIRC), and Jen takes full advantage of
that, too (hardly impacting performance at all), so it's the best of both
worlds.
</product placement>

Signature
Ross Bamford - rosco@roscopeco.remove.co.uk
Chris Uppal - 03 Nov 2005 17:39 GMT
> <(free) product placement>
[...]
<grin>
My apologies, I had forgotten all about Jen (odd since I read about it only a
couple of weeks ago). In mitigation I offer the excuse that I hadn't realised
that Jen was supposed to ease reading classfiles as well as writing them.
-- chris
Ross Bamford - 03 Nov 2005 18:34 GMT
>> <(free) product placement>
> [...]
[quoted text clipped - 8 lines]
>
> -- chris
:) No probs. Writing is probably what most people would use it for, but
it's actually small and fast enough to be useful for pretty much any kind
of processing of .class files.

Signature
Ross Bamford - rosco@roscopeco.remove.co.uk