way to extract the method names from all java files in a set of directories?

J

Jeff Kish

Hi.

Is there a way to extract all the method names from a large web project? I am
interested in all the java files and their classes.

Thanks
Jeff Kish
 
P

Paul Lutus

Jeff said:
Hi.

Is there a way to extract all the method names from a large web project? I
am interested in all the java files and their classes.

You can extract the method names from a given class using reflection, but
you may have to create a list of all the classes in your project in
advance.
 
W

Will Hartung

Jeff Kish said:
Hi.

Is there a way to extract all the method names from a large web project? I am
interested in all the java files and their classes.

I'm sure if you're enterprising, you may be able to get what you need by
hacking out a sed/awk/perl script to find what you need.

Another way is you can simply generate a JavaDoc from your source tree, and
then perhaps hack up the generate HTML files.

You can take a list of all the classes in the directory, and then write a
Java utility that uses reflection to dump the methods from the compiled
classes. You'd need to be conscious of any internal classes as well.

There are a couple of cross referencing tools on the web that may do what
you need, but I've never been really happy with any of them myself.

If it's a one time thing, I'd sed/awk/perl the source or the generated HTML
from JavaDoc, but that's me.

Regards,

Will Hartung
([email protected])
 
S

Sudsy

Jeff said:
Hi.

Is there a way to extract all the method names from a large web project? I am
interested in all the java files and their classes.

Thanks
Jeff Kish

If the classes have been wrapped in a jar then you can extract the
entries and run javap on them. A shell script follows:


#!/bin/sh
#
# barebones shell script to display all methods in all classes
# contained in a jar file
#
pgmName=`basename $0`
if [ $# -ne 1 ]
then
echo "Usage: $pgmName jarfile"
exit 1
fi
jarFile=$1
if [ ! -f $jarFile ]
then
echo "$pgmName: $jarFile does not exist"
exit 1
fi
jar tf $jarFile | while read entryName junk
do
if [ -z $entryName ]
then
continue
fi
lastChar=`echo $entryName | \
awk '{ print substr( $0, length( $0 ) ) }'`
if [ x$lastChar = x/ ]
then
continue
fi
className=`basename $entryName .class`
if [ x$className = x`basename $entryName` ]
then
continue
fi
className=`echo $entryName | \
awk '{ print substr( $0, 0, length( $0 ) - 6 ) }' | \
tr "/" "."`
javap -classpath $jarFile $className
done
 
J

Jeff Kish

I'm sure if you're enterprising, you may be able to get what you need by
hacking out a sed/awk/perl script to find what you need.

Another way is you can simply generate a JavaDoc from your source tree, and
then perhaps hack up the generate HTML files.

You can take a list of all the classes in the directory, and then write a
Java utility that uses reflection to dump the methods from the compiled
classes. You'd need to be conscious of any internal classes as well.

There are a couple of cross referencing tools on the web that may do what
you need, but I've never been really happy with any of them myself.

If it's a one time thing, I'd sed/awk/perl the source or the generated HTML
from JavaDoc, but that's me.

Regards,

Will Hartung
([email protected])
Thanks for the reply.
If it turns out to be as useful as I'm hoping it will be, then I'll probably
need to repeat it.
I'm not much of a set/awk/perl kind of person, but I can c++/java/sql fairly
well (but not great by anymeans..)

Jeff Kish
 
W

Will Hartung

Jeff Kish said:
Thanks for the reply.
If it turns out to be as useful as I'm hoping it will be, then I'll probably
need to repeat it.
I'm not much of a set/awk/perl kind of person, but I can c++/java/sql fairly
well (but not great by anymeans..)

Hmm..that's too bad.

For typical "reasonable" java code awk may well give you your best bang for
the buck.

This is a gawk script.

/\<public\>.*\<class\>/ {
print;
}
/protected[^=]*\(|private[^=]*\(|public[^=]*\(/ {
print;
}

That will take a file and:
a) print any line with that has the word 'public' followed by 'class' in it.
b) print any line that has protected/private/public followed by a '(' but
without an intervening '=';

The first line should give you your class declaration. There are certainly
lines of legal java that this will match that are not class declarations.

The second should give at least the first part of the method names. Same
caveat as above. It should cleverly NOT show lines such as:
public Object o = new Object();

You will have issues with inner classes, or if you break your lines up away
from typical Java. It also doesn't find "package scoped" methods (i.e. those
without public/protected/private).

perl/awk/sed's regular expressions can be very powerful, but are not to be
left unattended. There's a great saying: "You have a problem that you think
regular expressions can solve. Now you have two problems." Java syntax can
not be easily represented by a regular expression, but they can fake it for
common, albeit limited, cases.

That may be enough to get you started. You may as well be able to do the
same thing use regexes or whatever with Java/C++/whatever. But, that would
be my first cut at it, depending on the goals. But, IMHO, 6 lines of awk is
pretty powerful.

Regards,

Will Hartung
([email protected])
 
S

Sudsy

Will Hartung wrote:
This is a gawk script.

/\<public\>.*\<class\>/ {
print;
}
/protected[^=]*\(|private[^=]*\(|public[^=]*\(/ {
print;
}

That will take a file and:
a) print any line with that has the word 'public' followed by 'class' in it.
b) print any line that has protected/private/public followed by a '(' but
without an intervening '=';
<snip>

Will,
Glad to see that you're skilled with awk (some people think it's
short for "awkward" ;-). It's a viable solution when you're dealing
with source code. I took a different approach (also different from
those suggesting the use of the output of javadoc) and assumed that
the code would have already been packaged for production/deployment.
While javap isn't perfect (what is?) it has served me well in a
number of situations. Heck, I sometimes want to look into a class
to see whether there are additional (undocumented) classes which
might address specific needs.
Introspection and reflection can also be used and my shell script
solution is not what I consider to be "highly efficient" but then
most of these scripts are meant to be q&d (quick and dirty) solutions
to an immediate problem. Did you notice that I used awk scripts in a
couple of places? It's an excellent tool for certain manipulations.
Wonder what the OP will make of all this...
 
W

Will Hartung

Sudsy said:
Will,
Glad to see that you're skilled with awk (some people think it's
short for "awkward" ;-). It's a viable solution when you're dealing
with source code. I took a different approach (also different from
those suggesting the use of the output of javadoc) and assumed that
the code would have already been packaged for production/deployment.

Oh, I'm an old salt awk hack. I don't write compilers and such in it, but
for folding, spindling, stapling, and mutilating bulk text files, it has its
uses...and I keep forgetting Perl, so...
While javap isn't perfect (what is?) it has served me well in a
number of situations. Heck, I sometimes want to look into a class
to see whether there are additional (undocumented) classes which
might address specific needs.
Introspection and reflection can also be used and my shell script
solution is not what I consider to be "highly efficient" but then
most of these scripts are meant to be q&d (quick and dirty) solutions
to an immediate problem. Did you notice that I used awk scripts in a
couple of places? It's an excellent tool for certain manipulations.
Wonder what the OP will make of all this...

I had forgotten about javap, that's a good idea. If nothing else, it's a
good starting filter for the awk script :).

#!/bin/bash
CLASSPATH=allmycompiledclasses.jar; export CLASSPATH
mkdir /tmp/results
jar tvf allmycompiledclasses.jar | grep "\.class" | awk '{print $8}' | \
sed -e 's/\//.' -e 's/\.class//' > /tmp/classes.out
for i in `cat /tmp/classes.out`
do
echo $i;
javap $i | awk -f /tmp/willsscript.awk > /tmp/results/$i
done

voila! A file named for each class (like com.example.pkg.Class) with the
class and method signatures...

It all depends on the ultimate goal regarding how robust and complete the
solution is, of course. While I suggested reflection, I shied away from it
simply because it's a bunch of code to write. But, on the other hand, it's
certainly more robust and reusable than 6 lines of awk :). (Of course, the
above script might be considered a lot of code to write, but I write that
kind of code on the fly in my sleep every day, so in my case, it's not.)

Regards,

Will Hartung
([email protected])
 
B

Bas

Jeff Kish said:
Is there a way to extract all the method names from a large web project? I am
interested in all the java files and their classes.

Someone mentioned BeautyJ (http://beautyj.berlios.de/). This tool generates xml from your java code that contains all the
info that you need.

The resulting XML is probably easier to parse than javadoc-generated
html.
 
J

Jeff Kish

Jeff said:
Hi.

Is there a way to extract all the method names from a large web project? I am
interested in all the java files and their classes.

Thanks
Jeff Kish

If the classes have been wrapped in a jar then you can extract the
entries and run javap on them. A shell script follows:


#!/bin/sh
#
# barebones shell script to display all methods in all classes
# contained in a jar file
#
pgmName=`basename $0`
if [ $# -ne 1 ]
then
echo "Usage: $pgmName jarfile"
exit 1
fi
jarFile=$1
if [ ! -f $jarFile ]
then
echo "$pgmName: $jarFile does not exist"
exit 1
fi
jar tf $jarFile | while read entryName junk
do
if [ -z $entryName ]
then
continue
fi
lastChar=`echo $entryName | \
awk '{ print substr( $0, length( $0 ) ) }'`
if [ x$lastChar = x/ ]
then
continue
fi
className=`basename $entryName .class`
if [ x$className = x`basename $entryName` ]
then
continue
fi
className=`echo $entryName | \
awk '{ print substr( $0, 0, length( $0 ) - 6 ) }' | \
tr "/" "."`
javap -classpath $jarFile $className
done
Thanks.
I am trying to figure out how to translate this, using my limited skills to my
windows platform.

Is this in Unix from, maybe a kornshell or c-shell or something?
I don't think I have command line access to awk etc right now, but I'm still
studying what you did.

... oh. some jar, some not jar here.
Thanks Again
Jeff Kish
 
S

Sudsy

Jeff Kish wrote:
Thanks.
I am trying to figure out how to translate this, using my limited skills to my
windows platform.

Is this in Unix from, maybe a kornshell or c-shell or something?

Bourne shell.
I don't think I have command line access to awk etc right now, but I'm still
studying what you did.

Don't waste your time then. Use the suggestions of others to achieve the
goal programmatically.
.. oh. some jar, some not jar here.
Thanks Again
Jeff Kish

Give me a few minutes to cobble together some sample code...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top