I'm looking for a way to automatically generate source code for new methods within an existing Java source code file, based on the fields defined within the class.
In essence, I'm looking to execute the following steps:
Read and parse SomeClass.java
Iterate through all fields defined in the source code
Add source code method someMethod()
Save SomeClass.java (Ideally, preserving the formatting of the existing code)
What tools and techniques are best suited to accomplish this?
EDIT
I don't want to generate code at runtime; I want to augment existing Java source code
What you want is a Program Transformation system.
Good ones have parsers for the language you care about, build ASTs representing the program for the parsed code, provide you with access to the AST for analaysis and modification, and can regenerate source text from the AST. Your remark about "scanning the fields" is just a kind of traversal of the AST representing the program. For each interesting analysis result you produce, you want to make a change to the AST, perhaps somewhere else, but nonetheless in the AST.
And after all the chagnes are made, you want to regenerate text with comments (as originally entered, or as you have constructed in your new code).
There are several tools that do this specifically for Java.
Jackpot provides a parser, builds ASTs, and lets you code Java procedures to do what you want with the trees. Upside: easy conceptually. Downside: you write a lot more Java code to climb around/hack at trees than you'd expect. Jackpot only works with Java.
Stratego and TXL parse your code, build ASTs, and let you write "surce-to-source" transformations (using the syntax of the target language, e.g., Java in this case) to express patterns and fixes. Additional good news: you can define any programming language you like, as the target language to be processed, and both of these have Java definitions.
But they are weak on analysis: often you need symbol tables, and data flow analysis, to really make analyses and changes you need. And they insist that everything is a rewrite rule, whether that helps you or not; this is a little like insisting you only need a hammer in toolbox; after all, everything can be treated like a nail, right?
Our DMS Software Reengineering Toolkit allows the definition of an abitrary target language (and has many predefined langauges including Java), includes all the source-to-source transformation capabilities of Stratego, TXL, the procedural capability of Jackpot,
and additionally provides symbol tables, control and data flow analysis information. The compiler guys taught us these things were necessary to build strong compilers (= "analysis + optimizations + refinement") and it is true of code generation systems too, for exactly the same reasons. Using this approach you can generate code and optimize it to the extent you have the knowledge to do so. One example, similar to your serialization ideas, is to generate fast XML readers and writers for specified XML DTDs; we've done that with DMS for Java and COBOL.
DMS has been used to read/modify/write many kinds of source files. A nice example that will make the ideas clear can be found in this technical paper, which shows how to modify code to insert instrumentation probes: Branch Coverage Made Easy.
A simpler, but more complete example of defining an arbitrary lanauges and transformations to apply to it can be found at How to transform Algebra using the same ideas.
Have a look at Java Emitter Templates. They allow you to create java source files by using a mark up language. It is similar to how you can use a scripting language to spit out HTML except you spit out compilable source code. The syntax for JET is very similar to JSP and so isn't too tricky to pick up. However this may be an overkill for what you're trying to accomplish. Here are some resources if you decide to go down that path:
http://www.eclipse.org/articles/Article-JET/jet_tutorial1.html
http://www.ibm.com/developerworks/library/os-ecemf2
http://www.vogella.de/articles/EclipseJET/article.html
Modifying the same java source file with auto-generated code is maintenance nightmare. Consider generating a new class that extends you current class and adds the desired method. Use reflection to read from user-defined class and create velocity templates for the auto-generating classes. Then for each user-defined class generate its extending class. Integrate the code generation phase in your build lifecycle.
Or you may use 'bytecode enhancement' techniques to enhance the classes without having to modify the source code.
Updates:
mixing auto-generated code always pose a risk of someone modifying it in future to just to tweak a small behavior. It's just the matter of next build, when this changes will be lost.
you will have to solely rely on the comments on top of auto-generated source to prevent developers from doing so.
version-controlling - Lets say you update the template of someMethod(), now all of your source file's version will be updated, even if the source updates is auto-generated. you will see redundant history.
You can use cglib to generate code at runtime.
Iterating through the fields and defining someMethod is a pretty vague problem statement, so it's hard to give you a very useful answer, but Eclipse's refactoring support provides some excellent tools. It'll give you constructors which initialize a selected set of the defined members, and it'll also define a toString method for you.
I don't know what other someMethod()'s you'd want to consider, but there's a start for you.
I'd be very wary of injecting generated code into files containing hand-written code. Hand-written code should be checked into revision control, but generated code should not be; the code generation should be done as part of the build process. You'd have to structure your build process so that for each file you make a temporary copy, inject the generated source code into it, and compile the result, without touching the original source file that the developers work on.
Antlr is really a great tool that can be used very easily for transforming Java source code to Java source code.
Related
This page describes how I can use the code generator in javac to generate code given that I can build an AST (using a separate parser which I wrote). The technique involves editing javac's source code to basically bypass the Java parser, so that one could supply his/her own AST to the code generator. This could work, but I was hoping to do it in a slightly cleaner way. I want to include the code generating part of javac as a library in my project so I can use it to generate code, without bringing with it the rest of javac's source.
Is there a way to do this with javac, or is there perhaps a better library?
Also, feel free to change the question's title. I couldn't think of a better one, but it's a little ambiguous. If you suggest an edit for a better title, I'll accept it.
I think what you might be interested in is a java library like BCEL(ByteCode Engineering Library)
I played around with it back when I took a class on compiler construction, basically, it has a nice wrapper for generating the constant pool, inserting named bytecode instructions into a method and whatnot, then when you are done, you can either load the class at runtime with a custom classloader, or write it out to a file in the normal way.
With BCEL, it should be relatively easy to go from the syntax tree to the java bytecodes, albeit a bit tedious, but you may want to just use BCEL to generate the raw bytecode without building the tree as well in some cases.
Another cool framework is ASM, a bytecode analysis and manipulation framework.
In case you do not want to use a framework, as of now (2014), it is not possible to generate bytecode from a tree using the arbitrary representations of com.sun.source.tree.* as said here.
I'm parsing Java source files and I need to extract type information to guess at signatures of called methods.
e.g. I have foo.x(bar) and I need to figure out the type of foo and bar.
I'm using a java parser that gives me a complete AST, but I'm running into problems with scoping. Is there a different parser I could use that resolves this?
This can't be resolved perfectly because of reflection, but I'm hoping a good parser can deal with scoping and casting issues in the least.
edit: I can't assume that other source files will be present, so I can't simply follow the method call to its source and read the signature from the method declaration
Java's Pluggable Annotation Processing Framework (and its associated APIs) are designed to model Java code in the way you're talking about. You can invoke the Java compiler at runtime, giving you access to the model of the source using the APIs in the javax.lang.model packages. An introductory article is available here.
Basically getting types of identifier would require you to run a big part of a Java compiler yourself. In Java there is a long way from parsing to resolving types, so implementing this is a challenging task even for good programmers.
Perhaps your best way through this would be to take the Java Compiler from OpenJDK, run the relevant compiler phases and extract the types from that.
What you need is all the name and type resolution machinery. As another poster observed, one way you can get that is to abuse the Java compiler.
But you likely have some goal in mind other than compiling java; once you have those names, you want to do something with them. The Java compiler is unlikely to help you here.
What you really want is a foundation for building a tool that processes the Java language, including name and type resolution, that will help you do the rest of your task.
Our DMS Software Reengineering Toolkit is generalized program analysis and transformation machinery. It parses code, builds ASTs, manages symbol tables, provides generic flow analysis mechnisms, supports AST modification (or construction) both procedurally and in terms of surface syntax pattens, including (re)generation of compilable text from the ASTs including any comments.
DMS has a Java Front End that enables DMS to process Java, build Java ASTs, do all that name and type resolution you want. Yes, that's a lot of machinery, equivalent to what the Java compiler has, go read your latest Java reference manual. You can build whatever custom tool you need on top of that foundation.
What you won't be able to do as a practical issue is full, accurate name and type resolution without the rest of the Java source files (or corresponding class files), no matter how you tackle it. You might be able to produce some heuristic guess, but that's all it would be.
We want to design a simple domain specific language for writing test scripts to automatically test a XML-based interface of one of our applications. A sample test would be:
Get an input XML file from network shared folder or subversion repository
Import the XML file using the interface
Check if the import result message was successfull
Export the XML corresponding to the object that was just imported using the interface and check if it correct.
If the domain specific language can be declarative and its statements look as close as my sentences in the sample above as possible, it will be awesome because people won't necessarily have to be programmers to understand/write/maintain the tests. Something like:
newObject = GET FILE "http://svn/repos/template1.xml"
reponseMessage = IMPORT newObject
newObjectID = GET PROPERTY '/object/id/' FROM responseMessage
(..)
But then I'm not sure how to implement a simple parser for that languange in Java. Back in school, 10 years ago, I coded a language parser using Lex and Yacc for the C language. Maybe an approach would be to use some equivalent for Java?
Or, I could give up the idea of having a declarative language and choose an XML-based language instead, which would possibly be easier to create a parser for? What approach would you recommend?
You could try JavaCC or Antlr for creating a parser for your domain specific language. If the editors of that file are not programmers, I would prefer this approach over XML.
Take a look at Xtext - it will take a grammar definition and generate a parser as well as a fully-featured eclipse editor pluging with syntax highlighting and -checking.
ANTLR should suffice
ANTLR, ANother Tool for Language Recognition, is a language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions containing actions in a variety of target languages. ANTLR provides excellent support for tree construction, tree walking, translation, error recovery, and error reporting.
Look at Antlr library. You'll have to use EBNF grammatic to describe your language and then use Antlr to make java classes from your grammatic.
Have a look at how Cucumber defines its test cases:
(source: cukes.info)
http://cukes.info/ - can run in JRuby.
Or, I could give up the idea of having a declarative language and
choose an XML-based language instead,
which would possibly be easier to
create a parser for? What approach
would you recommend?
This could be easily done using XML to describe your test scenarios.
< GETFILE object="newObject" file="http://svn/repos/template1.xml"/ >
Since your example of syntax is quite simple, it should also be possible to simply use StringTokenizer to tokenize and parse these kind of scripts.
If you want to introduce more complex expressions or control structures you probably better choose ANTLR
I realize this thread is 3 years old but still feel prompted to offer my take on it. The questioner asked if Java could be used for a DSL to look as closely as possible like
Get an input XML file from network shared folder or subversion repository
Import the XML file using the interface
Check if the import result message was successfull
Export the XML corresponding to the object that was just imported
using the interface and check if it correct.
The answer is yes it can be done, and has been done for similar needs. Many years ago I built a Java DSL framework that - with simple customization - could allow the following syntax to be used for compilable, runnable code:
file InputFile
message Message
get InputFile from http://<....>
import Message from InputFile
if validate Message export Message
else
begin
! Signal an error
end
In the above, the keywords file, message, get, import, validate and export are all custom keywords, each one requiring two simple classes of less than a page of code to implement their compiler and runtime functions. As each piece of functionality is completed it is dropped into the framework, where it is immediately available to do its job.
Note that this is just one possible form; the exact syntax can be freely chosen by the implementor. The system is effectively a DIY high-level assembly language, using pre-written Java classes to perform all the functional blocks, both for compiling and for the runtime. The framework defines where these bits of functionality have to be placed, and provides the necessary abstract classes and interfaces to be implemented.
The system meets the primary need of clarity, where non-programmers can easily see what's happening. Changes can be made quickly and run immediately as compilation is almost instantaneous.
Complete (open) source code is available on request. There's a generic Java version and also one for Android.
We have a need to generate Java source code. We do this by modeling the abstract syntax tree and have a tree walker that generate the actual source code text. This far all good.
Since my AST code is a bit old, it does not have support for annotations and generics. So I'm looking around for open projects to use for future projects with code generation needs. And this is where the actual issue comes. We want to test that the code generated has the correct behavior.
Here is where I got the idea to actually evaluate the AST instead of generating the java source code, compile it, and run tests against that code. An evaluator would speed up the unit tests, and one could evaluate smaller pieces of generated code, such as only a method, making the "units" more reasonable.
So far i have found the com.sun.codemodel project that seems quite nice as for being a modern (support for java5 and 6 features) AST based code-generating solution.
Anyone know if there is another project that would allow me to evaluate pieces of AST directly (such as a single generated method)?
To evaluate Java, you need all the semantic analysis that goes along with it ("what is the scope of this identifier? What type does it have?") as well as an interpreter.
To get that semantic analysis, you need more than just an AST: you need full name resolution (symbol table building) and type resolution (determination of expression types and validation that expressions are valid in the context in which they are found),
as well as class lookup (which actual method does foo refer to?)
With that, you can consider building an interpreter by crawling over the trees in execution order. You'll also need to build a storage manager; you might not need to do a full garbage collector, but you'll need something. You'll also need an interpreter
for .class files if you really want to run something, which means you need a parser
(and name/type resolution for the class files, too).
I don't know if Eclipse has all this (at least the storage manager part you can get for free :). I'd sort of expect it to, given that its original design was to support Java development, but I've been sorely disappointed by lots of tools over the years.
The DMS Software Reengineering Toolkit is a program analysis/transformation too that handles many languages. It has a full Java front end including parsing, AST building, symbol table construction and name resolution, type resolution, builds call graphs (needed to resolve virtual function calls), and has a .class file reader to boot with name resolution. So it would be a good foundation for building an interpreter.
DMS can construct arbitrary ASTs, too, and then generate source code from them, so it would handle the code generation end, too, just fine.
[The reason DMS exists is the "sorely disappointed" part].
I'm not sure if this is what you're looking for, but Eclipse's JDT project provides a very good view on the Java AST (including the Java 5 and 6 features). It has a series of utilities and tools for code viewing/rewriting (not necessarily generation). They're all licensed under the Eclipse Public License.
You can get more info at http://eclipse.org/jdt/
Has anyone used the noweb literate programming tool on a large Java project, where several source code files must be generated in different subdirectories? How did you manage this with noweb? Are there any resources and/or best practices out there?
Noweb will dump out files relative to the current working directory, or at the absolute path you specify. Just don't use * at the end of your filename (to avoid inserting the # preprocessor directives). I would recommend using %def with # to show where you define and use names.
<</path/to/file.java>>=
reallyImportantVariable += 1;
# %def reallyImportantVariable
noweb lets you reorder and (the real win) reuse snippets of code, which I don't think javac would understand.
I'd agree that since most people expect that you'll use javadoc, you're probably swimming against the stream to use noweb.
Literate Programming works its best if the generated intermediate code can point back to the original source file to allow debugging, and analyzing compiler errors. This usually means pre processor support, which Java doesn't support.
Additionally Literate Programming is really not necessary for Java, as the original need for a strict sequential order - which was what prompted Knuth to write a tool to put snippets together in the appropriate sequence - is not present. The final benefit of literate programming, namely being able to write prose about the code, is also available as Javadoc which allow you to put everything in as comments.
To me, there is no benefit in literate programming for Java, and only troubles (just imagine getting IDE support).
Any particular reason you are considering it?