从类名获取Java源代码 [英] Obtaining Java source code from class name

查看:163
本文介绍了从类名获取Java源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从类名获取Java源代码?

Is there a way to obtain the Java source code from a class name?

例如,如果我可以访问库 java.io.File ,我想要它的源代码。

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) {...}

用户的源代码,等等。

推荐答案

有没有办法从类名获取java源?例如:...

您可能需要几种可能的解决方案之一。不知道你真正想要如何处理这些信息,我们不能对我们的建议非常精确,但如果可能的话,我会从你的源代码开始。 JSE源代码在线可用,许多开源库,但可能不总是这样。此外,你需要保持它的所有组织,当你想找到它,很像一个类路径,而Class对象更容易掌握和操纵,而不必再次解析文本。

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.

如果在运行时只需要一个类的信息,只需使用 Java反射API 。有了它,给了一个 Class对象您可以,例如,获取特定字段的类型,列出所有字段并对其进行迭代等...:

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对于发现程序中的类的信息很有用,当然,

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.

记住你可以查找一个类对象(只要它在运行时的类路径中)与<$ c $

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.

如果你需要更多的信息,并且实际上想要操纵类,你想要字节码操作。有些人试图生成源代码,编译成字节码并加载到他们的程序,但是相信我 - 使用一个固体的字节码操作API远远更容易。我推荐 ASM

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.

有了它,你不仅可以获取有关类的信息,还可以添加新字段,新方法,创建新类...甚至可以加载类的多个变体(如果您感觉自虐)。使用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.

做需要源,并没有它可用,你可以反编译它从类对象使用各种反编译器之一。他们使用与上面两个相同的信息和技术,但进一步和[尝试]生成源代码。注意它并不总是工作。我建议Jode,但一个体面的名单,和其他人的比较是在线

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.

如果你有源,而且只是想查找,也许你需要的是放。 java文件在大树中的某个地方,并根据需要基于包名检索。

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")

你想要更多的逻辑来检查类类型,因为显然 primatives 没有类定义,并且你希望处理数组类型的方式不同。

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.

你可以按名称查找一个类(如果.class文件在您的类路径)与 Class.forName(MyClass)

You can lookup a class by name (if the .class files are on your classpath) with Class.forName("MyClass").

这篇关于从类名获取Java源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆