如何从文件系统加载任意java .class文件并进行反思? [英] How to load an arbitrary java .class file from the filesystem and reflect on it?

查看:56
本文介绍了如何从文件系统加载任意java .class文件并进行反思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个命令行实用程序,该实用程序根据外部类文件的反映进行一些操作。我会传入.class文件或源文件(可能是通配符)的路径。在执行过程中的某个时刻,我需要为每个类获取Class对象,而不事先知道它们的包名称。要做什么呢?我应该如何看待入门?我也可以访问类的源文件。

I want to make a command-line utility that does some operations based on reflection of external class files. I would pass in a path to .class files or source files(possibly wildcards). At some point during the execution, I need to get Class objects for each class, not knowing their package names beforehand. What would it take to do this? What can I look at to get started? I also have access to the classes' source files.

这是在Java 1.6中。

this is in java 1.6.

推荐答案

您将需要创建一个 ClassLoader 并使用 defineClass 创建一个 Class 您已阅读的文件。 向您的应用程序添加动态Java代码是有趣的是,这类文章甚至描述了源代码的编译。

You will need to create a ClassLoader and use defineClass to create a Class from the file you've read. Add dynamic Java code to your application is an interesting to read about such things, it even describes the compilation of the sources.

这篇关于如何从文件系统加载任意java .class文件并进行反思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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