Java parser in Bnd ?

5 views
Skip to first unread message

Clément Delgrange

unread,
May 1, 2019, 7:42:44 AM5/1/19
to bndtools-dev
Hi,

Is there a Java source code parser in Bnd? I look at how annotations are processed for metatypes, it seems to use the Clazz class, but I think I need something  simpler (just to have an object model of a source file).

Thanks,
Clément.

BJ Hargrave

unread,
May 1, 2019, 7:49:41 AM5/1/19
to bndtoo...@googlegroups.com
Bnd does not have a Java source parser. 

--
You received this message because you are subscribed to the Google Groups "bndtools-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bndtools-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

BJ

Clément Delgrange

unread,
May 1, 2019, 8:46:55 AM5/1/19
to bndtools-dev
Ok thanks


Le mercredi 1 mai 2019 13:49:41 UTC+2, BJ Hargrave a écrit :
Bnd does not have a Java source parser. 
On Wed, May 1, 2019 at 07:42 Clément Delgrange <cl.de...@gmail.com> wrote:
Hi,

Is there a Java source code parser in Bnd? I look at how annotations are processed for metatypes, it seems to use the Clazz class, but I think I need something  simpler (just to have an object model of a source file).

Thanks,
Clément.

--
You received this message because you are subscribed to the Google Groups "bndtools-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bndtoo...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

BJ

BJ Hargrave

unread,
May 1, 2019, 8:52:43 AM5/1/19
to bndtoo...@googlegroups.com
I am not sure that parsing Java source is simpler than parsing Java
class files :-)

But if you need to process source, you may want to consider writing an
annotation processor for the java compiler which will provide you
access to the AST (I think). This is what Micronaut
(https://micronaut.io/) does.
> To unsubscribe from this group and stop receiving emails from it, send an email to bndtools-dev...@googlegroups.com.

Fr Jeremy Krieg (Home)

unread,
May 1, 2019, 8:02:02 PM5/1/19
to bndtoo...@googlegroups.com
BJ is correct. Here is a tutorial on writing an annotation processor:


Typically, you write an annotation processor that processes elements tagged with a certain annotation. The annotation processor's config will specify which annotations it is interested in, and the compiler framework will invoke your processor whenever it comes across an annotation that matches.

If you want to access all of the source, from memory you can specify a wildcard in your annotation processor's config so that it will be invoked for any annotated element. You can also get access to any AST element by walking the hierarchy. It might be a little bit clunky if your purpose is not directly to handle annotated elements, but rather more general source-code parsing - however, I think you should still be able to achieve what you want.

Another alternative is to use Eclipse's AST parsing library, which might be useful in an Eclipse context (if you're not in an Eclipse context, it may pull in a few too many extra dependencies for your liking). The main entry point is org.eclipse.jdt.core.dom.ASTParser. There is an example of its use in one of the Bnd tests: https://github.com/bndtools/bnd/blob/master/bndtools.core/test/org/bndtools/core/editors/ImportPackageQuickFixProcessorTest.java (see the setupAST() method). From memory, the Eclipse AST is read-write too, which might be useful for you if you want to programmaticly edit the source (Eclipse uses this library internally for all of its code quick-fixes).

Hope this helps.

Blessings,
Fr Jeremy Krieg

Clément Delgrange

unread,
May 11, 2019, 4:54:48 PM5/11/19
to bndtools-dev
Hi Jeremy,

Thanks for the link, finally annotation processing did not fit my use case I think, I used the JavaParser instead of the Eclipse's AST parsing library.


Le jeudi 2 mai 2019 02:02:02 UTC+2, Fr Jeremy Krieg (Home) a écrit :
BJ is correct. Here is a tutorial on writing an annotation processor:


Typically, you write an annotation processor that processes elements tagged with a certain annotation. The annotation processor's config will specify which annotations it is interested in, and the compiler framework will invoke your processor whenever it comes across an annotation that matches.

If you want to access all of the source, from memory you can specify a wildcard in your annotation processor's config so that it will be invoked for any annotated element. You can also get access to any AST element by walking the hierarchy. It might be a little bit clunky if your purpose is not directly to handle annotated elements, but rather more general source-code parsing - however, I think you should still be able to achieve what you want.

Another alternative is to use Eclipse's AST parsing library, which might be useful in an Eclipse context (if you're not in an Eclipse context, it may pull in a few too many extra dependencies for your liking). The main entry point is org.eclipse.jdt.core.dom.ASTParser. There is an example of its use in one of the Bnd tests: https://github.com/bndtools/bnd/blob/master/bndtools.core/test/org/bndtools/core/editors/ImportPackageQuickFixProcessorTest.java (see the setupAST() method). From memory, the Eclipse AST is read-write too, which might be useful for you if you want to programmaticly edit the source (Eclipse uses this library internally for all of its code quick-fixes).

Hope this helps.

Blessings,
Fr Jeremy Krieg


On Wed, May 1, 2019 at 10:22 PM BJ Hargrave <b...@bjhargrave.com> wrote:
I am not sure that parsing Java source is simpler than parsing Java
class files :-)

But if you need to process source, you may want to consider writing an
annotation processor for the java compiler which will provide you
access to the AST (I think). This is what Micronaut
(https://micronaut.io/) does.

Reply all
Reply to author
Forward
0 new messages