Do you know of a C++ library (open source, or free for non-commercial use) that can parse Java source codes, for example from a jar file or defined classpath? I want to extract classes, class members, methods, method calls and relations between these artifacts.
I've spent all day googling for a solution. Either I'm blind, or can't read! :)
You can't get source codes from a jar file, since that is really a set of (binary) class files. Assuming you means the source codes that might have been used to produce a jar file, then there's a decent answer.
If you want an open source solution, you can try ANTLR, which has a Java 1.5 grammar and AFAIK will build AST. From that you can "extract" the trees for the items you want, or at least the line numbers for the subtree of interest; from there, you can extract the code you want.
I believe ANTLR can be configured to produce a C++-based parser.
To capture relations between these, you need full name and type resolution, so you know which definition an identifier actually references. For this, ANTLR being just a parser won't do the trick; you need to live a Life After Parsing.
An alternative might be the Java compiler; it offers some kind of API.
There are a number of decompilers available for Java. These aren't based on C++ necessarily, but they can convert Java classes and libraries back into source.
Examples: JD Core, DJ Java Decompiler. more
I need to extract from c/c++ source code files, function/class/macro names and their locations in their files. I need to do this in java and over a lot of files (~100/150). How can I do this?
So basically I need something similar to ctags but in java.
The easiest thing to do would probably be to write a Java Native Interface wrapper for ctags!
You could also look at finding a C++ parser in Java. Maybe abduct the parser Eclipse uses for syntax highlighting. Writing your own parser will be extremely painful since it's not a LALR grammar (I know this from experience).
Working on a code generation tool to help creating boiler code for our project.
The generator is written in ruby with erb templates, the project itself is in Java.
Now I am looking for a ruby gem/library for parsing java source files, given a string from a .java files, get the imports, methods, fields, class name etc etc, that would enable me to navigate to a certain method and appending code to it etc (kinda like jQuery selector).
I am wondering if there are already solutions that I can use, kinda like the javaclass-rb library, but that is for parsing bytecodes from .class files.
I know I could use ANTLR and a ruby adapter, but I hope there are existing solutions.
Thanks!
JRuby is a Ruby implementation on top of the JVM that make interaction between Ruby and Java objects trivial. If you decide to use this, you can use any Java library to solve the task, like javaparser.
The project I'm doing is written in Java and parsers source code files. (Java src up to now). Now I'd like to enable parsing Ruby code as well.
Therefore I am looking for a parser in Java that parses Ruby source code.
The only thing I have been able to find up to now are Ruby parsers in Ruby (ParseTree and RubyParser...).
I could maybe parse the source files I want with a Ruby parser in JRuby, and then access my Java stuff from there.
But that would mean that I will not be able to reuse a lot of my previously written Java code.
Is there a decent Ruby parser in Java out there and have I just not be able to find it? Or does someone see a better solution?
See http://kenai.com/projects/jruby-parser/
You could let ANTLR generate a lexer/parser for you. They have a simplified Ruby grammar available: http://www.antlr.org/grammar/1160820213459/rubyParserLexerSPGrammar.g which may be sufficient for your needs.
As you know there is a Syntax highlighter for PHP called GeSHi which supports a great number of Programming Languages or Code formats.
However, I couldn't find such a library for Java which supports programming languages that I need (ADA, ASP, BNF, Bash, Brainfuck, C, C++, C#, CSS, Cobol, ColdFusion, D, Fortran, Haskell, HTML, INI (Config), Java, JavaScript, Lisp, Make, Objective C, PASCAL, Perl, PHP, PLSQL, Prolog, Python, Ruby, Scheme, SQL, VB.NET, Verilog, VHDL, Visual Basic, XML.)
Do you know one or should I prefer inefficient way which is retrieving the highlighted code from a remote PHP server via http transaction? Any ideas?
Thanks.
Two related questions:
What code highlighting libs are there for Java?
Where can I find a syntax highlighting library for Java?
And one library I found: http://colorer.sourceforge.net/
Have a look at JHighlighter or jEdit Syntax Package. All mentioned languages aren't supported out of the box. However, you have the sources, so I guess it should be possible to add language support.
Not a direct answer but, if client-side syntax highlighting is an option, the SyntaxHighlighter library from Alex Gorbatchev is an awesome javascript library, supports lots of languages and is highly extensible.
You could use Pygments through Jython. Won't be as fast as a Java solution, but much faster than interacting with a remote server.
Barring that, you could run Geshi locally and pipe source code through it, that would also beat an HTTP round trip.
It seems that it is possible to run GeSHi from Java: GeSHi4J it seems to be a wrapper that run the PHP library on the JVM.
There is a port of prettify.js for Java: java-prettify.
It can be used to produce HTML (computed in Java), as I discussed here:
Use the java-prettify parser to create HTML
jedit is a text editor with syntax highlighting support for some 170+ languages via "modes". It also allows you to specify your own syntaxes. You can use the StandaloneTextArea component in your own application as follows:
Extract source (eg: jedit4.3source.tar.bz2 to d:\source\jedit)
Use ant to copy all the textarea files to ..\textarea eg:
D:\Source\jedit\jEdit> ant prepare-textArea
However, it misses the file BufferUndoListener.java. Copy this manually by executing
D:\Source\jedit\jEdit> copy org\gjt\sp\jedit\buffer\BufferUndoListener.java ..\textarea\src\org\gjt\sp\jedit\buffer\
In Eclipse create a Java Project from existing source in the directory D:\Source\jedit\textarea
Navigate to org.gjt.sp.jedit.textarea.StandaloneTextArea.java
Change the line
mode.setProperty("file","modes/xml.xml");
to
mode.setProperty("file","src/modes/xml.xml");
Run. Copy and paste an XML into the editor and see the syntax highlighting is working.