I am looking for a Java library (source code parser) that would help me extract unqualified names of all class names being used in the source code. For example for the given code example:
public class Example {
private ClassName1;
protected ClassName2 instance = new ClassName2();
public Example() {
ClassName3 test = new ClassName3();
}
public doSomething() {
//ClassName4 test = new ClassName4("SomeExampleString");
ClassName5 test = new ClassName5("ExampleString2");
}
}
I need to get the following list:
ClassName1, ClassName2, ClassName3, ClassName5
as this is the list of all names of classes that are being "used" in the source code.
So far I have tried to write a simple parser that would do this for me but is not robust enough to be used in the real world. I have looked into a few Java parsers too, but the problem is that I don't know how this problem would be called to look into their code for a solution, which I believe exists in the domain of existing Java parsers.
So what I am looking for is a Java source parser that would allow me to obtain a class name lists like the one in the example and a short example on how to achieve this or directions where to look for / how this problem is properly called.
NOTE: I am not looking for a method to detect all classes loaded by JVM nor classes in classpath, but a way to detect classes in textual sense by parsing original Java source code that is not compiled.
If you're just looking for a robust parser, looks like javaparser is pretty good.
You may want to check out this question where the solution is to view all of the classes loaded by the JVM by using the -verbose:class flag. One answer there also mentions using reflection (which was my initial reaction) with this API.
If that question doesn't completely solve your problem (since it would show all classes loaded, "used" or not), and you're not having any luck with reflection, you could try something like this that combines the first solution from there with my idea:
Use whatever parser you have to parse tokens in the source code
Use the -verbose:class flag when running some main program that instantiates the class in the file you want to check
grep whatever tokens your parser tokenized from that output
So, some program Main.java:
public class Main {
public static void main(String[] args) {
Example e = new Example();
}
}
Your (or some other) parser with a main method (psuedo-code):
tokens = parse_tokens()
print "\\\|".join(tokens)
And in bash:
javac *.java
TOKS="$(java MyParser Example.java)"
java -verbose:class Main | grep ${TOKS}
That way, you don't need a robust parser, just something to tokenize Java code. Just a thought, not sure if that would work perfectly or not.
I am trying to let users of my program rename classes and methods in the class. The new names can be read in and configured at run time and they will call these classes and methods in a scripting language using Java Script Engine. I need a way to bind their new names to the real names of the classes and methods. I have been looking at Reflection but I do not think this can provide me with the capability I need, or is this even possible?
Ex:
public class RealName {
public void printHello() {
System.out.println("Hello");
}
}
Then in maybe Paython say
obj = new NewName()
obj.hello()
Tell me if this is impossible please!
You can not change the method names, but you can bind an instance of an object to a given name and inject that into the context of the scripting language.
This would only work for that instance of the class, not for instantiating new instances.
If you really want this you may be able to generate sub classes with the new name and method names in the target scripting language and inject them into to the context to get the effect you are looking for.
Having said all that I can't really come up with a good reason to do any of this.
To answer my question from what I've found no you cannot use reflection to bind a class to a new name. In fact I found no easy way to do dynamic renaming.
What I did to overcome this was to write code from a string to a file, save that file with extension .java, compile that file, then use it with reflection or better yet use it inside a script using the Java ScriptEngine API (that way you can avoid the ugly reflection code and actually have everything dynamic and on the fly).
Here's a starting point for creating the file,
Dynamic in-memory compilation
And here's something for scripting Java,
Scripting for Java
I would like to know if it is possible to use ctags to extract class usage from within Java methods. So far, I was able to only use it to get a listing of methods and instance variables but not which classes are utilized within methods.
E.g.
...
public void doSomething(MyClass myClassInst) {
int someVar = myClassInst.getSomeVar();
System.out.println("Some var is " + someVar);
}
...
Right now all I get is a recognition of the method declaration. But I would like to extract that doSomething is also utilizing the classes MyClass and System but not just to parse the text but also know the full class address including the package (java.lang.System and com.mypackage.MyClass).
In order to be able to extract this kind of metadata, ctags would need to have access to the compiler's abstract syntax tree and I am not sure if it does that or it simply does text parsing of the source code.
Is there a way to accomplish what I am trying to do using ctags or does it eclipse the scope of its functionality?
You can use asm or BCEL to achieve this.
My problem is that I am trying to interop with a Java app whose jar file contains obfuscated byte code. The app releases updates ever month or so, and when they do a release, most of the class and method names change.
Thus, the method proposed here:
http://rickyclarkson.blogspot.com/2006/07/duck-typing-in-java-and-no-reflection.html
or
Simulating duck typing in Java
won't work in my solution because because I would have to update the interfaces by hand each time.
What I do have however is an automatically generated (for the most part) mapping from deobfuscated class name <-> obfuscated class name by means of parsing the class files for calls to debug logging calls in the form of:
logger.log(severity, "ClassName", "MethodName() has some error")
What I generate is something like this:
public final static String MyRealName = "someObfuscatedName".
public final static String MyRealName_myCoolMethod = "someMethodName".
I have a fairly decent solution for interacting with objects of "myRealName" via the reflection API and simply proxy objects that implement a subset of functionality of the object it is proxying. Somewhat like this:
class MyRealName {
private Object backingObject;
public MyRealName(Object o) { backingObject = o;}
public void myCoolMethod() {
return getFieldValue(backingObject
, DeobNames.MyRealName_myCoolMethod);
}
}
However, the problem arises when I want to test my code in the absence of the obfuscated app from running - startup time and setup could take several minutes whereas I want test verification to be a couple of seconds.
What I am looking for is some way of easily adapting my tests to accommodate the frequently changing class names that my code depends upon.
I was intrigued by the power of tools like JMockit, etc in that they were able to automatically generate mock objects for me, I'm hoping to be able to have some thin layer that will enable to still have the majority of my mocks generated quite easily vs having to manually write everything, every update.
If you are running the code from Java, I don't think this is possible.
However if you are running the code with Groovy then you can use Groovy's methodMissing
See: http://groovy.codehaus.org/Using+methodMissing+and+propertyMissing
Is there a way to obtain the Java source code from a class name?
For example, if I have access to the library with the class java.io.File, I want its source code.
I am working on a kind of parser and I need the source at execution time. I have also to search it recursively.
Say the aforementioned class has this method:
int method (User user) {...}
I would need to obtain User's source code, and so on and so forth with its inner classes.
Is there any way to obtain the java source from a class name? For example:...
You may want one of several possible solutions. Without knowing what you really want to do with the information, we can't be very precise with our recommendations, but I'd start by steering you away from source code if possible. JSE source code is available online, as are many open source libraries, but that may not always be the case. Additionally, you'll need to keep it all organized when you want to find it, much like a classpath, whereas the Class objects are much easier to get hold of, and manipulate, without having to parse text again.
Reflection
If you just need information about a class at runtime, just use the Java Reflection API. With it, given a Class object you can, for example, get the types of a specific field, list all fields and iterate over them, etc...:
Class clazz = User.class;
Field field = clazz.getDeclaredField("var");
System.out.println(field.getType().getName());
Reflection is useful for discovering information about the classes in the program, and of course you can walk the entire tree without having to find source code, or parse anything.
Remember you can lookup a class object (as long as it's on the classpath at runtime) with Class.forName("MyClass") and reflect on the resulting Class.
Bytecode Manipulation
If you need more than information, and actually want to manipulate the classes, you want bytecode manipulation. Some have tried to generate source code, compile to bytecode and load into their program, but trust me - using a solid bytecode manipulation API is far, far easier. I recommend ASM.
With it, you can not only get information about a class, but add new fields, new methods, create new classes... even load multiple variations of a class if you're feeling self-abusive. An example of using ASM can be found here.
Decompilation
If you really, really do need the source, and don't have it available, you can decompile it from a class object using one of the various decompilers out there. They use the same information and techniques as the above two, but go further and [attempt] to generate source code. Note that it doesn't always work. I recommend Jode, but a decent list, and comparison of others is available online.
File Lookup
If you have the source and really just want to look it up, maybe all you need is to put the .java files somewhere in a big tree, and retrieve based on package name as needed.
Class clazz = User.class;
String path = clazz.getPackage().getName().replaceAll("\\.","/");
File sourceFile = new File(path, clazz.getName() + ".java")
You want more logic there to check the class type, since obviously primatives don't have class definitions, and you want to handle array types differently.
You can lookup a class by name (if the .class files are on your classpath) with Class.forName("MyClass").
You can get a good approximation of the source from a class file using the JAVA decompiler of your choice. However, if you're really after the source of java.io.File then you can download that.
The best and simplest bet can be javap
hello.java
public class hello
{
public static void main(String[] args)
{
System.out.println("hello world!");
world();
}
static public void world()
{
System.out.println("I am second method");
}
}
do a javap hello and you will get this:
Compiled from "hello.java"
public class hello extends java.lang.Object{
public hello();
public static void main(java.lang.String[]);
public static void world();
}
Yes, if you download the source code. It's available for public download on the official download page.
If you're using Eclipse whenever you use the class you could right click > View Source (or simply click the class > F3) and it'll open a new tab with the source.
You can print the resource path from where the class was loaded with
URL sourceURL=obj.getClass().getProtectionDomain().getCodeSource().getLocation();
It will be a .class file , .jar,.zip, or something else.
So what you're trying to do is get the Java class at execution. For this, you need Java reflections.
If your goal is to get information about what's in a class, you may find the Java reflection API to be an easier approach. You can use reflection to look up the fields, methods, constructors, inheritance hierarchy, etc. of a class at runtime, without needing to have the source code for the class available.
Is there any way to obtain the java source from a class name?
The answer is complicated, not least because of the vagueness of your question. (Example notwithstanding).
In general it is not possible to get the real, actual Java source code for a class.
If you have (for example) a ZIP or JAR file containing the source code for the classes, then it is simple to extract the relevant source file based on the classes fully qualified name. But you have to have gotten those ZIP / JAR files from somewhere in the first place.
If you are only interested in method signatures, attribute names and types and so on, then much of this information is available at runtime using the Java reflection APIs. However, it depends on whether the classes were compiled with debug information (see the -g option to the javac compiler) how much will be available. And this is nowhere like the information that you can get from the real source code.
A decompiler may be able to generate compilable source code for a class from the bytecode files. But the decompiled code will look nothing like the original source code.
I guess, if you have a URL for a website populated with the javadocs for the classes, you could go from a class name, method name, or public attribute name to the corresponding javadoc URL at runtime. You could possibly even "screen scrape" the descriptions out of the javadocs. But once again, this is not the real source code.