Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
we're writing some sort of compiler from Pascal to JVM Bytecode. And we've already implemented an expression tree generation, so the next step should be the creation of .class file. Can you suggest any guide/tutorial of how to generate any .class file at least from some static data? Because I've googled for 2 hours already and read JVM specification, but I really need some even simplest example to start developing the whole stuff.
Someone has already written a widely-used byte code generation library: CGLIB.
You'd have it knocked if you could figure out how to get your AST into CGLIB.
Actually there is an example file inside ASM folder that you download. It's called Helloworld and it's located in examples subfolder. It shows how to compile (generate from scratch) .class file that corresponds to simple hello world app. It also shows how to get date from .class files but it's another story.
Maybe this is'n the best way, but when you need to start with java byte code generation and you need some basic examples it's a good idea to have a look at ASM and the examples that are bundled within standard package.
Moreover Groovy uses ASM to generate its code :)
I don't know if you are aware, but there is a backend for the FPC that generates bytecode compliant to the JDK 1.5. The development looks fairly recent (November 2011). You should have a look at it.
There are a couple of widely-used bytecode generation projects.
ASM and CGLib are probably the two best examples.
You probably don't want to build a generation library for yourself from scratch - it's a lot of work, difficult to get right and probably doesn't offer you much over using an existing project.
ASM is widely used by non-Java languages on the JVM, has OK-ish documentation and is not too bad to get going.
I haven't used CGLib as much, but I didn't find it as easy to get started with.
As a final data point, the Java 8 team are prototyping some of the new Java features (including lambda expressions) with ASM.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
The company I work at has a piece of code they would like to rewrite. It uses Java/Java server faces. The problems with it are that it is old and uses depreciated code, was written in an IDE that is no longer available and doesn't work well with netbeans, and that it is kind of sloppy coding in the first place. No one really knows its structure and there is limited documentation.
Before beginning on the rewrite, we would like to find the structure of the old program and get a decent UML diagram. What tools would work the best in this situation? So far we have looked at one called Agilej.
Sorry if this is a little vague, I'm just a lowly intern an haven't been filled in on everything yet =p
You can use JArchitect, a pretty complete java static analysis tool
I'm not giving clickable hyperlinks as you should be able to find them easily through a web search and there may be more than 1 link that is useful for you
Sparx Systems Enterprise Architect has very powerful reverse engineering capabilities. Java (also compiled binary JAR) is in the list of supported languages.
I have used Enterprise Architect's reverse engineering features several years ago to help us understand and design modifications of legacy C++, QT code.
Enterprise Architect was able to automatically retrieve the class model. I then used the raw class model to drag/drop draw several other diagrams including only classes of my interest with level of detail I needed etc.
In the range of other static code analysis tools (e.g. the code flow visualization) I don't know which tool particularly supports Java well enough. Quick Google points e.g. to Coder Gears JArchitect. In the past project I have mentioned we used Doxygen's automatically generated documentation. Part of that were also some automatically generated graphviz dependency diagrams.
To clarify the design and visualize the flow (especially big legacy functions) I have found useful the Rapid Quality Systems Code Rocket flow visualizer
Once you get the basic facts at your hands through the various reverse engineering tools next step would be to go through them, annotate what is the unknown, what is doing what, basically apply some formal code review method (e.g. Fagan inspection or some of its derivatives).
(using evaluation versions of the various tools may be enough if you think ahead what are the required deliverables for your follow up actions. I guess the company is not planing to give you a >0$ budget)
The Agile Modeling site may have some good refactoring tips and some minimal UML mapping guidelines, start e.g. at Agile Legacy System Analysis and Integration Modeling
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to create a exe file for my java project but the problem is that anyone can get my application code from exe. Is there any way so that no one can get source code of application from exe file
?
You can create an exe with an application such as Excelsior Jet, (as I have in the past) or another freeware (or payable software if you want to invest in it) from the internet. The problem with excelsior jet is that if you want it for free, you will only get a 90 day trial period, and all created exes stop working after the trial has ended. Exes are not crack proof, however they do make getting to the source code a bit tougher. The other bonus of creating an exe is that the final user will not be so dependent on Java to be installed in their computer.
Is there any way so that no one can get source code of application from exe file ?
No.
And a short elaboration (to what I expect the author asked):
Java classes are compiled to bytecode to be executed by a JVM. As JVMs were primarily designed to be implemented by software, the JVM instruction set contains certain high-level operations, such as object creation, method invocation etc. that allow 1:1 mapping to high-level operations in the source code. This, along with the fact that the JVM instruction set is very-well documented, is one of primary reasons why it is so easy to reconstruct the Java source code.
And a short elaboration (to what was asked):
Creating a platform-targeting executable out of a Java class is not a trivial task. One of the reasons might be the fact that the whole Java standard library (including essential java.lang classes) is implemented in Java.
Assuming that you have solved that issue, most probably by providing a subset of the standard library classes implemented natively, the resulting assembler code can still be reverse-engineered back into the source code. It is possible to track down class/object/method boundaries and work your way up from there. Of course, the resulting source may not be nearly as readable/comprehensible.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I am looking for a simple dynamic call graph logger for Java that you can add in a few lines of code. I know there is an Aspect J solution. Also, I helped Zola develop Glow for C/C++ so I could rewrite a similar tool but I don't want to dig into JVM internals.
Any open source solution out there right now that is stable and better than the AspectJ solution?
The purpose is to use as a companion to unit testing certain portions of the code that you want more information about their behavior.
I think you want to collect a call graph (as opposed to just a set of calls) by any means possible.
One can do with with a static analyzer (if you can get a strong enough one), to collect the potential call graph. A dynamic method collects one at runtime by instrumenting the code. Some folks may specifically want the dynamic one, because they want to see the actual call graph for a specific set of input data.
There are several Java profilers that will collect this information dynamically, including ours. None of the ones that do that are open source, that I know of, but I could be wrong.
Such profilers often work by instrumenting the code (either source or VM code if the language [e.g., Java,C#] has such). How they do it depends on the supplier.
In our case, we use our program transformation tools to transform the source code from its original form, into a form that also collects profiling data.
You can use AspectJ to insert instrumentation to do this, too. [It is worth noting that aspects are just a special case of program transformation]. Of course, there's more work than just instrumenting the code; you have to collect the runtime data efficiently and after execution process to produce the call graph. So its rather a bit of work to do all this but you presumably know that from your Glow experience.
Maybe off-topic, but are you sure you want actually call graph? Somehow I think such a detailed graph will be next to useless in a reasonably sized application. What I find much more useful is a dependency graph between classes, one that is very easy to get as long as you use some kind of dependency injection. I used google guice (and it was actually pretty useful to restructure/keep clean a reasonably sized application).
There is a very nice google-guice-dependency grapher available out-of-the box and for free: http://code.google.com/p/google-guice/wiki/Grapher . I even customized it (extended the Grapher class) to mark different class types with different colors (DAO, controller, API etc.)...
There is instrumentation via the native JVMTI C/C++ native interfaces. Like I said I would like to stay in pure Java.
Java does have a Runtime.getRuntime().traceMethodCalls(), but you need something to consume the output still.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I know about BCEL, but this project seems to be dead, as it had no releases for two years. And the Java-world moves on. For example JDK 1.6 has a new class-file-format.
So what library can be used to create bytecode for the JVM. If no library, a program is ok too, if I can manipulate the generated code in detail, for example a bytecode-assembler.
Which software can you recommend? Is it easy too use? has good examples/tutorials?
EDIT: For all asking: Yes, the javac is fine. But for generating some classes at runtime, a path directly to bytecode would be cleaner.
ASM
http://asm.objectweb.org/
It is much faster than BCEL and supports generics and annotations.
One point about its architecture: in order to ensure high performance ASM is built around a parser that throws events (in contrast to BCEL where the parser builds a data structure). This is somewhat similar to the difference between SAX and DOM parsers. It takes some practice to get used to this kind of thinking.
EDIT (Following McDowell's comment): Indeed visitors are heavily used in ASM, but it's more than plain visitors: the visited data structure is lazily built by the parser, so if you're not interested in certain parts of the classfile (for example, you want to know the names of the methods but you don't care about their body), you can return a null from the visitMethod() method. This will make the parser skip the method body sections thereby preventing the (expensive) construction of the net of objects fully describing the method.
There is a fairly complete example of using ASM to generate byte code from a Java-like intermediate language in the implementation of CAL (a Haskell-like language for the JVM). If you download the sources at http://openquark.org/Open_Quark/Download.html
then you can find the code in AsmJavaByteCodeGenerator.java, and the java model classes in the same folder. The code generated is basically what javac would do, minus debug annotations.
The CAL implementation originally used BCEL but switched to ASM because ASM was significantly faster (probably an order of magnitude), and just as significantly, ASM is thread safe, so that concurrent compilation is possible, which is needed by CAL.
http://serp.sourceforge.net/ is a great library for more abstraction when editing the bytecode.
Javassist and cglib are two good bytecode engineering libraries. They are used extensively in the Java EE world for generating proxies of objects at runtime. Hibernate and Spring are two leading frameworks making use of these libraries.
There are technologies like asm and cglib but I recomend Javaassist because it is a very good library for that and you can find examples in tapestry5 framework.
I think my favorite java bytecode creator is called javac and you can find it at www.sun.com
Why not use the Java compiler, javac? What's wrong with using it to generate JVM byte code?
[Seriously. What stops you from taking your source, making Java and compiling it?]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
Is there a good Eclipse plugin for recording and playing back macros?
I've tried this one, but it didn't do me any good- it seemed like it wasn't ready for prime time.
I know about editor templates, but I'm looking for something that I can use to record my keystrokes and then apply multiple times against a wad of text.
This seems like a strange hole in an IDE. Am I missing some built-in facility for this?
I put something together over the last month or so that you may find useful. It has limitations since the Eclipse editor/commands weren't designed with macro support in mind, but I think it works reasonably well. I just posted Practical Macro at SourceForge a couple of days ago. It requires Eclipse 3.4.
Emacs+ Version 3.x adds keyboard macros (http://www.mulgasoft.com/emacsplus) to its feature set.
This seems like a strange hole in an IDE, am I missing some builtin facility for this?
This is a common problem. There are around four bugs opened in Eclipse tracker for this. Unfortunately you would probably see macros in Eclipse in v4.0 or later.
I've had success using AutoHotKey (Windows only, though).
There was a plug-in called Eclipse Monkey which allowed writing scripts that execute inside the IDE. It was terminated about a month ago due to lack of interest.
It is based on an older plug-in called Groovy Monkey. If you google it, you can still get it. The Aptana team has some more information on using it.
Note that this allows writing scripts, but not recording actions.
This is not an Eclipse-specific one, but it can be used there as well:
http://sikuli.org/
For simple text expansion on a Windows computer, you could use AutoHotkey. It's not as powerful as most macro tools, but since it's not tied to any one program, it can be used in other editors, emails, etc.
For example, if I type ";;ln" AutoHotkey instantly sends the keystrokes to delete this and replace it with "System.out.println();" with the cursor in between the parentheses.
Just for the record, there is another project called MacroSchmacro that does Eclipse macros, but it doesn't record many important things (like searching to navigate). It is also extremely slow.
Talking about Emacs, jEdit has a very strong macro facility. There are a lot of high quality macros and plug-ins, and several macros are already built it in. You can even add some logic using bean scripting, which is analogous to VBA. So, you can write very powerful stuff (any many people have done so).
jEdit is obviously a separate editor, but I think it's worth a shot. See http://www.jedit.org/