How to Programmatically Modify Java Source Code - java

I am going to need the ability to programmatically modify Java source code, specifically apply refactorings. For example moving a method from one class to another, changing an access modifier from public to private, etc.
Now in C# I'd probably go the abstract syntax tree / Roslyn approach, but I have no idea where to even begin in Java.
Given a java source code file, how does one parse it so that modifications can be made and then saved to it?

First: Things like that (called refactoring) are already handled by a good Java IDE (Eclipse, IntelliJ IDEA, NetBeans and you name it). I really think that stuff like that should be handled by an IDE.
Second: If you really want to edit the files programmatically keep in mind that you're the one who is in charge of format and syntax. You have to read the files, edit them (cut out, paste in or add some code at proper line) and save them. The link provided from VedX is a good start for basic IO. But there is so much more to keep track of.
I did something similar with OpenEdge ABL and it is no easy task (you'll find things that went wrong you never thought of and if you didn't backed up your code it's f**ked up). I've got it done but in the end it wasn't a good trade-off between time spend to get it done and actual usefullness (I just needed it about 4 times for approx. 1500 files).
Finally (after I lost many hours inventing the wheel of refactoring) I wrote some class generators which wrote a whole new file evertime I needed to change something - lastly I only changed the class generators (maybe this is something you didn't thought of and it fits too). Now it's automatized and generic and could run in parallel (something what OpenEdge isn't designed for - okay it's a cheap hack by me).
Third: Maybe there is a package you can use in your Java project. I would try to Google it. My first attempt resulted in this: http://www.methodsandtools.com/archive/archive.php?id=4 for first understanding what it's all about and what to consider.

Related

How to load a .java file into a variable? [duplicate]

This question already has answers here:
What is a Java ClassLoader?
(8 answers)
Closed 5 months ago.
I'm still fairly new to java and currently working on a text-based adventure game as a practice project.
The engine loads scenes to play that are all child-classes from a "Scene" superclass and appear as for eg. "dungeon.java". Now I want the game to be expandable.
The ideas is that a user can drop new scenes as .java-files into a "Scenes" folder and everytime the game is launched it reads all files in that folder and safes them into a "Scene"-class array.
My problem is that I don't know how to code this. I googled a lot and tried various phrasings but all I can find are tutorials for reading lines from txt-files or similar.
Is it even possible to read a complete file into a variable without serialzation?
I already tried the following code, but didn't get around to test it yet.
private static void buildScenePool() {
File scenesFolder = new File("/scenes");
\\ setup filter for .java files
FilenameFilter javafilter = (dir, name) -> name.endsWith(".java");
File[] sceneList = scenesFolder.listFiles(javafilter);
\\ create new arry large enough for all scenes
allScenes = new Scene[sceneList.length];
try{
FileInputStream fileIn = new FileInputStream(scenesFolder);
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
\\ iterate trough the list array and safe files to array
for (int x = 0; x < allScenes.length; x++) {
allScenes[x] = objectIn.readObject( (Scene)sceneList[x] );
}
objectIn.close;
} catch (IOException e) {
System.err.println(e.toString());
}
}
This is rather very complicated. java code is normally compiled, and thus, that means you'd have to scan for a new java file, compile it (which is its own complicated ordeal, as that also means setting up the classpath and the like properly so that the compiler knows what to do), then load in the class file the compiler produced, and then call the appropriate methods in there.
You can do that. It's just, quite complicated. Your standard JVM doesn't necessarily even ship with a compiler; this is solvable too (either demand that this runs only on one that does, and the modern deployment rules for java involve you getting a JVM on your user's machines, so you thus pick one that does include a compiler – or you can just ship the compiler as dependency with your app, javac is itself a java app and runs on any JVM).
However, the more usual approach is to not actually use java for this. Instead, use something java-like, but not java: Scripting languages, like groovy, or javascript (okay, that is not particularly java-like perhaps).
That is its own sort of complication. There are no easy answers to any of this.
I think it's time to first think broad strokes and determine how you want the user's experience (that is, a user that wants to add a scene) should be, and then ask a new SO question about your specific choice.
You write em, they install em
In this model, users simply download or pick a 'scene' impl that someone else wrote (a 'real' programmer with a full fork of the entire source code, an IDE, a build tool, the works). Don't knock it - programming is hard, and saying: "Oh, look, anybody can customize a scene, it's easy, just open notepad.exe, write something like this (example java file here), and dump it in the Scene folder and off you go!", but this is not at all easy. To us programmers that seems familiar at least, but to your average user you're asking them to just rattle off a poem in ancient sumerian - normal people don't program. And if they do program, they're programmers. They would much rather get instructions about how to fork a project and set it up in an IDE than some bizarreness about tossing raw java files someplace.
Scripting
This is what Processing (for programming arduinos) does, more or less what webbrowsers did (which explains why javascript is so weird), and most 'plugin systems' for pseudo-smart editors like TextMate and Emacs do: You script them. As in, you fully buy into the idea that the custom stuff written by the user is extremely simple and highly targeted (really only makes sense to run within the confines of your app, not as standalone stuff - and dependencies don't generally come up), and pick a language that fits that model, and java certainly is not that.
Obvious options are javascript and groovy. This is certainly possible but not at all easy. Still, if you're interested, search the web for tutorials on how to run javascript or groovy inside a JVM, and you'll get plenty of hits.
Java code, and you compile it
That's what your question is positing as only option. I don't recommend it, but you can do this if you must. Be aware that it seems to me, based on the way you worded your question and your example code which makes various newbie mistakes (such as print-and-continue exception handling, which is always wrong, using obsolete APIs, and messing with built-in serialization) that this is a few ballparks beyond your current skillset. A challenge is always cool, so, if you want to go for it, you should! Just be aware it'll be the most difficult thing you've ever written and it'll take a few fully dedicated weeks, with a lot of reading and experimenting.
Just definitions, really
The central tenet so far has been that you can actually program. Instructions that make the machine act in certain ways. Possibly you don't need any of that. If a Scene is really just a background colour and a few widgets displayed here and there, should it even be code at all? Maybe you just want a declarative setup: The plugin/scene writer just declares certain properties and that's all they get to do, you just 'run' such a declarative definition. In which case the 'language' of the declaration can be in JSON, XML, YAML, TOML, or any other format designed for configuration files and such, and you can forego the hairy business of attempting to compile/run user-provided code in the first place.
In order to load the Java classes into your application, you need to compile them. You could do this from Java by invoking the javac executable. See Starting a process in Java? for instructions on how you could do that. Once compiled, you'd then need to load the classes into the JVM using a class loader, e.g. by invoking ClassLoader.defineClass. You probably want to configure a protection domain as well, to prevent user provided classes from misbehaving.
However, Java might not be the best approach for extending your application. You could consider using a scripting language instead, like JavaScript. See Nashorn (an open source script engine that was included in previous versions of Java, and that can now be downloaded separately) and the Java Scripting Programmer's Guide for more information.

Java: Parse JavaScript & Flag Errors

I've been having terrible luck trying to get this to work, so I'm hopeful someone can help here.
In Java, I need to be able to take an HTML page with JavaScript within it and detect any JavaScript errors without, preferably without executing the JavaScript code.
I found this article:
Javascript parser for Java
And I've attempted to figure out how I'm supposed to use Caja to do this, but I'm having a difficult time finding any documentation with working examples of anything close to what I'm doing.
As a result I took a look at Nashorn also referenced in that article. I found a few examples which show how to execute JavaScript code from Java, but this doesn't process the whole HTML page. Even then, the execution doesn't seem to include the ability to validate common JavaScript functions (e.g. It hadn't heard of "alert").
Can anyone recommend something that might be able to do what I want, and point me in the right direction for their documentation or give me an example?
jshint as a standalone product seems to be a good fit for this:
it can run in java inside rhino (see https://github.com/jshint/jshint/)
a nodejs package exists (see https://www.npmjs.com/package/jshint)
it works with nashorn but it's quite tricky
I will only cover the technical difficulties around 3rd solution as I finally managed to make it work too...
Spoiler alert: "alert()" is not detected yet... Solution nb 2 will help there...
You first need to grab this specific release of jshint: https://github.com/jshint/jshint/releases/tag/2.4.4
Anything later than v2.7.0 will fail for now and I personally gave up patching intensively prototypes and namespaces... Releases from v2.4.4 until v2.6.3 work without modification but are limited in functionalities.
In the release notes, it's specifically written that "support for the Nashorn JavaScript engine" is working on this release. I'm using JDK8 nashorn 1.8.0_45 for this test.
Next step is to extract from this release this single file jshint-2.4.4/dist/jshint-rhino.js
Now you need to run nashorn/jjs in scripting mode and you need to be specific about the single file you wish to verify. In solution 2 (nodejs based) you can do multiple files or a complete hierarchy below a folder...
Create a simple file file.js:
function(){}
Now run the following command (please note the presence of -- ):
jjs -scripting jshint-rhino.js -- file.js
This will give you the following output:
Missing name in function declaration. (file.js:1:9)
> function(){}
So this covers the how to run jshint in a simple manner with nashorn... With the 3rd solution, at least you can find missing semicolons and several typical errors. But it's not a silver bullet and to me it's not a real alternative.
My personal preference would be to stick to solution 2 only. If you've the possibility to install either nodejs or iojs on your dev platform, go and grab https://www.npmjs.com/package/jshint. Not only will you be able to do more than the 3rd solution, you'll also be able to configure a jshintrc file as described at http://jshint.com/docs/

Drools workbench goals

I'm really new to the drools environnement, and try to understand it.
I've done a first java project which basicaly parse a drl rules files, apply them to a java object and output a result according the object properties and the rules.
Stop me if I'm wrong, but I think it's the ultra-basics of drools.
My second step was to use a decision table (xls) : I compiled it to a drl file and used it. It works.
Now, I'm looking for a tool allowing to manage xls, compiling them live to drl. I was thinking it was the aim of the drools Workbench (old guvnor) so I installed it(yes I succeeded ;-) ).
With the help of http://docs.jboss.org/drools/release/latest/drools-docs/html/wb.Workbench.html#wb.Installation , I created a new project containing my model, my drl, and I deployed it...
->But now, I'm not really clear about the next steps : there is a maven repository, but what does it contain?
->Was I'm thinking right that the workbench could allow me to dynamicaly compile my xls to drl?
->Do I missed a point about the workbench?
(The question invites being closed as its answers will be "primarily opinion-based". But it has been around for 10 hours no without a single vote for close, so I'll just answer it, based on my opinion and my experience. YMMV.)
Regarding maven, it is of course the prime choice as a build tool for Java applications in general, and Drools supports this very well. OTOH, you may write a non-trivial application in, say, Eclipse, compile your rules offline, use simple jar building step for .class and serialized packages or rule base and deploy that. The development process of your institution will have its say, or you take your pick.
The foremost intent of Drools Workbench is to make rule programming viable for, well, hum, non-programmers. I have already unveiled myself as an orthodox believer in the dogma that, except for the mechanic parts, programming remains an "art" (D.E.Knuth), a "discipline" (E.W.Dijkstra) and a "science" (D.Gries). There is nothing wrong with entering the ingredients for a Java Bean via some GUI (provided you have learned the basics about data types including collections), although doing the equivalent in a modern IDE isn't really difficult. Composing rules, conditions and consequences, in a GUI is where I begin to have my doubts. You'll need a full understanding of the underlying principles anyway, and switching between code items in a GUI is for me highly inconvenient in comparison to seeing them (with syntax highlighting and all) all in a modern code editor, with its search and replace and refactoring possibilities.
A last word on the XLS decision table. While many see this as a strong point in favour of Drools, I always add the warning that its unreflected use leads to an abuse of rules as a collection for sets of literals, with uniform logic throughout, something which is much more efficiently represented by a collection of facts and a single rule.
No doubt, others may have to say more in favour of the workbench - not that I'm completely blind to its advantages either ;-)

Compile a Java program on the server right before a User downloads the program

I did a quick search and could not find anything on this topic. I am not even sure if this is possible, but I am curious.
Is it possible to compile a Java program on a server right before a user downloads the program. The application that I can think of for this would be to modify a program's source code on the fly before a user downloads it. This could be helpful in a setup where the program is modified based on user input or settings on a website and those changes are hard baked into the program so what they download is a stand alone program that is customized and fully portable. The other application I thought of would be if each user were to use a different feature combination in a program so it is compiled only with the feature set they need/want on the fly.
I have a few programs ideas that I could test this out with, but this is mostly an academic thought and curiosity of mine.
So long story short, does anyone know of any technologies that could make a system like this work?
Sure, it's possible.
Just let the download link point to some script, that compiles / packages the source and sends back the result. This can be implemented in, for instance PHP, in just a few lines of code. It's quite similar to captchas: On-the-fly generated unique data, retrieved through a URL.
I myself have thought about this idea for protocol obfuscation purposes and for "software registration key algorithm" generation.
I would however recommend you to factor out the parts which you want to be recompiled into a separate class / set of classes, compile only these, and package it with the rest of the (already compiled) program upon request.
I have written a library using the Compile API (comes with Java 6) to compile code in ememory but I would suggest you don't need to pre-generate code as anything you can do with generated code you can do with dynamic code. There can be a slight performance advantage, but I suggest you try doing what you need with dynamic code (i.e. code with loops, if statements and reflection) to do what your generated code would do first as this is alot simpler and likely to do what you want.
Even if you must have generated code, it is useful to write the code in a non-generated form first so you are clear as to what you need the code to do.

Using noweb on a large Java project

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?

Categories