Java source code parser in Ruby - java

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.

Related

Templates compiled to javascript in a Java environment

Coming from Node.js and now working with Java I'm wondering about how to achieve with Java what I did with Node.js, more specifically: How to compile my templates into JS functions.
What I did before was using EJS templates, then they were compiled into a single JS file that exposed functions to call from JS with parameters, these functions returned a HTML string that I could use as it to update my view.
The great thing about this is that I could write my templates in separated files, (EJS) then dynamically Grunt/EJS (I guess, that's from Sails.js internal logic) was converting the template into a function, merged all of them and finally generating a single file usable in my app, I just had to call a function, provide arguments and that's it: I get a view dynamically generated.
I want to achieve the same in a Java environment, using Ant, maybe maven, but I don't know how to do it neither where to look for since I'm a Java novice.
Edit:
I'm talking about client-side templating, I just want to avoid the -ugly-traditionnal way to write HTML code inside JS strings and split them in separated files so I can maintain them easier. I only need to use them from JS, not from Java, but I need to "compile" them from a Java environment, using Ant.
This is somewhat similar to this post, and this one, so you may have a look there to see if there are other useful answers.
A consensus seems to be that Mustache, which has a Java-implemented compiler (among other flavors) could be executed as an Ant target. It's a logic-less template, so you may want to find another solution.
Because it is possible to call Node.js scripts from Ant, it seems like an artificial restriction to not have Node.js. You will have much more choice and flexibility in template choice if you can persuade your team to allow Node.js.
Node.js is not in an either-or relationship with Java tools. There is plenty of room for both on a project, and I've worked on C# projects that use Node.js, just as I've worked on projects that use JRuby, Java, and Rails. None of those technologies excludes the others.
Note that I'm not recommending that you try to persuade people to switch to Node.js as an environment, migrate existing code, or use Grunt, but if it's a useful tool that you're familiar with, I can't think of a single good reason why you should be denied its use.
Another solution is to use this library I discovered yesterday. I've tried it and it seems to work well, some features are also quite useful for development mode, like the watcher on the templates.
http://jcruncher.org/
I think I'll go with it, just wondering about the author and his implication, and hope to see a Handlebars 3.0.0 compatible version soon, as well as the source code on GitHub.
P.S: You can find the handlebars compiler on the CDN (select the 2.0.0 version):
http://cdnjs.com/libraries/handlebars.js/
https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.runtime.min.js

Extract function names from c/c++ source code in java

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).

Syntax Preprocessors for Java

I'm looking for a Java macro language that provides for convenient ways of doing closures (that compile to anonymous inner classes) and list comprehension (that compiles down to basic java loops).
An example of the kind of thing I'm looking for would be Xtend2 http://www.eclipse.org/Xtext/#xtend2
But I want something for general purpose programming (Xtend2 is very specific DSL for Xtext and has a ton of dependencies). Maybe even something that would let me define multiple classes in a single file (which would then get split up into two separate files by the pre-processor).
Does anything like this exist?
Edited to add:
I'm doing Android development so any alternatives have to generate either valid Java source or the byte code has to be compatible with the dalvik recompiler.
Mmm, there used to be the JSE, which was tremendous fun, back in the day.
Mirah is cool, but not ready for primetime, IMO.
You can do a lot with smart templating, although your source view is the Java.
There's a post on SO about using XTend on Android from a few days ago, too.
Frege produces java source code.
I do not know whether dalvik would like it. (But I would be interested to hear ...)
And, of course, you have some runtime library code.
That being said, there are a number of other projects that do closures etc. in java, for example: lambdaj

Java Code Generation (Metaprogramming, Reflection, wtv)

Does anyone knows a tool for Java (something like codedom for C#) that provides a way to generate Java code to a .java file?
EDIT:
I'm building a platform the main objective of which is to automate an operation. Giving some input, I want to generate code for an external tool. So it isn't generation on runtime. I want to generate and output that to an actual file.
JET maybe outdated (I didn't use it) JET Tutorial Part 1
More Plugins for Eclipse Plugins in Code Generation
EDIT:
Sorry I don't know codedom and what features this tool implies.
Standalone is Freemarker
and Velocity see also this example
I have had some success using ASM to both modify existing classes at the bytecode level or to generate completely new classes on the fly. The tutorial walks you through this in a very understandable fashion.
ASM like most such tools generates bytecode not source. The reason for this is if you want to dynamically generate and execute new code from with a program, historically it was not straight forward to invoke the Java compiler. Therefore it was generally easier to generate and use bytecode than source.
If you need to generate and run the code immediately within your program I recommend you use bytecode manipulation tool. If all you need is Java source, I would roll my own code generator that takes my input format and generates the code. You may want to look for a framework to help you with this but since a source file is just text, usually it is just as easy to create this yourself especially if you have a custom input format.
ABSE and AtomWeaver form a code generation and model-driven-development framework where you can easily implement what you want. ABSE is a new methodology where you compose your code generator from smaller bits (called Atoms) and AtomWaver is an straightforward IDE that lets you implement, manipulate and use your generator models.
It also allows non-programmers to build programs/configurations/whatever, made from already-built parts (Atoms you have previously prepared).
This project is just being publicly launched now, and an alpha version is being made available now. ABSE is open, and AtomWeaver is free for personal and commercial use.
Get more info here : http://www.abse.info (Disclaimer: I am the project lead)
What you could try is to use an existing grammar (e.g. from ANTLR) and build the AST. Then from the AST generate the code. That should be much more robust than simple templating. For something in the middle I suggest the (eye-opening) talk from Terence Parr about StringTemplate. (Sorry, don't have the link for the talk at hand)
I am not sure what you really need, but take a look at javassist. Is it the thing you are looking for?

Ruby parser in Java

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.

Categories

Resources