是否可以打开包含类的.txt/.java文件,并对其进行反射? [英] Is it possible to open a .txt/.java file containing a class, and using reflection on it?

查看:46
本文介绍了是否可以打开包含类的.txt/.java文件,并对其进行反射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是,打开其中包含Java代码的文件作为类,而不是文件.所以基本上我想:

What I mean by this, is opening a file with a Java code in it as a class, not as a file. So basically I want to:

->在自编写的Java应用程序(.txt/.log/.java)中打开纯文本文件->识别文件中的类,例如:

-> Open a pure text file in a self written Java application (.txt/.log/.java) -> Recognize class(es) in the file, for example:

public class TestExample { 
private String example; 

public String getExample() { 
return example; 
} 
} 

我将用手写程序打开它.而不是将文件中的文本识别为String或纯文本,而是在其中找到类TestExample.然后将其保存为类.

I will open this in a hand-written program. Instead of recognizing the text in the file as a String or as pure text, it will find the class TestExample in it. Then it will save this as a Class.

->在类上使用Reflection API->从文件中获取字段,方法等并显示它们

-> Use the Reflection API on the class -> Get fields, methods, etc from file and display them

这可能吗?

推荐答案

是的,有可能.

看看这个例子.

http://weblogs.java.net/blog/malenkov/archive/2008/12/how_to_compile.html

知道如何查找和读取文件(这非常简单),然后可以使用显示的代码,尤其是

Knowing how to find and read files (which is really simple), you can then use the code shown, especially

javax.tools.JavaCompiler
javax.tools.ToolProvider.getSystemJavaCompiler()

编译代码,然后使用反射对其进行调用.

to compile the code and then call it using reflection.

这篇关于是否可以打开包含类的.txt/.java文件,并对其进行反射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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