parsing java files

  • Thread starter =?ISO-8859-1?Q?st=E9phane_bard?=
  • Start date
?

=?ISO-8859-1?Q?st=E9phane_bard?=

hello
i would like to parse java files an detect
class name's, attributes name's type's and visibility (and or list of
methods).

is there any module who can parse easily a java file without using
(jython)?
 
P

Paul Boddie

stéphane bard said:
hello
i would like to parse java files an detect
class name's, attributes name's type's and visibility (and or list of
methods).

is there any module who can parse easily a java file without using
(jython)?

There are probably a number of standard parser solutions which can
expose such information. However, another solution is that provided by
the javaclass package:

import javaclass.classfile # http://www.python.org/pypi/javaclass

# Read the contents of the compiled class.

f = open("org/w3c/dom/Element.class", "rb")
s = f.read()
f.close()

# Process the class data.

c = javaclass.classfile.ClassFile(s)

# Obtain the internal name of the class.

name = unicode(c.interfaces[0].get_name()) # u'org/w3c/dom/Node'
for m in c.methods:

# Obtain each method name and access details.

method_name = unicode(m.get_name()) # eg. u'getTagName'
is_public = m.access_flags & javaclass.classfile.PUBLIC

Obviously, this is more convoluted than it needs to be, but the
original purpose of the module employed is to assist in translating
Java class data to other forms - notably Python bytecode - although
such experiments haven't been continued by myself for quite some time.
The class decoding part illustrated above should be quite usable,
however.

Paul
 
G

Gabriel Genellina

At said:
i would like to parse java files an detect
class name's, attributes name's type's and visibility (and or list of
methods).

is there any module who can parse easily a java file without using
(jython)?

I would get the needed info using javadoc (in
Java) and then process the output in Python.



Gabriel Genellina
Softlab SRL





__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
?

=?ISO-8859-1?Q?st=E9phane_bard?=

i use javaclass and it's great !!
this is what i need. thank's paul

gabriel thank's too for advice


Paul Boddie a écrit :
stéphane bard said:
hello
i would like to parse java files an detect
class name's, attributes name's type's and visibility (and or list of
methods).

is there any module who can parse easily a java file without using
(jython)?

There are probably a number of standard parser solutions which can
expose such information. However, another solution is that provided by
the javaclass package:

import javaclass.classfile # http://www.python.org/pypi/javaclass

# Read the contents of the compiled class.

f = open("org/w3c/dom/Element.class", "rb")
s = f.read()
f.close()

# Process the class data.

c = javaclass.classfile.ClassFile(s)

# Obtain the internal name of the class.

name = unicode(c.interfaces[0].get_name()) # u'org/w3c/dom/Node'
for m in c.methods:

# Obtain each method name and access details.

method_name = unicode(m.get_name()) # eg. u'getTagName'
is_public = m.access_flags & javaclass.classfile.PUBLIC

Obviously, this is more convoluted than it needs to be, but the
original purpose of the module employed is to assist in translating
Java class data to other forms - notably Python bytecode - although
such experiments haven't been continued by myself for quite some time.
The class decoding part illustrated above should be quite usable,
however.

Paul
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top