简体   繁体   English

运行时的类

[英]class at runtime

有没有办法在运行时创建Java类@(类方法和变量),使用Java反射API

You can't do that using reflection. 你不能用反射做到这一点。 You need a bytecode manipulation library, like Jakarta BCEL . 您需要一个字节码操作库,如Jakarta BCEL

The standard Java API provides a set of static methods, that allows you to dynamically create a class that implements one (or many) interfaces. 标准Java API提供了一组静态方法,允许您动态创建实现一个(或多个)接口的类。 Those methods are part of the class java.lang.reflect.Proxy. 这些方法是java.lang.reflect.Proxy类的一部分。

What do you require this for? 你需要这个什么?

Interpreting the question in a very loose manor I can think of four likely options. 在一个非常松散的庄园里解释这个问题我可以想到四个可能的选择。

If you have a class that you add something too you might find that Aspect-oriented programming is what you are really after. 如果你有一个类,你也可以添加一些东西,你可能会发现面向Aspect的编程就是你真正想要的。

If you have an interface that you want to dynamically implement (as posted by barjak ) what you want is java.lang.reflect.Proxy . 如果你有一个你想要动态实现的接口(由barjak发布)你想要的是java.lang.reflect.Proxy This does not let create "code" at runtime but rather allows you link existing code to to a interface. 这不允许在运行时创建“代码”,而是允许您将现有代码链接到接口。

Finally (at three I know) you have actually building random classes at runtime. 最后(我知道三个)你实际上已经在运行时构建了随机类。 This you will need something like cglib or BCEL . 这需要像cglibBCEL这样的东西。 While there are cases when this is required it is IMO rare. 虽然有些情况需要这样,但IMO很少见。

One other option is that you don't really need runtime but rather build time. 另一个选择是你不需要运行时而是需要构建时间。 In this case you might be able to use annotations and apt (Java 5) / Processor (Java 6). 在这种情况下,您可以使用注释apt (Java 5)/ Processor (Java 6)。

you can use javassist. 你可以使用javassist。 here is sudo code 这是sudo代码

        javassist.ClassPool pool = new ClassPool(true); 
        CtClass bclass = pool.makeClass("brandnewclass);
        bclass.addConstructor(CtNewConstructor.defaultConstructor(bclass));
        CtClass[] fieldclasses = new CtClass[fields.length];
        CtClass serClass = pool.get(Serializable.class.getName());
        bclass.addInterface(serClass);

Class clazz = pool.loadClass("className"); class clazz = pool.loadClass(“className”); obj = clazz.newInstance(); obj = clazz.newInstance();

Use reflection to extract values from an existing class and assign values to new class. 使用反射从现有类中提取值并将值分配给新类。 hope this helps. 希望这可以帮助。 Gopi 戈皮

Sure there is. 当然有。 You need a java.lang.Class instance initially, for the target class you wish to create. 对于要创建的目标类,最初需要一个java.lang.Class实例。 Depending on your structure, this might either be passed in by a caller (if they're supplying the concrete class they want created), or you can statically access the class variable (eg MyFooImpl.class ). 根据您的结构,这可能由调用者传入(如果他们提供他们想要创建的具体类),或者您可以静态访问类变量(例如MyFooImpl.class )。

The simplest way is to call Class.newInstance() . 最简单的方法是调用Class.newInstance() This invokes the default, no-arg constructor (assuming there is one for the class; if not it throws an exception). 这将调用默认的no-arg构造函数(假设该类有一个;如果不是,则抛出异常)。

If you need to invoke a particular constructor with some argument, you need to call Class.getConstructor() to get a Constructor instance, which you can then call newInstance on. 如果需要使用某个参数调用特定构造函数,则需要调用Class.getConstructor()以获取Constructor实例,然后可以调用newInstance

In all cases you'll need to deal with reflection exceptions that you wouldn't get if invoking the constructor directly. 在所有情况下,您都需要处理直接调用构造函数时无法获得的反射异常。

Big edit : I assume your question was about creating instances of a class via reflection. 大编辑 :我假设你的问题是关于通过反射创建一个类的实例。 However I'm beginning to think that you're asking about defining new classes through at runtime. 但是我开始认为你要求在运行时定义新的类。 If so, then reflection won't help you here - you'd need to invoke a compiler programatically, which I believe can be done but I'm not 100% on the details. 如果是这样,那么反射在这里不会帮助你 - 你需要以编程方式调用编译器,我相信可以这样做,但我不是100%的细节。 I think you'd also have to go through some hoops to get the ClassLoader to pick up your new class too. 我想你也必须通过一些箍来让ClassLoader接收你的新课程。

您可以使用Janino创建源代码字符串并将其编译为类文件。

As people have already mentioned, there's no way of creating new classes at runtime using reflection. 正如人们已经提到的那样,没有办法在运行时使用反射创建新类。 One library that I know is used by different mocking libraries and the likes is cglib . 我知道的一个库被不同的模拟库使用,例如cglib

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM