can srcgen4javasssit be used to generate source code of my existing class? - java

i am working with javassist and willing to regenerate the source code / alter the classes. but the problem is that i am unable to find a library to generate source code of my existing classes from .class file and then use javassist to edit them.
i want a library to use in my project to generate the source code from existing class and then modify it.
while searching web, i cam to know about http://www.fuin.org/srcgen4javassist srcgen4javassist but i am unable to use it for my existing classes. can u guide plz ?

Javassist is a framework to modify JVM byte code that means you do not modify the source code (.java file) but rather the byte code (.class file).
On the other hand if what you really want is to decompile classes, by other words generate java source files from the bytecode, my suggestion is to use JD decompiler which also has an eclipse plugin which can help you a lot.
I don't know srcgen4javassist, so I won't comment on that project, but I can assure you that JD won't let you down.

Related

auto-generated java files in Eclipse

How to understand which are the auto-generated files in a huge Java project developed in Eclipse?
I am a newbie. For example, little by little, I am discovering that many files are generated by using xcore plug-in. Other with xtend. Just going through the code and trying I am learning and that's ok. The question is: is there a way to understand which files to modify to automatically regenerate the others? My error was to start modifying all the files manually.
Usually you set up different source directories for your code and generated code.
For example, a project using Xtend and EMF would have following source directories in its build path:
src contains all Java and Xtend files that you write
xtend-gen contains generated Java files created by Xtend
src-gen contains generated Java files created by EMF
In this setup you should only edit files in src. Files in xtend-gen will be updated automatically if you edit Xtend files in src. Files in src-gen will be updated if you regenerate the model.
I'm not that familiar with Xcore, but since it is based on EMF I think you just have to set the "model directory" property of the genmodel.
This should make the distinction between your code and generated code more clear. You may still feel the need to modify generated code sometimes. EMF actually supports this by adding special annotations in the generated file but I would not recommend this, because it's very hard to see if a file has been modified this way. If you really need to change the generated behavior, the first approach described in this article about properly overriding generated EMF code is better. Basically you extend and override methods of some generated classes and the factory and then use Eclipse extension points to replace the generated factory with your extended one.
If you are looking at a project of someone else and don't know which code generating tools are used:
You should try to ask the authors if possible or check if there is any documentation about building the project. Otherwise I guess you'll need to analyze the project structure to see which plugins are used to generate code. This might be a bit hard if you don't already know which plugins actually can generate code though.
Check the Eclipse "Project Nature" to see which plugins are used to build
Check build configuration (Ant, Maven, Gradle) for plugins that might generate code
Look for special files (xcore, genmodel, etc) and figure out to which
Check if there are any Annotation Processors configured and check if they create any files
(Xtend only) Check if there are Active Annotation and check if they create any files
Check if #Generated annotations is used in the code, which is used to mark generated files
If you have Identified all tools, then try to change the model destination directory and regenerate the code. Then compare the generated files with your original code - all duplicated files are likely generated.

How to get source code from .jar file

I have my project in .jar file.
But i lost the source code of it.
How to get source code from .jar file.
First of all I would like to tell you that if your project is big and complex, you are in trouble. Generated source code via external tools(no matter whatever the tool is) is never same as the real code. Code like comments, constants, inner classes, etc gets messy.
For simpler code and projects you can use -
Java Decompiler (JD-GUI) http://jd.benow.ca/
DJ Java Decompiler http://www.neshkov.com/dj.html
But always know this that its not what was written originally in the source code.
You must decompile your code. You can use Java decompiler http://jd.benow.ca/

How do I change a double in a compiled jar?

I have a jar, which contains a line of code which compares two doubles, one having the value of 0.7, but I need to be able to change that to 0.0.
I cannot use reflection or anything like that because of the fact that this is a compiled jar file (not open source) but changing this value is crucial, because if it stays at 0.7 there is a huge lag problem.
I was thinking of editing bytecode, but cannot find any good software to do so.
I appreciate any help with this.
Look into how to decompile the program Java classes, and then re-compile the software yourself.
How do I "decompile" Java class files?
From another similar question: (Change string constant in a compiled class)
If you have the sources for this class, then my approach is:
Get the JAR file
Get the source for the single class
Compile the source with the JAR on the classpath (that way, you don't have to compile anything else; it doesn't hurt that the JAR already contains the binary). You can use the latest Java version for this; just downgrade the compiler using -source and -target.
Replace the class file in the JAR with the new one using jar u or an Ant task
If you can decompile the class, you can make the change and recompile it. Then roll up the jarfile with your patched class. It depends on the class and the JDK used to compile it how easy this will be.
References: JAD (Wiki link because JAD is basically a dead project, though it continues to work when I need it to.) If you use Eclipse, you might have luck with JD-Eclipse. There is also the Procyon project, which I have had limited success with.
Besides using (Hex) Editor or decompiler, use can use a byte code manipulation lib like asm. You actually might even be able to use an pre-load agent to transform the code on the fly.

Java Edit Jar File

I have a jar file which includes seviral classes. In that jar there is a Confirmation.class file which i want to edit. I decompiled that class by usin JAD. Then i edit it with notepad++ and saved as .java file.
Now how can i create my new jar file with other classes?
Other files format is .class but mine is .java, is it problem?
If it is , how can i compile my .java class ? (when i use command javac Confirmation.java it gives errors and want other classes)
Thanks..
Do you have dependencies to other jars?
If this is the case, you will have to put them on the compiler classpath in advance.
In any case, just decompile all the package (I use jd-gui), change the class, compile, open the original jar as a zip, put the new class on it, and you are done.
Another approach is to create dummy classes for the missing dependencies...
Personally, I really discourage this approach of "reverse engineering" working with an already compiled package, but I understand situations where you don't have access to the original source code, but you need to fix something urgently (ex: working in a company, where your code comes from an external provider on the other side of the planet...)
You may try to copy & paste the code in Java IDE such as Eclipse, NetBeans, etc and ask the IRC to compile for u. Have a nice day :)

Issue with classpath in Java project

I'm currently working on a Java project which uses Tibco rendezvous control.
I have the tibrvj.jar file on my build path. But I'm not able to see the classes which are there in the package "com.tibco.tibrv".
This package is in the jar which is on my build path.
When I try to open the class in that package , the message displayed on class editor is : "Source not found. the jar file tibrvj.jar has no source attachment".
Is there anything that could be done to set it right ?
This looks like an IDE specific message. Which IDE are you using?
The jar in your classpath only contains the java classes (bytecode) and not the source files (.java files). It looks like you are trying to open the class in your IDE/editor. Since you do not have the java files corrsponding to these classes, the editor is comlaining.
As these are Tibco specific classes, the only option is to "decompile" the classes to see the corresponding java code. Consider using a decompiler like DJ Java Decompiler
It depends on what you mean by "I'm not able to see the classes." If you mean you're getting build failures, that's possibly a matter of not actually having it properly in your build path (how are you building?) or maybe not having the right imports.
If you mean you can't see the source code, you'll need to get hold of it somehow (is Tibco open source?) and then configure your IDE so that it knows where the source is. Of course you don't need to source just to be able to build your app which uses Tibco.
If you could give more information about exactly which problem you're facing, along with what IDE you're using (if any) we could probably help more.

Categories