I'm looking for a mechanism in java (maybe like Reflection) to access the statements in a program. for example i access the statements of a function, then walk throw the statement tree to have analysis on java programs.
In .Net Microsoft provides the Extended Reflection. what is the alternative in java?
For C files, CIL process the .c source files and allow us to access the statements and walk throw the tree (even changing and inserting codes) statically. if there is a tool that process the .java codes and does similar works, can solve my problem.
If you aim to analyze Java code via a Java application you will need a copy of the source code. .java files are essentially text files, so with the source code in hand your program could read the files similar to reading any text file.
There are several tools such as PMD and Clover that will perform this analysis for you. It may save time and resources to use an established tool. Although Clover is no longer a free tool it provides extensive metrics on code complexity. I believe PMD may provide similar metrics.
Related
I'm having an issue with java reflection.
How can I load a .java file or the whole project then analyze them?
input : .java code
output : analyzed class, method, relations between classes, attributes. v/v
Analyzing .java files is a lot more difficult than it sounds, as they are pure text and therefore requires textual analysis inorder to get something. A tool like PMD knows that and performs static code analysis on .java files.
https://pmd.github.io/
Analyzing .class files however is alot easier. For this task one need to create a custom class loader object (URLClassLoader should work) and use it to search and load all of the Class objects. Then one cause those objects' methods to get information on those classes. A tool that performs static code analysis on .class files is FindBugs.
http://findbugs.sourceforge.net
Hopefully this helps you bit
Whenever I build my app all classes (logically) are visible in the .jar that comes out of it.
Aswell as a class that holds information to my MYSQL server (for the app to connect to). But I dont want this information to be publicly visible!
How can I "hide" this code or "hide" the class?
Thanks!!
I think you mean you dont want someone to do reverse engineering with your .class inside your jar file. There are many decompilers that can do that.
So you would need to Obfuscate your code with an obfuscator utility.
The process of obfuscation will convert bytecode into a logical
equivalent version that is extremely difficult for decompilers to pick
apart. Keep in mind that the decompilation process is extremely
complicated and cannot be easily 'tweaked' to bypassed obfuscated
code. Essentially the process is as follows:
Compile Java source code using a regular compiler (ie. JDK)
Run the obfuscator, passing in the compiled class file as a
parameter. The result will be a different output file (perhaps with a
different extension).
This file, when renamed as a .class file, will be functionally
equivalent to the original bytecode. It will not affect performance
because a virtual machine will still be able to interpret it.
Here is an article describing this process in more detail and
introducing an early obfuscator, Crema:
http://www.javaworld.com/javaworld/javatips/jw-javatip22.html
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.
I have one GUI with one list box to display the list of methods in the class.
I can achieve it using reflection.
But can I view the source code in another text area on selecting the method name?
I knew about decompilers. but I don't want to see source code in their window.
I want to use some thirdparty lib so that I can see the source code of specific method in my own GUI.
Please suggest if there is an API available for this.
You will need a decompiler of some sort, that you can link to. I am not sure there are any libraries, but here's a link to the JD Java Decompiler.
Remember that you lose variable names and such during compilation, so if you decompile the resulting source code may be less readable.
If you have access to the source you could link it to the class files, and find the chosen method source in the source files linked. This can be achieved by a simple one-pass parse of the source files.
Your biggest problem will be determining when a method ends, and a simple solution is to count {'s and }'s and determine when the { of the method declaration is closed.
This is an old question, but seeing as the decompiler landscape has changed significantly in the past year, I feel it's worth resurrecting.
Procyon is an open source framework that contains, among other things, a Java decompiler. It is written in Java, and the decompiler APIs can be integrated into another application fairly easily. In fact, there are already two third-party GUI front-ends, including the SecureTeam Java Decompiler.
CFR does not have source code available yet, but it is also an excellent decompiler. It too is written in Java, and while I have not tried to integrate it with an existing application, it should certainly be possible.
I once created application that included it's own source code viewer. I think it's a good alternative to decompilers, which can come with quite dependencies.
I was using NetBeans so packaging the .java files was as easy as changing one filter option. I checked java properties to find the jar file and scanned it just as any zip file for java source files. With this approach having a GUI with JTreeTable populated with source files and JTextArea displaying source code was trivial.
I believe You could do the same with addition of one step more - clipping the source to contain only the selected method. I think it should boil down to simple parser, one that counts opening and closing brackets.
I'm leaving the earlier answer up in case you need it, but JODE hasn't been updated in a long time. Searching around, I can't find any decompiler that is open-source or available in library form.
JODE may be just what you want. The core decompiler is released as a library under the GNU LGPL, so you can integrate it into your program with no issues.
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/