Alternative to Eclipse's Abstract Syntax Tree parser for code manipulation - java

Background
I am writing a program that will do some bulk renaming of members and functions in a directory of java source code to de-obfuscate the code based on a look-up table .csv file passed in to the program.
What this is for is the source code I have was written against a obfuscated jar. I have a de-obfuscated version of the jar that was run through a customized version RetroGaurd and I would like to parse the mapping file that was passed in to RetroGaurd to de-obfuscate the function calls my source code makes in to the jar. If I just compile my code and run it through RetroGaurd too when I decompile I loose all of my nice commenting and formatting (unless there is a option on RetroGaurd that I missed).
Problem
I found the Abstract Syntax Tree parser built in to Eclipse and it looks perfect for my uses, however I am not planning on writing my program as a plugin for Eclipse, this is going to be a stand alone jar that can be run on any machine.
My main concern is as I write my code I am getting a lot of dependencies on internal jars that Eclipse uses. I know that if I conform to the EPL for the library jars I will have no issues distributing it, but I am concerned about this project getting bigger and bigger as I write it as more and more jars from Eclipse's SDK are required.
Are there any other projects out there that would give me the ability to parse Java source code to do find and replace reliably like AST will allow me to, or is there a way to use RetroGaurd (or a program like it) to run the same de-obfuscation but keep my comments and functions the same without needing to run the de-obfuscated program though a de-compiler afterwards?

If you're worried about the need to run Eclipse GUI to execute your code, then you could consider running the plugin in headless mode. This would allow your plugin to be run from command line. See this SO thread.
You could also use any other open source java compiler. For e.g., openjdk's Java compiler. Please refer to the Resources section.
Hope it helps.

Related

Looking for Makefile and Build System solutions for Java

I am trying to veer away from writing most of my Java code in Eclipse and I'm coming up with small projects for myself to work on in order to a. become a better Java programmer and b. learn how to organize my applications (in both terms of code and directory structure).
I noticed that I make plenty of small, incremental changes to classes then to recompile and run my whole application. I'm slightly familiar with Makefiles from a course I took in C++, and I'm less familiar with Build Systems (Maven, Ant). Regarding this mater, here are a few things I'd appreciate help with:
First of all, is there a way that I can write a little file that separates where my .class files are saved during javac *.java?
Say I want to put all my .class files in a /bin folder
In that file, is there a command I can write to simplify the javac *.java and java [class name here...] process?
I know that this process is relatively simple already, but what I want to know is can I write something that will allow me to run commands along the lines of myExec build (compile and save all my java files) myExec run (run my application)
Finally, is there a simple Build System that I can/should learn to use that will allow me to accomplish this? Or am I confusing the point of a Build System.
Look at Maven or Gradle, it automatically separates the source file and class files.
Manual build script writing is waist of time really.

NetBeans output of Java does too much

I have a 202 instructor who says that he feels its fine to use netbeans or eclipse but that for the final project he wants a file that he can load on his xp (I'm not sure why xp) machine, compile and run with the following commands:
javac *.java
java FinalProject
Up to this point I have been editing in a simple text pad like program and missing netbeans but to be fair I can't figure out how to code in netbeans in such a way that I get a generic set of files with no handily added code. If anyone could tell me how to convince netbeans that I don't need packages, ant build, team work software, and a bunch of netbeans helpful files lying around in my code I would really really appreciate it.
First you must get better understanding about IDE. Refer here
for NetBeans and Eclipse
When we write a simple program in few lines we can do with notepad and compile with javac. If we go for a big project there are lot of stuff and features required like
Adding external lib
UI Frame work
Identifying syntax error
Easy compilation, debugging and execution
Writing unit test etc.,
In netbeans, you just take "src" folder and use it.
So with netbeans all the source code files get put into a src directory. This contains nothing other than source files. In order to compile it using javac *.java you should place all your java files in the <default package> or without a package(Netbeans will warn that this is bad practice but you can ignore that for now). To run the program using java FinalProject you need to make a java file called FinalProject.java(with a class called FinalProject. You can create any additional classes in external files as long as they all go in the same <default package>.
Netbeans' scripts will just make it easier for you to compile and test your code. And to submit you just need to submit all the files in the src folder.
Also, as a side note, If you are creating GUI's using netbeans, you should probably use GridBagLayout rather than the default layout as that adds an additional library that will mean that your instructor's compilation will fail.

Java - How to make program run jar files internally

I was wondering ... I want to use a plugin-type thing with my Java program.
Here is the situation:
I have compiled a source file (.java) into a .jar file using MY .JAR program as a library. How to I make MY program run the other .jar file internally (using the main program as a reference).
I know this is weird (it sounds weird to me too), but if anyone understands what I am trying to say, please comment.
Thank you all in advance!
OK, here's a draft of how to do it.
Create an interface with a "run()" method.
Your .java plugin must implement that interface.
Load the all classes in classpath (help here Find Java classes implementing an interface)
run your plugin by executing the run method of the interface.
You would have to run the jar using the standard syntax. Your question is basically about running console commands inside java. Here is a nice answer to a similar question:
link!
This isn't necessarily exactly what you want, but it's goal is to put you on the right track, basically you would get the jar placement, then check for the system, then use the technique used there to run a command through the specific platform's console.

Java Makefile Output Single Executable

How can I create a Makefile to compile my Java program with a single source file located in src/Hello.java to ouput a file that I can run with ./prog
(1) Take a look at http://ant.apache.org/ - that's a build tool suited to Java better than make. For example check out the javac command in ant.
(2) You have to run java programs by running the java virtual machine (java) and by telling it what to execute. There is no ./xxx way to run a program in java; that method executes scripts or executable programs, and a compiled java program is bytecode which is neither. What you need is to create a little shell script, call it "Hello", give it execute permissions with chmod, its contents should be something like:
#!/bin/bash
java -cp . Hello
Make is not a great tool for building Java programs, primarily because its core design is to identify out-of-date sources (either source or object files) and apply transforms to them. The Java compiler handles this dependency checking already, and defining a Make rule for Java compilation is more trouble than it's worth.
Adrian Smith has already suggested Ant. I'm going to suggest Maven as an alternative.
My main reason for this suggestion is that it's very easy to get a simple project going in Maven: you can tell Maven to create the entire directory structure, and then it just works. And although you may not like the directory structure that Maven creates, there's a benefit to the consistency if you're working in a large project. Maven does have its limitations, and some of them are extremely painful, but you generally won't run into them until you have the knowledge to work around them.
As for running a Java program, you need to invoke the JVM somewhere along the line. A shell script is one approach, but generally it's easier just to invoke the JVM directly. If you create an "executable JAR" (which Maven will do for you, including options to include all dependencies), you can invoke it like this:
java -jar executable.jar

java cvs,svn file structure

I have recently learnt how to program in java, I was looking at some open source programs from sourceforge.net and after downloading these programs I don't understand the file structure most of the programs follow. Pretty much every program has src,bin,lib etc folders, how do I know the standard way of organizing my program. Is there any book or resource which explains this? also how do I compile this source code once I have downloaded it, to make a jar file from it
thanks
src is (usually) what it sounds like: source code
bin is (usually) shell scripts related to the product
lib is (usually) external dependencies needed for compilation
Most projects document how they build: look for a README or grok the project website. These days for a Java-centric project I would expect Ant or Maven.
What you have downloaded is actually a distribution version of the program. The directories as listed by carej are kind of a convention, mainly derived from how things are usually done on a Unix system.
Most projects supply some manual on how to build. This can be tricky if not all libraries (jars) used by the program are supplied in the distribution. Some of the Apache Commons project do it like this, thus forcing the user to download dependencies seperately.
If you just want to use the program try to find a binary version. This will usually consist of jars, scripts and documentation. Source distributions are useful if you want to look at the source and/or make modifications.

Categories

Resources